Quick Sort (Java)

Source: Internet
Author: User

The quick sort is the optimization of the bubbling sort, is a very efficient sort, even the most efficient sort so far, the idea is this: set the array A in the storage of n data elements, low to the lower end of the group, high-end subscript, from the array A to take any element (usually take a[low] As a standard element, the standard element adjusts the position of the other elements in array A, so that the elements that precede the standard elements are smaller than the standard elements, and the standard elements are greater than or equal to the standard elements, thereby decomposing the array into two sub-arrays according to the standard elements. The elements in these two sub-arrays are then recursively ordered in a similar way. The recursive exit condition of the algorithm is Low≥high.

You may not know much about it, but you will know it after you have finished the steps.

The steps are as follows:

1, there is a data a [n],

2, the definition of a low-end subscript lower and a high-end subscript higher;

3, define I, j two variables;

4, set conditions: If the low >= high algorithm end, otherwise proceed the following steps;

5. Take the first number of arrays as standard int standar, j = high, i = Low

6, when I < J, from the array last looking forward, if the condition satisfies I < J (because the value of I and J may have changed in the process of finding), and a[J] >= Standar, j--

Stop when not satisfied, make a[i] = a[j], then the subscript of I move right i++;

7, when I < J, start looking backwards from the array, if the condition satisfies I < J (because the value of I and J may have changed in the process of finding), and a[i] <= Standar, i++

Stop when not satisfied, make a[j] = a[i], and then the subscript of I move left j--;

8, exit the entire loop body, the value of I position is Standar

9. Two sub-arrays of a recursive array

The corresponding code is:

 PackageQuickSort; Public classQuickSort { Public int[] Quicksort (intA[],intLowintHigh ) {        intI, J; if(Low >=High ) {            returnA; } Else {            intStandar =A[low]; I=Low ; J=High ;  while(I <j) { while(I < J && A[j] >=Standar) {J--; }                if(I <j) {A[i]=A[j]; I++; }                                 while(I < J && A[i] <Standar) {i++; }                                if(I <j) {A[j]=A[i]; J--; }} A[i]=Standar; Quicksort (A, low, I-1); Quicksort (A, I+ 1, high); returnA; }    }     Public int[] Sort (intA[],intLowintHigh ) {a=quicksort (A, low, high); returnA; }}

The Test class is:

 PackageTest;Importorg.omg.CORBA.Current;ImportBubblesort.bubblesort;ImportInsertsort.insertsort;ImportQuicksort.quicksort;ImportSelectsort.selectsort; Public classTest { Public Static voidMain (string[] args) {QuickSort QuickSort=NewQuickSort (); int[] Array =Createarray (); LongCt1 =System.currenttimemillis (); int[] arrays = quicksort.sort (array, 0, array.length-1); LongCT2 =System.currenttimemillis ();                display (arrays); System.out.println ("Time Consumed:" + (CT2-ct1)); }     Public Static voidDisplayint[] arrays) {System.out.println ("Sorted Data:");  for(inti = 0; i < arrays.length; i++) {System.out.print (Arrays[i]+ "\ T"); if((i + 1)% 10 = = 0) {System.out.println ();    }} System.out.println (); }     Public Static int[] Createarray () {int[] Array =New int[100000]; System.out.println ("The elements in the array are:");  for(inti = 0; I < 100000; i++) {Array[i]= (int) (Math.random () * 1000); System.out.print (Array[i]+ "\ T"); if((i + 1)% 10 = = 0) {System.out.println ();        }} System.out.println (); returnArray; }}

Complexity of Time:

Calculated: The 10,000-digit sequencing time is 2 MS, and the 100,000-digit sort time is 40ms, which is 300 times faster than the last tested bubble sort of 14000ms.

Quick Sort (Java)

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.