+ 2

How can we write an algorithm that finds the maximum of given list without sorting it?

For example I need to write an algorithm find_max(list) which find me a maximum of the list, but not sort it. And can we use a recursion to find k-th maximum element in given list, over again without sorting it? I will be grateful if you give some examples

2nd Dec 2017, 4:32 PM
Денис Лим
4 odpowiedzi
+ 2
Like this, or did I misunderstand the question? https://code.sololearn.com/ca6gxiK1yH70/?ref=app
2nd Dec 2017, 4:55 PM
eRosz
eRosz - avatar
+ 6
#I imagine a for loop, and if something in the list is higher than variable highestThing, it becomes the new highestThing. someList=[1,5,8,9,3,12,42,4] highestThing=0 for thing in someList: if thing > highestThing: highestThing=thing print(highestThing)
2nd Dec 2017, 4:58 PM
Ahri Fox
Ahri Fox - avatar
+ 3
To be honest, whilst using native python functions is just fine, I prefer to do the extra work usually, just to actually comprehend how it works. Basically with C style :P
2nd Dec 2017, 5:01 PM
eRosz
eRosz - avatar
+ 2
can we finish this code in one line to reduce machine cycle something like max(list)
2nd Dec 2017, 4:59 PM
Sachin Lobo Jeppu
Sachin Lobo Jeppu - avatar