I really don't know if this kind of question is suitable in this website. I've been trying to understand the sorting algorithms nowadays and a new algorithm to sort a list of numbers has come to my mind and I wanted to implement it.
So here's the result:
def sort(obj: list) -> list:
result = []
for _ in range(len(obj)):
_biggest = 0
for i in obj:
_biggest = i if i > _biggest else _biggest
_lowest = _biggest
for i in obj:
_lowest = i if _lowest > i else _lowest
result.append(_lowest)
obj.remove(_lowest)
return result
This algorithm basically gets the biggest value in the given list first, then find the lowest value with the help of biggest value, adds the lowest value found to a new list named result
and finally removes the lowest value from the original list. Loops this until the list is empty.
I googled all the fastest sorting algorithms and this one was faster than many of them. So I decided to find out whether the algorithm I've come up with was already a thing or not, but I couldn't find any algorithm that works like that.
I'm sure I'm not the only one found this way. What's the name of this algorithm? Is there a drawback of this algorithm that makes it not proper?
CodePudding user response:
The algorithm is selection sort. Also, this could be improved by using the MAX_INT for biggest so you can eliminate the first loop to find the biggest value. That would take it from N×2N to N×N complexity.