Design and Analysis of Algorithms - Quick Sort

Quicksort is another sort algorithm based on the divide and conquer strategy, the basic idea is the input for the sub-array a [p: r], sorted by the following three steps.

 

Private  static  void , qSort ( int P, int R & lt) 
{ 
    IF (P < R & lt) 
    { 
        int Q = Partition (P, R & lt); 
        , qSort (P, Q -1); // sort the left half 
        qSort (q + 1 , R & lt); // right half segment ordering 
    } 
}

 

 

Private  static  int Partition ( int P, int R & lt) 
{ 
    int I = P;
     int J =. 1 + R & lt ; 
    the Comparable x = A [P];
     // the <exchange element x to the left area
     // will> element x switching to the right region of 
    the while ( to true ) 
    { 
        the while (A [++ I] .compareTo (X) <0 && I < R & lt);
         the while (A [- J] .compareTo (X)> 0 );
         IF (I> = J) BREAK ; 
        MyMath.swap (A, I, J); 
    } 
    A [P] =  A [J];
    A [J]=x;
    return j;
}

 

 

 Here to explain why there is an infinite loop? If you select a [p] as a benchmark is divided, a [p] is a: Why [p r] smallest elements do not fall into an infinite loop?

 

Guess you like

Origin www.cnblogs.com/wkfvawl/p/11517280.html