Algorithm (4th edition)-2.3 Quick Sort

Source: Internet
Author: User
Tags comparable shuffle sorts

 Public classQuick { Public Static voidsort (comparable[] a) {stdrandom.shuffle (a); //eliminate dependency on inputSort (A, 0, a.length-1); }    Private Static voidSort (comparable[] a,intLointhi) {        if(Hi <= lo)return; intj = Partition (A, lo, HI);//slicingSort (A, lo, j-1);//A[lo the left half of the section. J-1] SortSort (A, j + 1, HI);//sort the right half A[j+1..hi]    }    Private Static intPartition (comparable[] A,intLointhi) {        //divides the array into A[lo. I-1],a[i],a[i+1..hi]        inti = lo, j = hi + 1;//left and right scan pointersComparable v = A[lo];//slicing elements         while(true) {            //scan left and right, check if scan ends and swap elements             while(Less (a[++i], V))if(i = = hi) Break;  while(Less (v, A[--j]))if(j = lo) Break; if(I >= J) Break;        Exch (A, I, j);    } Exch (A, Lo, j); //put v = a[j] in the correct position        returnJ//A[lo. J-1] <= a[j] <= A[j+1..hi] reached    }    Private Static BooleanLess (comparable V, comparable W) {returnV.compareto (W) < 0; }    Private Static voidExch (comparable[] A,intIintj) {comparable T=A[i]; A[i]=A[j]; A[J]=T; }    Private Static voidShow (comparable[] a) {//to print an array in a single line         for(inti = 0; i < a.length; i++) Stdout.print (A[i]+ " ");    Stdout.println (); }     Public Static Booleanissorted (comparable[] a) {//test array elements for order         for(inti = 1; i < a.length; i++)            if(Less (A[i], a[i-1]))return false; return true; }     Public Static voidMain (string[] args) {//reads strings from standard input, sorts them, and outputsString[] A =in.readstrings ();        Sort (a); assertissorted (a);    Show (a); }}
Quick

Key Benefits:

· Simple to implement

· Suitable for a variety of different input data

· Much faster than other sorting algorithms in general applications

Main disadvantages:

· Very fragile.

2.3.1 Basic algorithm

1. We sort by recursive call slicing .

2. The following points need to be noted:

· In situ segmentation

· Don't cross the border.

· Keep randomness

· Terminating loops

· Processing the value of a split element is duplicated

· Terminating recursion

2.3.2 Performance characteristics

1. The best thing to do in a quick sequence is to just divide the array in half every time.

2. Sort the N-no-repeat array, and the fast sort requires an average of ~2nlnn comparisons (and 1/6 of the exchange).

3. Fast sorting requires approximately N^2/2 comparisons, but random scrambling of arrays can prevent this.

2.3.3 Algorithm Improvement

1. Switch to insert sort

That is, a small array is done by inserting a sort .

2. Three sampling and slicing

3. Entropy-Optimal ordering

Quick sort of three-direction segmentation

 Public classQuick3way { Public Static voidsort (comparable[] a) {stdrandom.shuffle (a); //eliminate dependency on inputSort (A, 0, a.length-1); }    Private Static voidSort (comparable[] a,intLointhi) {        if(Hi <= lo)return; intlt = lo, i = lo + 1, GT =Hi; Comparable v=A[lo];  while(I <=GT) {            intCMP =A[i].compareto (v); if(CMP < 0) Exch (A, lt++, i++); Else if(cmp > 0) Exch (A, I, gt--); Elsei++; }    //now A[lo. Lt-1] < v = a[lt: GT] < A[gt+1..hi] establishedSort (A, lo, lt-1); Sort (A, GT+ 1, HI); }    Private Static BooleanLess (comparable V, comparable W) {returnV.compareto (W) < 0; }    Private Static voidExch (comparable[] A,intIintj) {comparable T=A[i]; A[i]=A[j]; A[J]=T; }    Private Static voidShow (comparable[] a) {//to print an array in a single line         for(inti = 0; i < a.length; i++) Stdout.print (A[i]+ " ");    Stdout.println (); }     Public Static Booleanissorted (comparable[] a) {//test array elements for order         for(inti = 1; i < a.length; i++)            if(Less (A[i], a[i-1]))return false; return true; }     Public Static voidMain (string[] args) {//reads strings from standard input, sorts them, and outputsString[] A =in.readstrings ();        Sort (a); assertissorted (a);    Show (a); }}
Quick3way

· For arrays with a large number of repeating elements, this method is much more efficient than the standard fast ordering.

Algorithm (4th edition)-2.3 Quick Sort

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.