Created
March 25, 2019 01:20
-
-
Save himmelmaus/4a83d2077fc6e55ed288c592593dbfa9 to your computer and use it in GitHub Desktop.
A quick implementation of the thanos sort algorithm, removing half of the array's elements at a time until the array is sorted
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import randint | |
def thanos_sort(array): | |
while not is_sorted(array): | |
target_length = int(len(array)/2) | |
while len(array) > target_length: | |
del array[randint(0, len(array)-1)] | |
return array | |
def is_sorted(array): | |
for i in range(len(array)-1): | |
if array[i] > array[i+1]: | |
return False | |
return True |
In Russia we usually use "stalin sort", it traverses a list and removes all the elements that break the order...
This is O(n^2 log n)
because deleting an arbitrary element from an array is an O(n)
operation.
It can easily be done in O(n log n)
by choosing n/2
random elements and copying them all to a new array at once.
This is
O(n^2 log n)
because deleting an arbitrary element from an array is anO(n)
operation.It can easily be done in
O(n log n)
by choosingn/2
random elements and copying them all to a new array at once.
I think the original would be O(n^2)
. Is that what you meant?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
int(len(array)/2) == len(array) // 2