5

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.

1
  • 1
    How long your code took to sort a 10k data? I implemented a much simple qsort, and sys.setrecursionlimit(2**30), it takes about 20~30 seconds to sort [2, 3, 5] * 10000, a 30k data. Commented Jul 28, 2012 at 7:28

3 Answers 3

5

There seems to be a typo in the following line:

qsort(arr, 0, newPivotIndex)

I think it should be like this.

qsort(arr, left, newPivotIndex)

Otherwise the function will work somehow only for some of the input data sets. That is why the algorithm gets stuck.

Sign up to request clarification or add additional context in comments.

1 Comment

You are my favorite person right now. Thanks a bunch, this fixes it.
2

Note: I haven't check your algorithm so there maybe be a problem with it / python may not like it for some reason but: Quick sort will approximately improve sorting time from N^2 to N log(N), but maybe as bad as N^2 depending on input data. With optimal data, N=10,000 compared to N=20, would be a factor of 40,000/26 = 1538 times slower. Perhaps it's just processing?

With worst case data it will be 100,000,000 / 400 = 25,000 times slower. What's your data?

2 Comments

Only once in a blue moon would I expect N^2 running time complexity from QuickSort using a random pivot. Data is just a generic list of the integers 1 to 10000 in unsorted order.
Just asking incase you weren't supplying it with rnd data.
2

Python often hangs for deep recursive functions, sometimes it just terminates the current session (if you are trying it out in IDLE), and starts a fresh session without giving any output at all. Try this: import sys; sys.setrecursionlimit(2**30), this may help, though not always.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.