Java quicksort simple description of the algorithm and implementation

Quick Sort Bubble Sort is an improvement of. The basic idea is based on the divide and conquer method: to be sorted in Table L [1 ... n] take any of a pivot element as a reference, to be sorted by the sorting trip table is divided into two independent parts L [1 .. .k-1] and L [k + 1 ... n], such that L [1 ... k-1] is less than all the elements pivot, L [k + 1 ... n] is greater than or equal to all of the elements pivot, the pivot on the final position L (k), this process is called a trip to quick sort. Then the process is repeated recursively each of two sub-tables, until only a portion of each of the inner element up or empty, i.e. all elements in its final position.

First assumed known partitioning algorithm, referred to as a Partition (), returns to the above k, noted L (k) is already in the final position, the table can be divided into first, and then two of the same sort table called operating. Therefore it calls recursively quick sort algorithm to sort the specific program structure as follows:

. 1  public  static  void QUICKSORT ( int [] Array, int Low, int High) {
 2          IF (Low <High) { // recursive out condition 
. 3              int pivotpos = Partition (Array, Low, High);
 . 4              // Partition ( ) is the dividing operation, the table is divided into two sub-tables 
. 5              QUICKSORT (Array, Low, pivotpos -. 1 );
 . 6              QUICKSORT (Array, pivotpos +. 1, High); // sequentially recursively two sub-tables 
7          }
 8      }

From the above code is also not difficult to see the key Quicksort algorithm is division operation, rapid sorting algorithm performance also depends on the quality of the division operation. I thought the operating division of reference textbooks written by Yan Wei Min teacher, always assuming that each first element in the current table as a hub (standard) partitioning of tables, you must move the table is greater than the value of the element to the right hub after the hub is smaller than the value of the element is moved to the left, so that the trip Partition () operation, the table element is divided into two hub values.

// partitioning algorithm (trip sorting process) Version textbooks strict 
    public  static  int Partition ( int [] Array, int Low, int High) {
         int Pivot = Array [Low]; // table disposed in the first element value as the hub, the table divided into 
        the while (Low <High) { // cycle out condition 
            the while (Low <High && Array [High]> = Pivot) { 
                High -; // than the hub element is moved to a small value right 
            } 
            Array [Low] = Array [High];
             the while (Low <High && Array [Low] <= Pivot) { 
                Low ++; //Value larger than the hub element is moved to the left 
            } 
            Array [High] = Array [Low]; 
        } 
        Array [Low] = Pivot; // The final position of the hub element back 
        return Low; // returns the final position of the storage hub 
    }

Try running under:

int[] arr = new int[]{43, 25, 71, -42, 45, -69, 11, 31, 29, -4, 0, -17, 21};

The fast row array

/ * 
    Quicksort array 
* / 
public  class QUICKSORT {
     public  static  void main (String [] args) {
         int [] = ARR new new  int [] {43 is, 25, 71 is, -42, 45, -69,. 11, 31 is , 29, -4, 0, -17, 21 is }; 
        (System.out.println "front sort:" );
         for ( int I = 0; I <arr.length; I ++ ) { 
            of System.out.print (ARR [I] + "" ); 
        } 
        QUICKSORT (ARR, 0, arr.length -. 1 ); 
        System.out.println (); 
        System.out.println ( "sorted:");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + "  ");
        }
    }

    public static void quickSort(int[] array, int low, int high) {
        if (low < high) {
            int pivotpos = partition(array, low, high);
            quickSort(array, low, pivotpos - 1);
            quickSort(array, pivotpos + 1, high);
        }
    }

    //Partitioning algorithm (trip sorting process) Version textbooks strict 
    public  static  int Partition ( int [] Array, int Low, int High) {
         int Pivot = Array [Low]; // table element to the first hub value, the table divided into 
        the while (Low <High) { // cycle out condition 
            the while (Low <High && Array [High]> = Pivot) { 
                High -; // move value smaller than the hub element to the right 
            } 
            Array [Low] = Array [High];
             the while (Low <High && Array [Low] <= Pivot) { 
                Low ++; //Value larger than the hub element is moved to the left 
            } 
            Array [High] = Array [Low]; 
        } 
        Array [Low] = Pivot; // The final position of the hub element back 
        return Low; // returns the final position of the storage hub 
    } 
}

The result is correct

Guess you like

Origin www.cnblogs.com/xianghaoran/p/12238983.html
Recommended