Fast sorting Quick Sort

Source: Internet
Author: User
Tags benchmark

Fast sorting (Quick sort) uses a divide-and-conquer approach.
The basic idea is to select a base number to divide the sorted data into two separate parts by a single trip, and one part of the data is smaller than any other part of the data. Then, the two parts of the data are quickly sorted by this method, and the whole sorting process can be carried out recursively so as to achieve the whole data into ordered sequence.

Quick Sort Process:
(1) Select a reference value from the series.
(2) Place all the smaller than the reference value in front of the benchmark, all the larger than the benchmark value is placed behind the benchmark (the same number can be on either side); After the partition exits, the datum is in the middle of the sequence.
(3) Sort the "sub-columns before the Datum" and the "sub-columns after the Datum" recursively.

The following is an example of a sequence a={30,40,60,10,20,50}, which demonstrates its quick ordering process (for example).

Just give the 1th trip to a quick sort of process. In the 1th trip, set x=a[i], i.e. x=30.
(01) Find the number less than X from "right-to-left": Find the number of a[j]=20 that meet the condition, j=4 at this point, and then assign A[j] to A[i], i=0 at this point, then walk from left to right.
(02) from "left-to-right" look for a number greater than x: Find the number of a[i]=40 that satisfies the condition, i=1 at this time, then assign A[i] to A[j], j=4 at this point, and then from right to left.
(03) Find the number less than X from "right-to-left": Find the number of a[j]=10 that meet the condition, j=3 at this point, and then assign A[j] to A[i], i=1 at this point, then walk from left to right.
(04) from "left-to-right" look for a number greater than x: Find the number of a[i]=60 that satisfies the condition, i=2 at this time, then assign A[i] to A[j], j=3 at this point, and then from right to left.
(05) Find fewer than x from "right-to-left": No number is found that satisfies the condition. When I>=j, stop searching and assign X to A[i]. This traversal is over!

In the same way, the subsequence is recursively traversed. Finally get an ordered array!

Java code example:

 PackageCom.sort;/*** Reference:http://www.cnblogs.com/skywang12345/p/3596746.html */ Public classQuickSort {/** Quick Sort * * Parameter description: A--the array to be sorted L--the left edge of the array (for example, sort from the starting position, then L=0) R--The right edge of the array (for example, sort up to the end of the array, r=a.length- 1)*/     Public Static voidQuickSort (int[] A,intLintr) {if(L <r) {intI, J, X; I= L;//left Borderj = r;//Right Borderx= A[i];//initial X-value             while(I <j) { while(I < J && A[j] >x) J--;//find the first number less than x from right to left                if(I <j) A[i++] =A[j];  while(I < J && A[i] <x) I++;//find the first number greater than x from left to right                if(I <j) A[j--] =A[i]; } A[i]=x; QuickSort (A, L, I-1);/*Recursive invocation*/QuickSort (A, I+ 1, R);/*Recursive invocation*/        }    }     Public Static voidMain (string[] args) {inti; intA[] = {30, 40, 60, 10, 20, 50 }; System.out.printf ("Before sort:");  for(i = 0; i < a.length; i++) System.out.printf ("%d", A[i]); System.out.printf ("\ n"); QuickSort (A,0, A.length-1); System.out.printf ("After sort:");  for(i = 0; i < a.length; i++) System.out.printf ("%d", A[i]); System.out.printf ("\ n"); }}

The quick sort adopts the concept of "divide and Conquer, conquer".

Fast sorting 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.