Created
February 1, 2020 18:01
-
-
Save sharpvik/19922e2d313397769f9c5637278c8176 to your computer and use it in GitHub Desktop.
Random sort function implemented in Python3.
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 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