Quicksort, just for fun


  • 0
    M

    Simple quicksort with average case O(n log n) passes.

       void sortColors(int A[], int n) {
            quicksort(A, 0, n-1);  
        }
        
        void quicksort(int A[], int s, int e){
            if(s >= e){
                return;
            }
            
            int idx = s;
            for(int i = s; i <= e-1; ++i){
                if(A[i] < A[e]){
                    swap(A[i], A[idx++]);
                }
            }
            
            swap(A[idx], A[e]);
            quicksort(A, s, idx-1);
            quicksort(A, idx+1, e);
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.