I'm working on an algorithms class which has me write a quick sort with a random pivot, and then time it vs a mergesort and heapsort that was written by the prof. I wrote said program, but it severely under performs, both of them, and when presented with an array of random numbers bigger than 70k, I get stack overflow. I've rewritten it several times, and haven't come up with anything that resolves these issues, and was hoping I could have some more eyes to see what I'm missing that's making it so slow/buggy.
So without further adieu here is my code:
I don't know if it'd be helpfull to post the timing code that I've written, since it's fairly basic, but I will if need be. Thanks in advance.
So without further adieu here is my code:
Code:
def quicksort(a, low, high)
if(low < high)
q = partition(a, low, high)
quicksort(a,low, q-1)
quicksort(a,q+1, high)
end # if
a
end #def
def swap(a, low, high)
temp = a[low]
a[low] = a[high]
a[high] = temp
end #def
def partition(a, low, high)
pivot = rand(low..high)
# swap(a, high, pivot)# put the pivot at the end point
a[high],a[pivot]=a[pivot],a[high]
pvalue = a[high]
i = low -1
low.upto(high-1){ |j|
if(a[j] <= pvalue)
i += 1
# swap(a, i, j)
a[i],a[j]=a[j],a[i]
end #if
}
# swap(a, i+1, high)
a[i+1],a[high]=a[high],a[i+1]
i+1
end #def
I don't know if it'd be helpfull to post the timing code that I've written, since it's fairly basic, but I will if need be. Thanks in advance.