QuickSort in Python - program freezes for large input sizes?

Pretty vague because I checked the correct inference for reasonably small test sites (N = 20). I'm trying to make N = 10,000 numbers, and the program just freezes, and I don’t understand why ... I implemented the algorithm as simple as possible.

Also, the call sorted(data)on my N = 10k list seems almost instantaneous. Therefore, I am convinced that my algorithm just got 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 pretty much lost why this could happen. Thanks for any help.

+5
source share
3 answers

The following line contains a typo:

qsort(arr, 0, newPivotIndex)

I think it should be so.

qsort(arr, left, newPivotIndex)

- . .

+5

. , /python - , : N ^ 2 N log (N), , N ^ 2 . N = 10000 N = 20 40 000/26 = 1538 . , ?

100 000 000/400 = 25 000 . ?

+2

Python , ( IDLE) - . : import sys; sys.setrecursionlimit(2**30), , .

+2

All Articles