Java Quick Sort

Source: Internet
Author: User
Tags benchmark

Fast sequencing is a sort of algorithm developed by Donny Holl. On average, sort n items to 0(n log n) comparisons. In the worst case scenario, a 0(n2) comparison is required, but this is not a common situation. In fact, fast sequencing is usually much faster than the other 0(n log n) algorithms because its internal loop (inner LOOP) can be implemented efficiently on most architectures.

Quick sort uses the Divide-and-conquer (Divide and conquer) strategy to divide a serial (list) into two sub-serial (sub-lists).

Algorithm Steps:

1 Select an element from the series, called the "datum" (pivot),

2 Reorder the columns, where all elements are placed in front of the datum in a smaller position than the base value, and all elements are larger than the base value behind the Datum (the same number can be on either side). After the partition exits, the datum is in the middle of the sequence. This is called partition (partition) Operation.

3 The position of the datum divides the series into the left and right parts, then repeats the left and right parts according to the above steps (recursive invocation).

At the bottom of the recursive scenario, the size of the sequence is 0 or one, which is always sorted. Although it is always recursive, the algorithm always exits, because in each iteration (iteration), it will at least put an element to its last Position.

Quick Sort detailed plots: "sitting on the toilet" algorithm 3: the most commonly used sort--quick sort

Algorithm diagram

Algorithmic performance

Sorting algorithms Average Time complexity Worst-case complexity of time The best time complexity Complexity of space Stability
Quick Sort O (n*logn) O (n2) O (n*logn) O (n*logn) Not stable

Java code

 packagecom.sort;Importjava.util.Random; public classMain6 {Private Static voidSortint[] array,intBeginintEnd) {        if(begin <End) {            intleft =begin; intright =end;  while(left! =right ) {                /*** Select the first element as a benchmark, i.e. array[begin]. Search from the right to the left to find a smaller than the benchmark*/                 while(array[right] > array[begin] && right >left ) { right--; }                /*** Select the first element as a benchmark, i.e. array[begin]. And search from the left to the right to find something bigger than the Benchmark.*/                 while(array[left] < array[begin] && Left <right ) { left++; }                /*** Swap left (larger than the benchmark, should be on the right side of the benchmark) and the corresponding element right (which is smaller than the benchmark, should be on the left side of the benchmark) * This is bigger than the benchmark, and it's smaller than the Benchmark.                 * Here to note, if there is no overlap between the left and right position of the element, if the left and right overlap, then * exchange this position and the position of the benchmark Element. */                if(left <right ) {                    inttemp =array[left]; array[left]=array[right]; array[right]=temp; }            }            /*** when left = = right, indicating that this is close to the end of the loop, when the benchmark and this position exchange, then the benchmark is in the middle of the array * (the lower the lower than the Benchmark)*/            inttemp =array[left]; array[left]=array[begin]; array[begin]=temp; /*** Continue to handle the left*/sort (array, begin, left-1); /*** Continue with the right*/Sort (array, right+ 1, end); }    }    /*** Gets a random array of the specified length*/     public Static int[] Getrandomarray (intN) {int[] array =New int[n]; Random Random=NewRandom ();  for(inti = 0; I < array.length; i++) {array[i]= Random.nextint (500); }        returnarray; }    /*** Print the specified array*/     public Static voidOutputarray (int[] Array) {         for(intI:array) {System.out.print (i+ " "); } System.out.println (""); }     public Static voidmain (string[] Args) {int[] array = Getrandomarray (10);        Outputarray (array); Sort (array,0, array.length-1);    Outputarray (array); }}

Java Quick Sort

Related Article

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.