Tìm số nhỏ nhất trong mảng javascript cho vòng lặp
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 Show Đố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 Toán Javascript. max tìm max trong mảnghã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ố Toán Javascript. tối đa. áp dụng tìm phần tử max trong mảnghã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()
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 Javascript vòng lặp tiêu chuẩn tìm phần tử tối đa trong mảngcho 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 Phương thức Javascript reduce() để lấy số mục tối đa ar tối thiểuhã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
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++
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 Java1) 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
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
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
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
Python3
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
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
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
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
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 Javascriptsmallest 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
smallest element is 1 second smallest element is 988
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
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) 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 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
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 Javasmallest 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ăn1) 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
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
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
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
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
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 Javascriptsmallest 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
smallest element is: 1 second smallest element is: 1035_______45_______37
smallest element : 1 second smallest element : 242
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
smallest element : 1 second smallest element : 254____45_______55
smallest element : 1 second smallest element : 258
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
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
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
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
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) 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 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
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
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
C
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
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_______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
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 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
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
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
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
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
smallest element is 1 second smallest element is 923
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
Python3
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
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
smallest element is 1 second smallest element is 9047
smallest element is 1 second smallest element is 9048
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
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 PHPsmallest 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
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
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
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 Javascriptsmallest 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) 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
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 Làm cách nào để tìm số nhỏ nhất trong mảng JavaScript bằng vòng lặp?Sử dụng hai vòng lặp
. Initialize the array arr . Khởi tạo tần số bằng 0. Khởi tạo tối thiểu với phần tử đầu tiên trong mảng. Sử dụng vòng lặp đầu tiên để tìm ra phần tử tối thiểu trong mảng.
Làm cách nào để tìm số tối thiểu từ mảng trong JavaScript?Toán. hàm min() thường lấy tất cả các phần tử trong một mảng và xem xét kỹ lưỡng từng giá trị để lấy giá trị nhỏ nhất.
Làm cách nào để tìm hai số nhỏ nhất trong mảng js?Sắp xếp mảng tăng dần. Sử dụng Array#slice để lấy hai phần tử đầu tiên (phần tử nhỏ nhất). |