简体   繁体   中英

Quick Sort in Python throwing RecursionError

I've written below code for Quick Sort in Python but getting RecursionError: maximum recursion depth exceeded in comparison . While running another code of same logic it's running fine.

Below is code written by me-:

def partition(Arr,start,end):
    pivot=Arr[end]
    pindex=start

    for i in range(start,end):
        if Arr[i] <= pivot:                                         

            Arr[i],Arr[pindex] = Arr[pindex],Arr[i]                   
            pindex += 1

    #print("pindex",pindex)
    Arr[pindex],Arr[end]=Arr[end],Arr[pindex]                       
    return pindex


def QuickSort(Arr,start,end):

    if(start>=end):
        return Arr


    if (start<end):
        pindex=partition(Arr,start,end)                 


        QuickSort(Arr, start, pindex-1)                 

        QuickSort(Arr, pindex-1, end)                   





Array = [10, 7, 8, 9, 1, 5]
start=0
end=len(Array)-1
sort_Arr=QuickSort(Array,start,end)
print ("Sorted array is: {}",sort_Arr)

and below is the code from GeeksforGeeks which runs fine:

# This function takes last element as pivot, places
# the pivot element at its correct position in sorted
# array, and places all smaller (smaller than pivot)
# to left of pivot and all greater elements to right
# of pivot
def partition(arr,low,high):
    i = ( low-1 )         # index of smaller element
    pivot = arr[high]     # pivot

    for j in range(low , high):

        # If current element is smaller than or
        # equal to pivot
        if   arr[j] <= pivot:

            # increment index of smaller element
            i = i+1
            arr[i],arr[j] = arr[j],arr[i]

    arr[i+1],arr[high] = arr[high],arr[i+1]
    return ( i+1 )

# The main function that implements QuickSort
# arr[] --> Array to be sorted,
# low  --> Starting index,
# high  --> Ending index

# Function to do Quick sort
def quickSort(arr,low,high):
    if low < high:

        # pi is partitioning index, arr[p] is now
        # at right place
        pi = partition(arr,low,high)

        # Separately sort elements before
        # partition and after partition
        quickSort(arr, low, pi-1)
        quickSort(arr, pi+1, high)

Following is the Traceback-:

Traceback (most recent call last):
  File "QuickSort.py", line 55, in <module>
    sort_Arr=QuickSort(Array,start,end)
  File "QuickSort.py", line 46, in QuickSort
    QuickSort(Arr, pindex-1, end)                   #Sorting Array elements after pivot point which are value>pivot
  File "QuickSort.py", line 44, in QuickSort
    QuickSort(Arr, start, pindex-1)                 #Sorting Array elements before pivot point which are value<=pivot
  File "QuickSort.py", line 46, in QuickSort
    QuickSort(Arr, pindex-1, end)                   #Sorting Array elements after pivot point which are value>pivot
  File "QuickSort.py", line 46, in QuickSort
    QuickSort(Arr, pindex-1, end)                   #Sorting Array elements after pivot point which are value>pivot
  File "QuickSort.py", line 46, in QuickSort
    QuickSort(Arr, pindex-1, end)                   #Sorting Array elements after pivot point which are value>pivot
  [Previous line repeated 990 more times]
  File "QuickSort.py", line 41, in QuickSort
    pindex=partition(Arr,start,end)                 #Calculating partition Index point by calling partition Function
  File "QuickSort.py", line 22, in partition
    for i in range(start,end):
RecursionError: maximum recursion depth exceeded in comparison

Can anyone please help me with this? Why is my code not running? Any suggestion are appreciated.

You have chosen to represent a range in the array by start,end where end is the index of the last element. Hence this code

    QuickSort(Arr, start, pindex-1)                 
    QuickSort(Arr, pindex-1, end)

does not divide the array in two disjoint parts -- pindex-1 will be in both parts, and the recursion will not terminate because the size of the second part never goes to one.

A better convention is to have end point to just after the last element. Dijkstra can explain why in more detail.

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