简体   繁体   中英

Python- Getting a quicksort recursion error

I created a quicksort with a test case for the same array to n=2,4,...2^16. My quicksort works (tested with a standard array), and now I'm trying to test for up to 2^16. I am getting the following error:

9 512
Single iteration for time elapsed:  0.0088467 seconds.
10 1024
10 1024
Traceback (most recent call last):
  File "quicksort1.py", line 37, in <module>
    quicksort1(array_to_sort, low, high)
  File "quicksort1.py", line 12, in quicksort1
    quicksort1(array, index + 1, high) 
  File "quicksort1.py", line 12, in quicksort1
    quicksort1(array, index + 1, high) 
  File "quicksort1.py", line 12, in quicksort1
    quicksort1(array, index + 1, high) 
  [Previous line repeated 993 more times]
  File "quicksort1.py", line 10, in quicksort1
    index = partition(array, low, high)    
  File "quicksort1.py", line 21, in partition
    for i in range(low+1, high+1):            
RecursionError: maximum recursion depth exceeded in comparison

As you can see, it runs to the 10th time then crashes. I used import sys // sys.setrecursionlimit(20000) but it did would run to the 15th iteration then crash again. If the limit was too high I would get a Segmentation fault: 11 . Would anyone know how to approach this? It just needs to run recursively without crashing. Thanks (code is below):

#import sys
import random
import time
from random import randint

def quicksort1(array, low, high):           


    if high > low:
        index = partition(array, low, high)    
        quicksort1(array, low, index - 1)      
        quicksort1(array, index + 1, high) 

    #sys.setrecursionlimit(20000)    

def partition(array, low, high):                

    firstitem = array[low]
    j = low

    for i in range(low+1, high+1):            
        if array[i] < firstitem:
            j+=1
            array[j], array[i] = array[i], array[j]
    index = j
    array[low], array[index] = array[index], array[low]     
    return index                               

# testing our program
for k in range(1, 17):
    unsorted_array = [random.randint(0, 2**k) for _ in range(2**k)]
    time_start = time.clock()
    for i in range(10):
        array_to_sort = unsorted_array
        print(k, len(array_to_sort))
        low, mid, high = 0, len(array_to_sort)//2, len(array_to_sort)-1
        quicksort1(array_to_sort, low, high)
    time_elapsed = (time.clock() - time_start)
    print("Single iteration for time elapsed: ", time_elapsed/10, "seconds.")


print("Time elapsed to run the program: ", time_elapsed, "seconds.")  

You're only sorting the list on the first iteration. The remaining 9 iterations are sorting the already sorted array.

Since you are choosing the pivot to be the first item of the partition, you are seeing the worst case recursion for subsequent runs.

You should make a copy of the unsorted list like this:

array_to_sort = unsorted_array[:]

Otherwise you are mutating the unsorted array.

Note: there is still a small chance that you can hit the recursion limit if the random list is extremely unlucky. This is a known issue with naive implementations of quicksort

You can limit stack space to O(log(n)), but the worst case time complexity is still O(n^2), unless you improve the partitioning. Recursion is only used on the smaller partition, while iteration is used on the larger partition.

def quicksort1(array, low, high):           

    while high > low:
        index = partition(array, low, high)
        if index-low < high - index:
            quicksort1(array, low, index - 1)
            low = index+1
        else:
            quicksort1(array, index + 1, high) 
            high - index-1

As already answered, for the sort testing, you need to randomize the array before each sort, or copy an already randomized array to a working array to be sorted.

Even ifound while using it on randomly generated numbers but after n=50000 onwards run time error showing similar message.

Anyone do reply with suitable solution

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM