Ruby quick sort, not so quick, (ruby)

Status
Not open for further replies.

Orionpaul

Ars Tribunus Militum
2,021
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:
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.
 

Orionpaul

Ars Tribunus Militum
2,021
KISS, I get it, I translated directly from pseudocode, then added the random pivot, and the swap to end of array, however you seem to be implying that it's the passing back and forth the array that may be problematic (I'm making wild assumptions at this point...) I'm at work right now, and can't really implement things ATM, but you think it would be better if I just appended this to the Array class so I'm not passing back and forth, just referencing self instead?

From what I've read on parameter passing, it seems everything is pass by reference, but then again when one makes changes outside the original scope ruby just creates a new variable which is lost outside the scope in which it was created. However, that doesn't appear to matter for the array that's being passed back and forth, as once a cell is changed it's changed... So I'm not entirely sure what is meant there.

I suppose looking at my code, I could reduce the function calls (and thus the stack) by making the pivot function simply a part of the quicksort function itself, and see if I get any improvement that way. Anyways, I have the same issue with a static pivot as I do with random, (i.e. they work but only on arrays > 80,000).
 
Orionpaul":34e0pnp3 said:
however you seem to be implying that it's the passing back and forth the array that may be problematic (I'm making wild assumptions at this point...)

Guestimating is what I'm doing. Reread about parameters, since everyone is wrong about it. Semantically, just pretend Ruby is pass by value, since it seems to clone for scope.
 

andrewme

Senator
7,120
Subscriptor
Orionpaul":qdbertj3 said:
I suppose looking at my code, I could reduce the function calls (and thus the stack) by making the pivot function simply a part of the quicksort function itself, and see if I get any improvement that way.

No, that shouldn't help. The "partition" call isn't part of the recursive path, so eliminating it wouldn't make the call stack any shallower.

quicksort on 80,000 items should require a stack depth of only ~17. What depth is it failing at? Does ruby have a debugger you can use to see a stack trace when the failure occurs? Does ruby have a hard-coded or configurable maximum stack depth?
 

Orionpaul

Ars Tribunus Militum
2,021
*** PROBLEM SOLVED *****


Have trouble understanding the debugging tool (first scripting language... really not used to writing code in notepad (go ahead throw your derision my way, I can take it)), I found some info here: http://stackoverflow.com/questions/3955 ... by-scripts but it doesn't fully make sense... in the meantime, I added a depth value, and report depth before each recursive call, then I put in a message to tell me when I'm leaving a recursion and the size of the array to be passed, and I've found that the arrays only appear to be decreasing by a size of 1 each time, but it should be roughly half the size...

The problem was that the random numbers that I was filling my arrays with were all between 1 and 10, so very quickly my sub arrays had only 1 number in them. Turns out quick sort super duper sucks on large arrays of a relatively limited number of values. I changed my random values to 0 - 10,000, and the problem was resolved, thanks everyone, especially the questions from andrewme, very helpful on getting me to the right place.
 

Orionpaul

Ars Tribunus Militum
2,021
ShuggyCoUk":2121qlrc said:
A salutatory lesson in the difference between average case and worst case in terms of Big O :)
heh, I think point of the assignment was supposed to show how you can avoid worst case with random pivots, since typically worst case on quick sort happens on an already sorted array (assuming pivot point is always set to the last element in any given sub array. Noone ever talked about trying to sort sub-arrays of the same number.
 
Status
Not open for further replies.