+ 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
4 Respostas
+ 2
Like this, or did I misunderstand the question?
https://code.sololearn.com/ca6gxiK1yH70/?ref=app
+ 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)
+ 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
+ 2
can we finish this code in one line to reduce machine cycle
something like max(list)