Search code examples
delphiquicksort

QuickSort Sortorder


I'm trying to implement quicksort for my library based on this post Delphi : Sorted List

I am not 100% sure how to implement a sort order ascending/descending into this.

Do I just switch the comperator in if Lo<=Hi then begin and until Lo>Hi;?

I admit I don't quite understand this.


Solution

  • You only need to reverse the comparison in these two lines

        while List[Lo] < Mid do Inc(Lo) ;
        while List[Hi] > Mid do Dec(Hi) ;
    

    So make that

        while List[Lo] > Mid do Inc(Lo) ;
        while List[Hi] < Mid do Dec(Hi) ;