A sort algorithm that realizes the amount of calculation of $ O (n) $ by ** purging (removing) elements that are not in ascending order (descending order)? It seems that it became a hot topic last year, but I didn't meet.
You can write very neatly with Python3.8
or later.
stalin_sort = lambda x:[m:=x[0]] + [m:=i for i in x[1:] if i>=m]
arr = [1, 2, 1, 1, 4, 3, 9]
print(stalin_sort(arr))
#---> [1, 2, 4, 9]
print(len('lambda x:[m:=x[0]] + [m:=i for i in x[1:] if i>=m]'))
#---> 50
It's an implementation that ignores the existence rather than the purge, but it's short and nice
Recommended Posts