Last active
July 2, 2019 20:54
-
-
Save Xevion/64f8f877fb325b85f9487dd6e474e037 to your computer and use it in GitHub Desktop.
failing quicksort of mine
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
import random | |
import random | |
generate = lambda length=10, min=0, max=100 : [random.randint(min, max) for _ in range(length)] | |
def verify(original): | |
array = list(original) | |
array.sort() | |
return array == original | |
def quicksort(array, recur=0): | |
# Prints out the array's current statet in a readable format | |
def repr(): | |
temp = list(map(str, array)) | |
if lm == rm: | |
temp[lm] = 'LR' + temp[lm] | |
else: | |
temp[lm] = 'L' + temp[lm] | |
temp[rm] = 'R' + temp[rm] | |
temp[pivot] = 'P' + temp[pivot] | |
print('[{}]'.format(', '.join(temp))) | |
def swap(x, y): | |
array[x], array[y] = array[y], array[x] | |
if len(array) <= 1: | |
return array | |
pivot = (len(array) // 2) | |
lm = 0 | |
rm = len(array) - 1 | |
repr() | |
while lm != rm: | |
if array[lm] < array[pivot]: | |
lm += 1 | |
elif array[rm] >= array[pivot]: | |
rm -= 1 | |
else: | |
swap(lm, rm) | |
repr() | |
print('>', end='') | |
swap(lm, pivot) | |
# pivot, lm, rm = lm, pivot, pivot | |
repr() | |
print('\nLEFT {}'.format(recur + 1)) | |
left = quicksort(array[:pivot], recur=recur+1) | |
print('\nRIGHT {}'.format(recur + 1)) | |
right = quicksort(array[pivot+1:], recur=recur+1) | |
return left + [array[pivot]] + right | |
def sorteth(arr): | |
arr.sort() | |
return arr | |
random.seed(2) | |
def compare(real, unreal): | |
temp = [str(num) + '*' if num != real[index] else str(num) for index, num in enumerate(unreal)] | |
return '[{}]'.format(', '.join(temp)) | |
for _ in range(1): | |
original = generate() | |
real = sorteth(list(original)) | |
myquicksort = quicksort(original) | |
if real != myquicksort: | |
print('\nOrig: {}\nReal: {}\nMine: {}'.format(original, real, compare(real, myquicksort))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment