Có nhiều phương pháp để tìm các số nhỏ nhất và lớn nhất trong một mảng JavaScript và hiệu suất của các phương pháp này thay đổi dựa trên số lượng phần tử trong mảng. Hãy thảo luận riêng về từng vấn đề và đưa ra kết quả thử nghiệm cuối cùng
Đối với các mảng thông thường, bạn có thể sử dụng Math. tối đa với ba dấu chấm
let max = Math.max[...arrayOfNumbers];
hãy để arrayOfNumbers = [4, 12, 62, 70, -10]; . nhật ký [Toán. tối đa [. arrayOfNumbers]];
Sử dụng dấu ba chấm […] giúp dễ dàng gọi bất kỳ hàm nào cần đối số
Toán học. hàm max sử dụng phương thức apply[] để tìm phần tử lớn nhất trong một mảng số
Math.min.apply[Math, testArr];
Math.max.apply[Math, testArr];
hãy để arrayOfNumbers = [4, 12, 62, 70, -10]; . nhật ký [Toán. tối đa. áp dụng [Math, arrayOfNumbers]];
Toán tử cũng được sử dụng để lấy giá trị lớn nhất của một mảng. Nó mở rộng một mảng số vào danh sách các đối số, chẳng hạn như với Math. tối thiểu [] và Toán. tối đa[]
Math.min[...testArr];
Math.max[...testArr];
Math.min.apply[Math, testArr];
Math.max.apply[Math, testArr];
Bạn có thể sử dụng vòng lặp tiêu chuẩn cho hàng tấn đối số vì vòng lặp for không có giới hạn về kích thước
let max = testArray[0];
for [let i = 1; i < testArrayLength; ++i] {
if [testArray[i] > max] {
max = testArray[i];
}
}
let min = testArray[0];
for [let i = 1; i < testArrayLength; ++i] {
if [testArray[i] < min] {
min = testArray[i];
}
}
cho arrayList = [1, 2, 3, 4, 3, 21, 0]; . chiều dài; . nhật ký [tối đa];
Bạn cũng có thể sử dụng phương thức reduce[] để lấy số lượng mục
testArr.reduce[function [a, b] {
return Math.max[a, b];
}];
testArr.reduce[function [a, b] {
return Math.min[a, b];
}];
hãy để mảngList = [1, 2, 3, 4, 3, 20, 0]; . giảm [[trước, hiện tại] => { trả về Toán học. max[trước, hiện tại] }]; . nhật ký [maxNum];
Các phương thức áp dụng và trải rộng có giới hạn là 65536 xuất phát từ giới hạn số lượng đối số tối đa. Vào năm 2019, giới hạn là kích thước tối đa của ngăn xếp cuộc gọi, nghĩa là kích thước tối đa cho các số trong trường hợp áp dụng và giải pháp trải rộng là khoảng 120000. Tập lệnh sau sẽ tính toán giới hạn cho môi trường cụ thể của bạn
let testArr = Array.from[{
length: 10000
}, [] => Math.floor[Math.random[] * 2000000]];
for [i = 10000; i < 1000000; ++i] {
testArr.push[Math.floor[Math.random[] * 2000000]];
try {
Math.max.apply[null, testArr];
} catch [e] {
console.log[i];
break;
}
}
Khi bạn kiểm tra tất cả các ví dụ đã cho ở trên, kết quả cho thấy vòng lặp tiêu chuẩn là nhanh nhất. Sau đó đến các phương pháp áp dụng và lây lan, sau chúng là giảm, đây là phương pháp chậm nhất. When dealing with large arrays of more than 40 elements, the spread operator is considered a worse choice compared to other methods
A Simple Solution is to sort the array in increasing order. The first two elements in the sorted array would be the two smallest elements. In this approach, if the smallest element is present more than one time then we will have to use a loop for printing the unique smallest and second smallest elements.
Below is the implementation of the above approach
C++
//C++ simple approach to print smallest
//and second smallest element.
#include
using
smallest element is 1 second smallest element is 90
smallest element is 1 second smallest element is 91
smallest element is 1 second smallest element is 92
smallest element is 1 second smallest element is 93
smallest element is 1 second smallest element is 92
smallest element is 1 second smallest element is 95
smallest element is 1 second smallest element is 92
smallest element is 1 second smallest element is 97_______6_______8
smallest element is 1 second smallest element is 99
smallest element is 1 second smallest element is 98
smallest element is: 1 second smallest element is: 101
smallest element is: 1 second smallest element is: 102
smallest element is: 1 second smallest element is: 103
smallest element is: 1 second smallest element is: 104
smallest element is: 1 second smallest element is: 105
smallest element is: 1 second smallest element is: 106_______17_______7
smallest element is: 1 second smallest element is: 108
smallest element is: 1 second smallest element is: 106_______26_______0
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second1
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second2
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second3
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second4
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second5
Java
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second6
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second7
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second8
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second7
The smallest element is 1 and second Smallest element is 100
The smallest element is 1 and second Smallest element is 101
The smallest element is 1 and second Smallest element is 102
The smallest element is 1 and second Smallest element is 103
//and second smallest element.
The smallest element is 1 and second Smallest element is 105
The smallest element is 1 and second Smallest element is 106
The smallest element is 1 and second Smallest element is 107
The smallest element is 1 and second Smallest element is 108
The smallest element is 1 and second Smallest element is 109
smallest element : 1 second smallest element : 20
smallest element : 1 second smallest element : 21
smallest element is 1 second smallest element is 92
smallest element : 1 second smallest element : 23
smallest element : 1 second smallest element : 24
smallest element : 1 second smallest element : 25
smallest element : 1 second smallest element : 26
smallest element : 1 second smallest element : 25
smallest element : 1 second smallest element : 28
smallest element : 1 second smallest element : 25
//C++ simple approach to print smallest
0smallest element : 1 second smallest element : 25
//C++ simple approach to print smallest
2smallest element : 1 second smallest element : 25
//C++ simple approach to print smallest
4//C++ simple approach to print smallest
5smallest element : 1 second smallest element : 21
smallest element is 1 second smallest element is 92
//C++ simple approach to print smallest
8//C++ simple approach to print smallest
9
smallest element : 1 second smallest element : 21
//and second smallest element.
1smallest element : 1 second smallest element : 21
//and second smallest element.
3smallest element : 1 second smallest element : 21
//and second smallest element.
5//C++ simple approach to print smallest
9
smallest element : 1 second smallest element : 21
//and second smallest element.
8smallest element : 1 second smallest element : 21
#include
0smallest element is: 1 second smallest element is: 107
#include
2#include
3#include
4smallest element : 1 second smallest element : 21
#include
01] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second0
#include
2//C++ simple approach to print smallest
4#include
41] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second4
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second4
using
3
Python3
using
4
using
5
using
6
using
7using
8 using
9
smallest element : 1 second smallest element : 24
smallest element : 1 second smallest element : 25
smallest element : 1 second smallest element : 26
smallest element : 1 second smallest element : 25_______45_______8
smallest element : 1 second smallest element : 25
//C++ simple approach to print smallest
0smallest element : 1 second smallest element : 25
//C++ simple approach to print smallest
2smallest element : 1 second smallest element : 25
//C++ simple approach to print smallest
4_______6_______11smallest element is 1 second smallest element is 912_______1194_______8
smallest element is 1 second smallest element is 914
smallest element is 1 second smallest element is 915
smallest element is 1 second smallest element is 916
smallest element is 1 second smallest element is 917
smallest element is 1 second smallest element is 918
smallest element is 1 second smallest element is 919
smallest element is 1 second smallest element is 920
smallest element is 1 second smallest element is 921
smallest element is: 1 second smallest element is: 107
smallest element is 1 second smallest element is 923
smallest element is 1 second smallest element is 924
smallest element is 1 second smallest element is 925
#include
3smallest element is 1 second smallest element is 927
smallest element is 1 second smallest element is 920
smallest element is 1 second smallest element is 921
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second0
smallest element is 1 second smallest element is 923
smallest element is 1 second smallest element is 924
smallest element is 1 second smallest element is 925
//C++ simple approach to print smallest
4smallest element is 1 second smallest element is 927
smallest element is 1 second smallest element is 936
C#
smallest element is 1 second smallest element is 937
smallest element is 1 second smallest element is 938
using
smallest element is 1 second smallest element is 940
The smallest element is 1 and second Smallest element is 106
The smallest element is 1 and second Smallest element is 101
smallest element is 1 second smallest element is 943
smallest element : 1 second smallest element : 20
//C++ simple approach to print smallest
9____36_______5
//C++ simple approach to print smallest
9_______36_______7
The smallest element is 1 and second Smallest element is 106
The smallest element is 1 and second Smallest element is 108
smallest element is 1 second smallest element is 951
//C++ simple approach to print smallest
9____45_______0
smallest element : 1 second smallest element : 21_______6_______2
smallest element is 1 second smallest element is 956
smallest element : 1 second smallest element : 21
smallest element is 1 second smallest element is 92
smallest element is 1 second smallest element is 959
smallest element : 1 second smallest element : 21
//and second smallest element.
1smallest element : 1 second smallest element : 21
//and second smallest element.
3smallest element : 1 second smallest element : 21_______6_______65
smallest element : 1 second smallest element : 21
//and second smallest element.
8smallest element : 1 second smallest element : 21
smallest element is 1 second smallest element is 969
smallest element is: 1 second smallest element is: 107
smallest element is 1 second smallest element is 971
smallest element : 1 second smallest element : 21
smallest element is 1 second smallest element is 969
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second0
smallest element is 1 second smallest element is 975
//C++ simple approach to print smallest
9____26_______4
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second4
smallest element is 1 second smallest element is 979
Javascript
smallest element is 1 second smallest element is 980
smallest element is 1 second smallest element is 981
smallest element is 1 second smallest element is 938
smallest element is 1 second smallest element is 983
smallest element is 1 second smallest element is 984
smallest element is 1 second smallest element is 985
//and second smallest element.
1
//and second smallest element.
3
smallest element is 1 second smallest element is 988
//and second smallest element.
8
smallest element is 1 second smallest element is 990
smallest element is: 1 second smallest element is: 107
smallest element is 1 second smallest element is 992
smallest element is 1 second smallest element is 993
smallest element is 1 second smallest element is 994
smallest element is 1 second smallest element is 990
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second0
smallest element is 1 second smallest element is 997
smallest element is 1 second smallest element is 993
smallest element is 1 second smallest element is 994
using
3
smallest element is: 1 second smallest element is: 1001
Đầu ra
smallest element is 1 second smallest element is 9
Độ phức tạp về thời gian. O[N*logN]
không gian phụ trợ. Ô[1]
Một cách tiếp cận hiệu quả. Giải pháp tốt hơn là quét mảng hai lần. Trong lần duyệt đầu tiên, hãy tìm phần tử tối thiểu. Đặt phần tử này là x. Trong lần duyệt thứ hai, tìm phần tử nhỏ nhất lớn hơn x
Sử dụng phương pháp này, chúng ta có thể khắc phục sự cố của Phương pháp 1 xảy ra khi phần tử nhỏ nhất có mặt trong một mảng nhiều lần
Giải pháp trên yêu cầu hai lần duyệt mảng đầu vào.
C++
smallest element is: 1 second smallest element is: 1002
smallest element is: 1 second smallest element is: 1003
smallest element is: 1 second smallest element is: 1004
using
smallest element is 1 second smallest element is 90
smallest element is 1 second smallest element is 91
smallest element is 1 second smallest element is 92
smallest element is: 1 second smallest element is: 1009
smallest element : 1 second smallest element : 20
smallest element : 1 second smallest element : 21____6_______2
smallest element is: 1 second smallest element is: 1013
smallest element : 1 second smallest element : 21
smallest element is 1 second smallest element is 92
smallest element is: 1 second smallest element is: 1016
smallest element is 1 second smallest element is 98
smallest element is: 1 second smallest element is: 1018
smallest element is 1 second smallest element is 98
smallest element is: 1 second smallest element is: 101
smallest element : 1 second smallest element : 21____6_______2
smallest element is: 1 second smallest element is: 1023
smallest element : 1 second smallest element : 21
smallest element is: 1 second smallest element is: 1025
smallest element : 1 second smallest element : 21
smallest element is: 1 second smallest element is: 1027
smallest element : 1 second smallest element : 21
smallest element is: 1 second smallest element is: 1029
smallest element is 1 second smallest element is 921
smallest element is 1 second smallest element is 92
smallest element is: 1 second smallest element is: 1032
smallest element : 1 second smallest element : 21____45_______0
smallest element is: 1 second smallest element is: 1035_______17_______36
smallest element is: 1 second smallest element is: 1037
smallest element is: 1 second smallest element is: 1035_______45_______0
smallest element is: 1 second smallest element is: 1040
smallest element is: 1 second smallest element is: 1041
smallest element is: 1 second smallest element is: 1035_______26_______4
smallest element : 1 second smallest element : 21____26_______4
smallest element : 1 second smallest element : 21
smallest element is: 1 second smallest element is: 1047
smallest element is: 1 second smallest element is: 1048
smallest element is: 1 second smallest element is: 1049
smallest element : 1 second smallest element : 21____6_______2
smallest element is: 1 second smallest element is: 1052
smallest element : 1 second smallest element : 21
smallest element is: 1 second smallest element is: 1054
smallest element : 1 second smallest element : 21
smallest element is: 1 second smallest element is: 1029
smallest element is 1 second smallest element is 921
smallest element is 1 second smallest element is 92
smallest element is: 1 second smallest element is: 1032
smallest element : 1 second smallest element : 21____45_______0
smallest element is: 1 second smallest element is: 1035_______17_______36
smallest element is: 1 second smallest element is: 1064
smallest element is: 1 second smallest element is: 1035_______45_______0
smallest element is: 1 second smallest element is: 1040
smallest element is: 1 second smallest element is: 1068
smallest element is: 1 second smallest element is: 1035_______26_______4
smallest element : 1 second smallest element : 21____26_______4
smallest element : 1 second smallest element : 21
smallest element is: 1 second smallest element is: 1047
smallest element is: 1 second smallest element is: 1075
smallest element is: 1 second smallest element is: 1076
smallest element : 1 second smallest element : 21____26_______2
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second3
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second4
smallest element is: 1 second smallest element is: 1081
Java
smallest element is: 1 second smallest element is: 1082
smallest element is: 1 second smallest element is: 1003
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second7
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second8
The smallest element is 1 and second Smallest element is 101
The smallest element is 1 and second Smallest element is 102
smallest element : 1 second smallest element : 21
The smallest element is 1 and second Smallest element is 106
The smallest element is 1 and second Smallest element is 107
The smallest element is 1 and second Smallest element is 108
The smallest element is 1 and second Smallest element is 109
smallest element : 1 second smallest element : 21____45_______0
smallest element is: 1 second smallest element is: 1035_______6_______2
smallest element is: 1 second smallest element is: 1097
smallest element is: 1 second smallest element is: 1098
smallest element : 1 second smallest element : 25
smallest element : 1 second smallest element : 26
smallest element : 1 second smallest element : 25
//C++ simple approach to print smallest
4smallest element : 1 second smallest element : 25
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second04
smallest element : 1 second smallest element : 25
//C++ simple approach to print smallest
2smallest element : 1 second smallest element : 25
//C++ simple approach to print smallest
4 //C++ simple approach to print smallest
5smallest element is: 1 second smallest element is: 1035_______6_______2
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second12
smallest element is: 1 second smallest element is: 1035_______6_______2
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second15
smallest element is: 1 second smallest element is: 1035_______17_______25
smallest element is: 1 second smallest element is: 1035_______17_______27
smallest element is: 1 second smallest element is: 1035_______17_______29
smallest element is 1 second smallest element is 921
smallest element is 1 second smallest element is 92
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second24
#include
31] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second26
smallest element is: 1 second smallest element is: 1040
smallest element is: 1 second smallest element is: 1036
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second29
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second30_______17_______41
smallest element is: 1 second smallest element is: 1040
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second4
smallest element is: 1 second smallest element is: 1035_______26_______4
smallest element is: 1 second smallest element is: 1035_______1193_______0
smallest element is: 1 second smallest element is: 1048
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second39_______26_______40
smallest element is: 1 second smallest element is: 1035_______6_______2
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second43
smallest element is: 1 second smallest element is: 1035_______26_______45
smallest element is: 1 second smallest element is: 1035_______26_______47
smallest element is: 1 second smallest element is: 1035_______17_______29
smallest element is 1 second smallest element is 921
smallest element is 1 second smallest element is 92
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second24
#include
31] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second26
smallest element is: 1 second smallest element is: 1040
smallest element is: 1 second smallest element is: 1036
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second57
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second30_______26_______59
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second30_______17_______68
smallest element is: 1 second smallest element is: 1040
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second4
smallest element is: 1 second smallest element is: 1035_______26_______4
smallest element is: 1 second smallest element is: 1035_______1193_______0
smallest element is: 1 second smallest element is: 1075
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second39_______26_______70
smallest element : 1 second smallest element : 21____26_______4
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second4
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second74
con trăn
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second75
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second76
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second7
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second78
using
7using
8 using
9
smallest element is: 1 second smallest element is: 1098
smallest element : 1 second smallest element : 25
smallest element : 1 second smallest element : 26
smallest element : 1 second smallest element : 25_______1191_______4
smallest element : 1 second smallest element : 25
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second04
smallest element : 1 second smallest element : 25
//C++ simple approach to print smallest
2smallest element : 1 second smallest element : 25
//C++ simple approach to print smallest
4_______6_______11smallest element is 1 second smallest element is 912_______1194_______8
smallest element is 1 second smallest element is 914
smallest element is 1 second smallest element is 915
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second98
using
8 The smallest element is 1 and second Smallest element is 1000
The smallest element is 1 and second Smallest element is 1001
smallest element is: 1 second smallest element is: 1029
The smallest element is 1 and second Smallest element is 1003
The smallest element is 1 and second Smallest element is 1004
The smallest element is 1 and second Smallest element is 1005
The smallest element is 1 and second Smallest element is 1006
smallest element : 1 second smallest element : 21
smallest element is: 1 second smallest element is: 1036
The smallest element is 1 and second Smallest element is 1009
smallest element is: 1 second smallest element is: 1035_______26_______98
using
8 The smallest element is 1 and second Smallest element is 1013
smallest element is 1 second smallest element is 920
smallest element is 1 second smallest element is 921
The smallest element is 1 and second Smallest element is 1016
smallest element is 1 second smallest element is 923
smallest element is 1 second smallest element is 924
The smallest element is 1 and second Smallest element is 1019
The smallest element is 1 and second Smallest element is 1020_______1194_______8
The smallest element is 1 and second Smallest element is 1000
The smallest element is 1 and second Smallest element is 1023
smallest element is: 1 second smallest element is: 1029
The smallest element is 1 and second Smallest element is 1003
The smallest element is 1 and second Smallest element is 1004
The smallest element is 1 and second Smallest element is 1005
The smallest element is 1 and second Smallest element is 1006
smallest element : 1 second smallest element : 21
smallest element is: 1 second smallest element is: 1036
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second57
The smallest element is 1 and second Smallest element is 1032
The smallest element is 1 and second Smallest element is 1033
smallest element is: 1 second smallest element is: 1035_______36_______20
using
8 The smallest element is 1 and second Smallest element is 1013
smallest element is 1 second smallest element is 920
smallest element is 1 second smallest element is 921
The smallest element is 1 and second Smallest element is 1040
smallest element is 1 second smallest element is 923
smallest element is 1 second smallest element is 924
The smallest element is 1 and second Smallest element is 1043
The smallest element is 1 and second Smallest element is 1044
C#
The smallest element is 1 and second Smallest element is 1045
smallest element is: 1 second smallest element is: 1003
using
smallest element is 1 second smallest element is 940
The smallest element is 1 and second Smallest element is 106
The smallest element is 1 and second Smallest element is 101
smallest element is 1 second smallest element is 943
smallest element : 1 second smallest element : 20
//C++ simple approach to print smallest
9_______36_______7
The smallest element is 1 and second Smallest element is 106
The smallest element is 1 and second Smallest element is 108
The smallest element is 1 and second Smallest element is 1057
//C++ simple approach to print smallest
9____45_______0
smallest element : 1 second smallest element : 21
smallest element is 1 second smallest element is 92
The smallest element is 1 and second Smallest element is 1062
smallest element : 1 second smallest element : 21
smallest element is 1 second smallest element is 92
smallest element is 1 second smallest element is 959
smallest element : 1 second smallest element : 21
smallest element is 1 second smallest element is 92
The smallest element is 1 and second Smallest element is 1068
smallest element : 1 second smallest element : 21
smallest element is: 1 second smallest element is: 1025
smallest element : 1 second smallest element : 21
smallest element is: 1 second smallest element is: 1027
smallest element : 1 second smallest element : 21
smallest element is: 1 second smallest element is: 1029
smallest element is 1 second smallest element is 921
smallest element is 1 second smallest element is 92
smallest element is: 1 second smallest element is: 1032
smallest element : 1 second smallest element : 21____45_______0
The smallest element is 1 and second Smallest element is 1080
smallest element is: 1 second smallest element is: 1036
smallest element is: 1 second smallest element is: 1037
The smallest element is 1 and second Smallest element is 1080_______45_______0
smallest element is: 1 second smallest element is: 1035_______17_______41
The smallest element is 1 and second Smallest element is 1080
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second4
smallest element : 1 second smallest element : 21____26_______4
smallest element : 1 second smallest element : 21
smallest element is 1 second smallest element is 969
smallest element is: 1 second smallest element is: 1048
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second40
smallest element : 1 second smallest element : 21
smallest element is 1 second smallest element is 92
The smallest element is 1 and second Smallest element is 1097
smallest element : 1 second smallest element : 21_______26_______45
smallest element : 1 second smallest element : 21____26_______47
smallest element : 1 second smallest element : 21
smallest element is: 1 second smallest element is: 1029
smallest element is 1 second smallest element is 921
smallest element is 1 second smallest element is 92
smallest element is: 1 second smallest element is: 1032
smallest element : 1 second smallest element : 21____45_______0
The smallest element is 1 and second Smallest element is 1080
smallest element is: 1 second smallest element is: 1036
smallest element is: 1 second smallest element is: 1064
The smallest element is 1 and second Smallest element is 1080_______45_______0
smallest element is: 1 second smallest element is: 1035_______17_______68
The smallest element is 1 and second Smallest element is 1080
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second4
smallest element : 1 second smallest element : 21____26_______4
smallest element : 1 second smallest element : 21
smallest element is 1 second smallest element is 969
smallest element is: 1 second smallest element is: 1075
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second70
//C++ simple approach to print smallest
9____26_______4
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second4
smallest element is 1 second smallest element is 979
Javascript
smallest element is 1 second smallest element is 980
smallest element : 1 second smallest element : 229
smallest element : 1 second smallest element : 230
smallest element : 1 second smallest element : 231
smallest element : 1 second smallest element : 232
smallest element : 1 second smallest element : 20
//C++ simple approach to print smallest
9____45_______35
smallest element is: 1 second smallest element is: 1035_______45_______37
//C++ simple approach to print smallest
9____45_______39
//C++ simple approach to print smallest
9_______17_______36
smallest element : 1 second smallest element : 242
//C++ simple approach to print smallest
9____45_______0
smallest element : 1 second smallest element : 21
smallest element is 1 second smallest element is 990
smallest element : 1 second smallest element : 247
smallest element is 1 second smallest element is 994
smallest element : 1 second smallest element : 21
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second2
smallest element : 1 second smallest element : 251
//C++ simple approach to print smallest
9____26_______4
smallest element : 1 second smallest element : 254____45_______55
//C++ simple approach to print smallest
9_______17_______29
smallest element : 1 second smallest element : 258
//C++ simple approach to print smallest
9____45_______0
smallest element : 1 second smallest element : 21
smallest element is: 1 second smallest element is: 1036
smallest element : 1 second smallest element : 263
The smallest element is 1 and second Smallest element is 1080_______45_______65
smallest element : 1 second smallest element : 21____26_______4
//C++ simple approach to print smallest
9____26_______4
smallest element : 1 second smallest element : 254____45_______71
smallest element : 1 second smallest element : 254
smallest element is: 1 second smallest element is: 1029
smallest element : 1 second smallest element : 274
smallest element : 1 second smallest element : 21
smallest element is: 1 second smallest element is: 1036
smallest element : 1 second smallest element : 277
The smallest element is 1 and second Smallest element is 1080_______45_______79
smallest element : 1 second smallest element : 21____26_______4
//C++ simple approach to print smallest
9____26_______4
//C++ simple approach to print smallest
9_______17_______36
smallest element : 1 second smallest element : 286
smallest element : 1 second smallest element : 21
smallest element is 1 second smallest element is 990
smallest element : 1 second smallest element : 289
smallest element is 1 second smallest element is 994
//C++ simple approach to print smallest
9____45_______92
smallest element : 1 second smallest element : 21
smallest element is 1 second smallest element is 990
smallest element : 1 second smallest element : 295
smallest element : 1 second smallest element : 296
smallest element : 1 second smallest element : 297
smallest element is 1 second smallest element is 923
The smallest element is 1 and second Smallest element is 1080
//C++ simple approach to print smallest
00 //C++ simple approach to print smallest
01//C++ simple approach to print smallest
02smallest element is 1 second smallest element is 994
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second4
//C++ simple approach to print smallest
9_______1191_______06
//C++ simple approach to print smallest
9
//C++ simple approach to print smallest
9____1191_______09
//C++ simple approach to print smallest
9_______6_______85
//C++ simple approach to print smallest
9_______1191_______13
//C++ simple approach to print smallest
9
smallest element is: 1 second smallest element is: 1001
Đầu ra
smallest element is: 1 second smallest element is: 10
Độ phức tạp về thời gian. TRÊN]
không gian phụ trợ. Ô[1]
Một giải pháp hiệu quả có thể tìm thấy hai yếu tố tối thiểu trong một lần duyệt. Dưới đây là thuật toán hoàn chỉnh
thuật toán.
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second
Below is the implementation of the above approach
C++
smallest element is: 1 second smallest element is: 1002
smallest element : 1 second smallest element : 230
smallest element is: 1 second smallest element is: 1004
using
smallest element is 1 second smallest element is 90
smallest element is 1 second smallest element is 91
//C++ simple approach to print smallest
22
The smallest element is 1 and second Smallest element is 108
//C++ simple approach to print smallest
24smallest element is 1 second smallest element is 92
//C++ simple approach to print smallest
26smallest element is 1 second smallest element is 92
//C++ simple approach to print smallest
28smallest element : 1 second smallest element : 20
smallest element : 1 second smallest element : 21
smallest element is 1 second smallest element is 92
//C++ simple approach to print smallest
32
smallest element : 1 second smallest element : 21
smallest element : 1 second smallest element : 239
smallest element : 1 second smallest element : 21
smallest element is: 1 second smallest element is: 1036
smallest element : 1 second smallest element : 242
smallest element : 1 second smallest element : 21____45_______0
smallest element is: 1 second smallest element is: 1035_______17_______6
smallest element : 1 second smallest element : 247
smallest element : 1 second smallest element : 251
smallest element is: 1 second smallest element is: 1035_______26_______2
smallest element : 1 second smallest element : 251
smallest element : 1 second smallest element : 21____26_______4
smallest element : 1 second smallest element : 21
//C++ simple approach to print smallest
50smallest element : 1 second smallest element : 21____17_______29
//C++ simple approach to print smallest
53smallest element : 1 second smallest element : 21____45_______0
smallest element is: 1 second smallest element is: 1035_______1191_______57
smallest element is: 1 second smallest element is: 1035_______1191_______59
smallest element is: 1 second smallest element is: 1035_______17_______36
//C++ simple approach to print smallest
62smallest element is: 1 second smallest element is: 1035_______45_______0
smallest element is: 1 second smallest element is: 1040
//C++ simple approach to print smallest
66smallest element is: 1 second smallest element is: 1040
smallest element : 1 second smallest element : 265
smallest element is: 1 second smallest element is: 1035_______26_______4
smallest element is: 1 second smallest element is: 1035_______1191_______72
smallest element is: 1 second smallest element is: 1035_______1191_______74
smallest element is: 1 second smallest element is: 1035_______45_______92
smallest element is: 1 second smallest element is: 1036
//C++ simple approach to print smallest
78smallest element is: 1 second smallest element is: 1040
smallest element : 1 second smallest element : 279
smallest element : 1 second smallest element : 21____26_______4
smallest element : 1 second smallest element : 21
smallest element is: 1 second smallest element is: 1036
//C++ simple approach to print smallest
85smallest element is: 1 second smallest element is: 1035_______17_______47____45_______89
smallest element : 1 second smallest element : 251
smallest element : 1 second smallest element : 21____45_______92
smallest element is: 1 second smallest element is: 1035_______17_______47____45_______95
//C++ simple approach to print smallest
95smallest element : 1 second smallest element : 297
smallest element is: 1 second smallest element is: 1040
//C++ simple approach to print smallest
00 //C++ simple approach to print smallest
991] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second4
//and second smallest element.
01
smallest element is 1 second smallest element is 92
smallest element is: 1 second smallest element is: 1009
smallest element : 1 second smallest element : 20
smallest element : 1 second smallest element : 21____6_______2
smallest element is: 1 second smallest element is: 1013
smallest element : 1 second smallest element : 21
smallest element is 1 second smallest element is 92
smallest element is: 1 second smallest element is: 1016
smallest element is 1 second smallest element is 98
smallest element is 1 second smallest element is 99
smallest element is 1 second smallest element is 98
smallest element is: 1 second smallest element is: 101
smallest element : 1 second smallest element : 21
//and second smallest element.
16smallest element : 1 second smallest element : 21____26_______2
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second3
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second4
//and second smallest element.
21
C
//and second smallest element.
22
//and second smallest element.
23
//and second smallest element.
24
The smallest element is 1 and second Smallest element is 108
//C++ simple approach to print smallest
24smallest element is 1 second smallest element is 92
//C++ simple approach to print smallest
26smallest element is 1 second smallest element is 92
//C++ simple approach to print smallest
28smallest element : 1 second smallest element : 20
smallest element : 1 second smallest element : 21
smallest element is 1 second smallest element is 92
//C++ simple approach to print smallest
32
smallest element : 1 second smallest element : 21
smallest element : 1 second smallest element : 239
smallest element : 1 second smallest element : 21
smallest element is: 1 second smallest element is: 1036
smallest element : 1 second smallest element : 242
smallest element : 1 second smallest element : 21____45_______0
smallest element is: 1 second smallest element is: 1035_______1192_______43
smallest element is 1 second smallest element is 921
smallest element : 1 second smallest element : 247
smallest element is 1 second smallest element is 994
smallest element is: 1 second smallest element is: 1035_______26_______2
smallest element : 1 second smallest element : 251
smallest element : 1 second smallest element : 21____26_______4
smallest element : 1 second smallest element : 21
//C++ simple approach to print smallest
50smallest element : 1 second smallest element : 21____17_______29
//C++ simple approach to print smallest
53smallest element : 1 second smallest element : 21____45_______0
smallest element is: 1 second smallest element is: 1035_______1191_______57
//and second smallest element.
61____1191_______59
smallest element is: 1 second smallest element is: 1035_______17_______36
//C++ simple approach to print smallest
62smallest element is: 1 second smallest element is: 1035_______45_______0
smallest element is: 1 second smallest element is: 1040
//C++ simple approach to print smallest
66smallest element is: 1 second smallest element is: 1040
smallest element : 1 second smallest element : 265
smallest element is: 1 second smallest element is: 1035_______26_______4
smallest element is: 1 second smallest element is: 1035_______1191_______72
//and second smallest element.
61____1192_______77
smallest element is: 1 second smallest element is: 1035_______45_______92
smallest element is: 1 second smallest element is: 1036
//C++ simple approach to print smallest
78smallest element is: 1 second smallest element is: 1040
smallest element : 1 second smallest element : 279
smallest element : 1 second smallest element : 21____26_______4
smallest element : 1 second smallest element : 21
smallest element is: 1 second smallest element is: 1036
//C++ simple approach to print smallest
85smallest element is: 1 second smallest element is: 1035_______1192_______43
smallest element is 1 second smallest element is 921
smallest element : 1 second smallest element : 289
smallest element is 1 second smallest element is 994
smallest element : 1 second smallest element : 21____45_______92
smallest element is: 1 second smallest element is: 1035_______1192_______43
smallest element is 1 second smallest element is 921
//and second smallest element.
99#include
00#include
01#include
02
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second4
#include
04
smallest element is 1 second smallest element is 92
smallest element is: 1 second smallest element is: 1009
smallest element : 1 second smallest element : 20
smallest element : 1 second smallest element : 21____6_______2
smallest element is: 1 second smallest element is: 1013
smallest element : 1 second smallest element : 21
smallest element is 1 second smallest element is 92
smallest element is: 1 second smallest element is: 1016
smallest element is 1 second smallest element is 98
smallest element is 1 second smallest element is 99
smallest element is 1 second smallest element is 98
smallest element is: 1 second smallest element is: 101
smallest element : 1 second smallest element : 21
//and second smallest element.
16smallest element : 1 second smallest element : 21____26_______2
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second3
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second4
Java
#include
24
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second7
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second8
The smallest element is 1 and second Smallest element is 101
#include
28smallest element : 1 second smallest element : 20
smallest element : 1 second smallest element : 21
#include
31The smallest element is 1 and second Smallest element is 1080
#include
33smallest element : 1 second smallest element : 21
The smallest element is 1 and second Smallest element is 107
The smallest element is 1 and second Smallest element is 108
//C++ simple approach to print smallest
24smallest element is 1 second smallest element is 92
#include
39smallest element : 1 second smallest element : 21____45_______0
smallest element is: 1 second smallest element is: 1035_______6_______2
#include
44
smallest element is: 1 second smallest element is: 1035_______45_______39
smallest element is: 1 second smallest element is: 1035_______17_______36
#include
49#include
50#include
51smallest element is: 1 second smallest element is: 1035_______45_______0
smallest element is: 1 second smallest element is: 1040
#include
0smallest element : 1 second smallest element : 247
smallest element is 1 second smallest element is 994
smallest element is: 1 second smallest element is: 1040
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second2
smallest element : 1 second smallest element : 251
smallest element is: 1 second smallest element is: 1035_______26_______4
smallest element is: 1 second smallest element is: 1035_______1193_______64
smallest element is: 1 second smallest element is: 1035_______17_______29
smallest element is 1 second smallest element is 921
smallest element is 1 second smallest element is 92
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second24
#include
3#include
71smallest element is: 1 second smallest element is: 1035_______45_______0
smallest element is: 1 second smallest element is: 1040
//C++ simple approach to print smallest
57#include
76//C++ simple approach to print smallest
59
smallest element is: 1 second smallest element is: 1040
smallest element is: 1 second smallest element is: 1036
//C++ simple approach to print smallest
62smallest element is: 1 second smallest element is: 1040
smallest element : 1 second smallest element : 20
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second30____1191_______66
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second30_______45_______65
smallest element is: 1 second smallest element is: 1040
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second4
smallest element is: 1 second smallest element is: 1040
//C++ simple approach to print smallest
72#include
00//and second smallest element.
77
smallest element is: 1 second smallest element is: 1040
smallest element : 1 second smallest element : 292
smallest element is: 1 second smallest element is: 1036
//C++ simple approach to print smallest
781] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second30_______45_______79
smallest element is: 1 second smallest element is: 1035_______26_______4
smallest element is: 1 second smallest element is: 1035_______17_______36
using
03smallest element is: 1 second smallest element is: 1040
#include
0using
06 smallest element is 1 second smallest element is 923
using
08____1194_______09
smallest element is 1 second smallest element is 994
smallest element is: 1 second smallest element is: 1035_______45_______92
smallest element is: 1 second smallest element is: 1040
#include
0smallest element : 1 second smallest element : 295
smallest element is 1 second smallest element is 923
using
08using
18using
19
smallest element is 1 second smallest element is 923
using
08using
22 using
23
smallest element : 1 second smallest element : 21____26_______4
smallest element : 1 second smallest element : 21
using
27smallest element : 1 second smallest element : 21
The smallest element is 1 and second Smallest element is 106
The smallest element is 1 and second Smallest element is 107
The smallest element is 1 and second Smallest element is 108
using
32smallest element : 1 second smallest element : 21____45_______0
smallest element is: 1 second smallest element is: 1035_______6_______2
smallest element is: 1 second smallest element is: 1097
smallest element is: 1 second smallest element is: 1098
smallest element : 1 second smallest element : 25
smallest element : 1 second smallest element : 26
smallest element : 1 second smallest element : 25
//C++ simple approach to print smallest
4smallest element : 1 second smallest element : 25
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second04
smallest element : 1 second smallest element : 25
//C++ simple approach to print smallest
2smallest element : 1 second smallest element : 25
//C++ simple approach to print smallest
4//C++ simple approach to print smallest
5smallest element is: 1 second smallest element is: 1035_______1194_______51
smallest element : 1 second smallest element : 21____26_______4
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second4
using
55
Python3
using
56
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second7
using
58
using
59 using
60
smallest element : 1 second smallest element : 21
using
62smallest element : 1 second smallest element : 21
using
64_______1194_______8 smallest element is 1 second smallest element is 914
smallest element is 1 second smallest element is 915
smallest element : 1 second smallest element : 21
smallest element is: 1 second smallest element is: 1036
using
70#include
50using
72smallest element is: 1 second smallest element is: 1035_______6_______20
smallest element is 1 second smallest element is 921
using
76#include
51smallest element is: 1 second smallest element is: 1035_______26_______2
smallest element : 1 second smallest element : 21
using
81_______1194_______8 using
83using
8 using
85smallest element : 1 second smallest element : 21
smallest element is: 1 second smallest element is: 1029
The smallest element is 1 and second Smallest element is 1003_______36_______04
The smallest element is 1 and second Smallest element is 1005
smallest element is 1 second smallest element is 921
#include
3using
93
smallest element is: 1 second smallest element is: 1035_______1194_______95
smallest element is: 1 second smallest element is: 1035_______1194_______97
smallest element is: 1 second smallest element is: 1035_______17_______36
smallest element is 1 second smallest element is 9000
smallest element is: 1 second smallest element is: 1040
using
83using
8 using
81smallest element is: 1 second smallest element is: 1040
using
81_______1194_______8 The smallest element is 1 and second Smallest element is 1013
smallest element is: 1 second smallest element is: 1035_______6_______010
smallest element is: 1 second smallest element is: 1035_______6_______012
smallest element is: 1 second smallest element is: 1035_______6_______014
smallest element is 1 second smallest element is 9015
The smallest element is 1 and second Smallest element is 1032
smallest element is 1 second smallest element is 9017
using
8 smallest element is 1 second smallest element is 9019
smallest element is: 1 second smallest element is: 1040
using
83using
8 smallest element is 1 second smallest element is 9023
smallest element : 1 second smallest element : 21
smallest element is: 1 second smallest element is: 1036
smallest element is 1 second smallest element is 9026
using
8using
8 smallest element is 1 second smallest element is 9029
smallest element is: 1 second smallest element is: 1035_______6_______20
smallest element is 1 second smallest element is 921
smallest element is 1 second smallest element is 9033
#include
51smallest element : 1 second smallest element : 21
smallest element : 1 second smallest element : 292
using
72smallest element is: 1 second smallest element is: 1035_______6_______20
smallest element is 1 second smallest element is 921_______6_______041
smallest element is 1 second smallest element is 9042
smallest element is 1 second smallest element is 9043_______6_______044
#include
76____6_______046
smallest element is 1 second smallest element is 9047
smallest element is 1 second smallest element is 9048
using
7using
8 using
9
smallest element is: 1 second smallest element is: 1098
smallest element : 1 second smallest element : 25
smallest element : 1 second smallest element : 26
smallest element : 1 second smallest element : 25_______1191_______4
smallest element : 1 second smallest element : 25
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second04
smallest element : 1 second smallest element : 25
//C++ simple approach to print smallest
2smallest element : 1 second smallest element : 25
//C++ simple approach to print smallest
4_______6_______11smallest element is 1 second smallest element is 9064
smallest element is 1 second smallest element is 9065
C#
smallest element is 1 second smallest element is 9066
smallest element is 1 second smallest element is 9067
using
smallest element is 1 second smallest element is 940
The smallest element is 1 and second Smallest element is 101
smallest element is 1 second smallest element is 943
smallest element : 1 second smallest element : 20
smallest element : 1 second smallest element : 21
smallest element : 1 second smallest element : 21____6_______075
smallest element is 1 second smallest element is 9076____6_______077
smallest element : 1 second smallest element : 21
The smallest element is 1 and second Smallest element is 107
The smallest element is 1 and second Smallest element is 108
//C++ simple approach to print smallest
24smallest element is 1 second smallest element is 92
smallest element is 1 second smallest element is 9083
smallest element : 1 second smallest element : 21____45_______0
smallest element is: 1 second smallest element is: 1035_______6_______2
smallest element is 1 second smallest element is 9088
smallest element is: 1 second smallest element is: 1035_______45_______39
smallest element is: 1 second smallest element is: 1035_______17_______36
smallest element : 1 second smallest element : 242
smallest element is: 1 second smallest element is: 1035_______45_______0
smallest element is: 1 second smallest element is: 1040
smallest element is 1 second smallest element is 9097
smallest element : 1 second smallest element : 247
smallest element is 1 second smallest element is 994
smallest element is: 1 second smallest element is: 1040
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second2
smallest element : 1 second smallest element : 251
smallest element is: 1 second smallest element is: 1035_______26_______4
smallest element is: 1 second smallest element is: 1035_______6_______106
smallest element is 1 second smallest element is 92
smallest element is 1 second smallest element is 9108
smallest element is: 1 second smallest element is: 1035
smallest element is: 1 second smallest element is: 1035_______17_______29
smallest element is 1 second smallest element is 921
smallest element is 1 second smallest element is 92
smallest element is 1 second smallest element is 9114
smallest element is: 1 second smallest element is: 1035_______45_______0
smallest element is: 1 second smallest element is: 1040
//C++ simple approach to print smallest
57smallest element is: 1 second smallest element is: 1040
//C++ simple approach to print smallest
59smallest element is: 1 second smallest element is: 1040
smallest element is: 1 second smallest element is: 1036
//C++ simple approach to print smallest
62smallest element is: 1 second smallest element is: 1040
smallest element : 1 second smallest element : 20
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second30____1191_______66
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second30_______45_______65
smallest element is: 1 second smallest element is: 1040
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second4
smallest element is: 1 second smallest element is: 1040
//C++ simple approach to print smallest
72smallest element is: 1 second smallest element is: 1040
//C++ simple approach to print smallest
74smallest element is: 1 second smallest element is: 1040
smallest element : 1 second smallest element : 292
smallest element is: 1 second smallest element is: 1036
//C++ simple approach to print smallest
781] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second30_______45_______79
smallest element is: 1 second smallest element is: 1035_______26_______4
smallest element is: 1 second smallest element is: 1035_______17_______36
smallest element is 1 second smallest element is 9146
smallest element is 1 second smallest element is 92
smallest element is 1 second smallest element is 9148
smallest element is: 1 second smallest element is: 1040
smallest element is 1 second smallest element is 9097
using
06 smallest element is 1 second smallest element is 923
smallest element is 1 second smallest element is 9153
using
09smallest element is 1 second smallest element is 994
smallest element is: 1 second smallest element is: 1035_______45_______92
smallest element is: 1 second smallest element is: 1040
smallest element is 1 second smallest element is 9097
smallest element : 1 second smallest element : 295
smallest element is 1 second smallest element is 923
smallest element is 1 second smallest element is 9153_______1194_______18_______1194_______19
smallest element is 1 second smallest element is 923
smallest element is 1 second smallest element is 9153
using
22 using
23smallest element : 1 second smallest element : 21____26_______4
smallest element : 1 second smallest element : 21
using
27smallest element : 1 second smallest element : 21
The smallest element is 1 and second Smallest element is 106
The smallest element is 1 and second Smallest element is 107
The smallest element is 1 and second Smallest element is 108
smallest element is 1 second smallest element is 951
smallest element : 1 second smallest element : 21____45_______0
smallest element is: 1 second smallest element is: 1035_______6_______2
smallest element is 1 second smallest element is 9182
smallest element is: 1 second smallest element is: 1035_______1194_______51
smallest element : 1 second smallest element : 21____26_______4
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second4
smallest element is 1 second smallest element is 9188
PHP
smallest element is 1 second smallest element is 9189
smallest element is 1 second smallest element is 9190
smallest element : 1 second smallest element : 230
smallest element : 1 second smallest element : 231
//C++ simple approach to print smallest
24smallest element is 1 second smallest element is 9194
smallest element : 1 second smallest element : 25
smallest element is 1 second smallest element is 9196
#include
51smallest element : 1 second smallest element : 20
smallest element : 1 second smallest element : 21_______6_______200
smallest element is 1 second smallest element is 9201
smallest element : 1 second smallest element : 21
smallest element : 1 second smallest element : 21____6_______204
smallest element is 1 second smallest element is 9205
smallest element is 1 second smallest element is 9206
smallest element : 1 second smallest element : 21
smallest element is: 1 second smallest element is: 1036
smallest element is 1 second smallest element is 921
smallest element is 1 second smallest element is 9196
smallest element is 1 second smallest element is 9211
smallest element : 1 second smallest element : 21____45_______0
smallest element is: 1 second smallest element is: 1035_______6_______215_______6_______21
smallest element : 1 second smallest element : 247
smallest element is 1 second smallest element is 994
smallest element is: 1 second smallest element is: 1035_______26_______2
smallest element : 1 second smallest element : 251
smallest element : 1 second smallest element : 21____26_______4
smallest element : 1 second smallest element : 21
smallest element is 1 second smallest element is 9225
using
8smallest element is 1 second smallest element is 9227
using
8smallest element is 1 second smallest element is 9200
smallest element : 1 second smallest element : 251
smallest element : 1 second smallest element : 21
smallest element is: 1 second smallest element is: 1029
smallest element is 1 second smallest element is 921_______6_______234
smallest element is 1 second smallest element is 9235
smallest element is 1 second smallest element is 9234
smallest element is 1 second smallest element is 9237
smallest element is 1 second smallest element is 9196
smallest element : 1 second smallest element : 251
smallest element is 1 second smallest element is 9234
smallest element is 1 second smallest element is 9241
smallest element : 1 second smallest element : 21____45_______0
smallest element is: 1 second smallest element is: 1035
smallest element is: 1 second smallest element is: 1035_______6_______246
//and second smallest element.
61____6_______248
//and second smallest element.
61____6_______250
//and second smallest element.
61____6_______252
smallest element is: 1 second smallest element is: 1035_______17_______36
smallest element is 1 second smallest element is 921
smallest element is 1 second smallest element is 9194
using
9smallest element is 1 second smallest element is 9234
smallest element is 1 second smallest element is 9259
smallest element is 1 second smallest element is 9225
#include
51smallest element is: 1 second smallest element is: 1035_______45_______0
smallest element is: 1 second smallest element is: 1040
smallest element is 1 second smallest element is 9227
using
8smallest element is 1 second smallest element is 9225
smallest element : 1 second smallest element : 251
smallest element is: 1 second smallest element is: 1040
smallest element is 1 second smallest element is 9225
using
8smallest element is 1 second smallest element is 9194
using
9smallest element is 1 second smallest element is 9234
smallest element is 1 second smallest element is 9275
smallest element is: 1 second smallest element is: 1035_______26_______4
smallest element is: 1 second smallest element is: 1035_______6_______279
//and second smallest element.
61____6_______281
//and second smallest element.
61____6_______283
smallest element is: 1 second smallest element is: 1035_______45_______92
smallest element is: 1 second smallest element is: 1036
smallest element is 1 second smallest element is 921
smallest element is 1 second smallest element is 9194
using
9smallest element is 1 second smallest element is 9234
smallest element is 1 second smallest element is 9259
smallest element is 1 second smallest element is 9227
smallest element is 1 second smallest element is 9293
smallest element is 1 second smallest element is 9294
smallest element is 1 second smallest element is 9194
using
9smallest element is 1 second smallest element is 9234
smallest element is 1 second smallest element is 9298
smallest element is 1 second smallest element is 9225
#include
51smallest element is: 1 second smallest element is: 1040
smallest element is 1 second smallest element is 9227
using
8smallest element is 1 second smallest element is 9194
using
9smallest element is 1 second smallest element is 9234
smallest element is 1 second smallest element is 9275
smallest element : 1 second smallest element : 21____26_______4
smallest element : 1 second smallest element : 21
smallest element is: 1 second smallest element is: 1036
smallest element is 1 second smallest element is 921
smallest element is 1 second smallest element is 9227
smallest element is 1 second smallest element is 9314
smallest element is 1 second smallest element is 9200
#include
51smallest element is: 1 second smallest element is: 1035_______6_______215
smallest element is 1 second smallest element is 921
smallest element : 1 second smallest element : 289
smallest element is 1 second smallest element is 994
smallest element : 1 second smallest element : 21____45_______92
smallest element is: 1 second smallest element is: 1035_______6_______215
smallest element : 1 second smallest element : 295
smallest element : 1 second smallest element : 25
smallest element is 1 second smallest element is 9225
smallest element is 1 second smallest element is 9329_______45_______5
smallest element is 1 second smallest element is 9331
smallest element is 1 second smallest element is 9332_______45_______5
smallest element is 1 second smallest element is 9227
smallest element : 1 second smallest element : 251
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second4
The smallest element is 1 and second Smallest element is 105
smallest element is 1 second smallest element is 9194
using
8smallest element is 1 second smallest element is 9340
smallest element is 1 second smallest element is 9341
smallest element is 1 second smallest element is 9342
using
8smallest element is 1 second smallest element is 9344
smallest element is 1 second smallest element is 921
smallest element is 1 second smallest element is 9194
smallest element is 1 second smallest element is 994
//C++ simple approach to print smallest
24
smallest element is 1 second smallest element is 9194
smallest element : 1 second smallest element : 25
smallest element is 1 second smallest element is 9342
#include
51
smallest element is 1 second smallest element is 9353
smallest element is 1 second smallest element is 9354
Javascript
smallest element is 1 second smallest element is 980
smallest element : 1 second smallest element : 229
smallest element : 1 second smallest element : 230
smallest element : 1 second smallest element : 231
smallest element is 1 second smallest element is 9359
smallest element : 1 second smallest element : 20
smallest element : 1 second smallest element : 21____6_______362
smallest element : 1 second smallest element : 21
smallest element : 1 second smallest element : 239
smallest element : 1 second smallest element : 21
smallest element is: 1 second smallest element is: 1036
smallest element : 1 second smallest element : 242
smallest element : 1 second smallest element : 21____45_______0
smallest element is: 1 second smallest element is: 1035_______6_______90
smallest element : 1 second smallest element : 247
smallest element is 1 second smallest element is 994
smallest element is: 1 second smallest element is: 1035_______26_______2
smallest element : 1 second smallest element : 251
smallest element : 1 second smallest element : 21____26_______4
smallest element : 1 second smallest element : 21____6_______380
smallest element : 1 second smallest element : 21____6_______382
smallest element : 1 second smallest element : 21____17_______29
//C++ simple approach to print smallest
53smallest element : 1 second smallest element : 21____45_______0
smallest element is: 1 second smallest element is: 1035_______1191_______57
smallest element is: 1 second smallest element is: 1035_______1191_______59
smallest element is: 1 second smallest element is: 1035_______17_______36
//C++ simple approach to print smallest
62smallest element is: 1 second smallest element is: 1035_______45_______0
smallest element is: 1 second smallest element is: 1040
//C++ simple approach to print smallest
66smallest element is: 1 second smallest element is: 1040
smallest element : 1 second smallest element : 265
smallest element is: 1 second smallest element is: 1035_______26_______4
smallest element is: 1 second smallest element is: 1035_______1191_______72
smallest element is: 1 second smallest element is: 1035_______1191_______74
smallest element is: 1 second smallest element is: 1035_______45_______92
smallest element is: 1 second smallest element is: 1036
//C++ simple approach to print smallest
78smallest element is: 1 second smallest element is: 1040
smallest element : 1 second smallest element : 279
smallest element : 1 second smallest element : 21____26_______4
smallest element : 1 second smallest element : 21
smallest element is: 1 second smallest element is: 1036
smallest element : 1 second smallest element : 286
smallest element is: 1 second smallest element is: 1035_______6_______90
smallest element : 1 second smallest element : 289
smallest element is 1 second smallest element is 994
smallest element : 1 second smallest element : 21____45_______92
smallest element is: 1 second smallest element is: 1035_______6_______90_______45_______95
smallest element : 1 second smallest element : 296
smallest element : 1 second smallest element : 297_______6_______23
smallest element is: 1 second smallest element is: 1040
//C++ simple approach to print smallest
00 //C++ simple approach to print smallest
01//C++ simple approach to print smallest
02smallest element is 1 second smallest element is 994
1] Initialize both first and second smallest as INT_MAX first = second = INT_MAX 2] Loop through all the elements. a] If the current element is smaller than first, then update first and second. b] Else if the current element is smaller than second then update second4
smallest element : 1 second smallest element : 21
//C++ simple approach to print smallest
06smallest element : 1 second smallest element : 21
smallest element : 1 second smallest element : 21
//C++ simple approach to print smallest
09smallest element : 1 second smallest element : 21_______6_______85
smallest element : 1 second smallest element : 21
//and second smallest element.
16smallest element : 1 second smallest element : 21
smallest element is: 1 second smallest element is: 1001
Đầu ra
The smallest element is 1 and second Smallest element is 10
Cách tiếp cận tương tự có thể được sử dụng để tìm các phần tử lớn nhất và lớn thứ hai trong một mảng
Thời gian phức tạp. Trên]
Không gian phụ trợ. Ô[1]
Tiếp cận. Cách tiếp cận log[N] sử dụng cấu trúc dữ liệu Priority_queue. Bạn có thể đọc chi tiết hơn về Hàng đợi ưu tiên tại đây
C++
smallest element is: 1 second smallest element is: 1004
using
smallest element is 1 second smallest element is 90
smallest element is 1 second smallest element is 91
smallest element is 1 second smallest element is 92
smallest element is 1 second smallest element is 93
smallest element : 1 second smallest element : 21_______6_______2
smallest element is 1 second smallest element is 9455
The smallest element is 1 and second Smallest element is 1080
smallest element is 1 second smallest element is 92
smallest element is: 1 second smallest element is: 1016
smallest element is 1 second smallest element is 98
smallest element is: 1 second smallest element is: 1018
smallest element is 1 second smallest element is 98
smallest element is: 1 second smallest element is: 101
The smallest element is 1 and second Smallest element is 1080
smallest element is 1 second smallest element is 9464
smallest element is 1 second smallest element is 92
smallest element is 1 second smallest element is 9466
smallest element is 1 second smallest element is 92
smallest element is 1 second smallest element is 9468
smallest element is 1 second smallest element is 92
smallest element is 1 second smallest element is 9470
smallest element is 1 second smallest element is 9471