0

What is the code to write a program in order to find the second least number ?

I want a program to find the 2nd least number of an unsorted array

5th Apr 2017, 2:19 PM
Saif Khalid Mohammed Almulla
4 Respostas
+ 4
Logic : Sort the array in ascending order and the 2nd element is what you want.
5th Apr 2017, 3:21 PM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 3
Krishna's Method works, but it's unnecessarily inefficient if you don't need the sorted list later on. Better just go through the array and keep/update temp values for the smallest and second smallest number you encountered. Runs in linear time compared to the O(n*log(n)) of a sorting algorithm and in practice the difference in speed will be larger, than Landau notation suggests.
5th Apr 2017, 7:52 PM
Tob
Tob - avatar
0
Ok, for School assignment?
5th Apr 2017, 2:26 PM
Danny Persson
Danny Persson - avatar
0
No, its for my own knowledge
5th Apr 2017, 3:11 PM
Saif Khalid Mohammed Almulla