Pretty confused about this because I've verified correct logical output for small enough testcases (N = 20). I try doing N = 10,000 numbers and the program just hangs and I don't understand why... I've implemented the algorithm as simply as I can.
Also, calling sorted(data) on my N = 10k list seems to work almost instantly. So I'm convinced my algorithm just gets stuck somewhere.
Here is the code:
def QuickSort(array):
qsort(array, 0, len(array))
def qsort(arr, left, right):
if ((right - left) < 2):
return
pivotIndex = choosePivot0(arr, left, right)
newPivotIndex = partition(arr, pivotIndex, left, right)
qsort(arr, 0, newPivotIndex)
qsort(arr, newPivotIndex + 1, right)
def partition(arr, pivotIndex, left, right):
swap(arr, pivotIndex, left)
pivot = arr[left]
i = left + 1
for j in range(left+1, right):
if (arr[j] < pivot):
swap(arr, i, j)
i = i + 1
swap(arr, left, i-1) #put pivot back where it belongs
#cobj.count = cobj.count + (right - left - 1) #add m-1 the #of comparisons
return (i-1) #give back where the pivot resides
def swap(array, i, j):
temp = array[i]
array[i] = array[j]
array[j] = temp
def choosePivot0(array, left, right):
return randint(left, right-1) #random
So I'm pretty lost as to why this could be happening. Thanks for any help.
sys.setrecursionlimit(2**30), it takes about 20~30 seconds to sort[2, 3, 5] * 10000, a 30k data.