Implementing quicksort algorithm

前端 未结 7 1383
走了就别回头了
走了就别回头了 2021-02-01 19:48

I found quicksort algorithm from this book

\"\"

This is the algorithm

QUICKSORT (A, p, r)
i         


        
7条回答
  •  误落风尘
    2021-02-01 20:16

    You did not properly implement the base case termination, which causes quicksort to never stop recursing into itself with sublists of length 0.

    Change this:

    if (low < high)
        pivot_loc = partition(input, low, high);
    quicksort(input, low, pivot_loc - 1);
    quicksort(input, pivot_loc + 1, high);
    

    to this:

    if (low < high) {
        pivot_loc = partition(input, low, high);
        quicksort(input, low, pivot_loc - 1);
        quicksort(input, pivot_loc + 1, high);
    }
    

提交回复
热议问题