Skip to content

Instantly share code, notes, and snippets.

@sharpvik
Created February 1, 2020 18:01
Show Gist options
  • Save sharpvik/19922e2d313397769f9c5637278c8176 to your computer and use it in GitHub Desktop.
Save sharpvik/19922e2d313397769f9c5637278c8176 to your computer and use it in GitHub Desktop.
Random sort function implemented in Python3.
from typing import List
from random import randint
def shuffle(arr: List[int]) -> List[int]:
out: List[int] = []
while len(arr) > 0:
idx: int = randint( 0, len(arr) - 1 )
out.append( arr.pop(idx) )
return out
def sorted(arr: List[int]) -> bool:
m: int = arr[0]
for i in arr:
if i < m:
return False
else:
m = i
return True
def random_sort(arr: List[int]) -> List[int]:
count: int = 0
while not sorted(arr):
arr: List[int] = shuffle(arr)
count += 1
return arr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment