Quicksort (Java Edition)

Source: Internet
Author: User
Tags benchmark

I believe we all know several sorting algorithms, such as bubble sort, choose sort, insert sort and so on, these algorithms are not difficult, their understanding of understanding can grasp, and today we want to talk about is the play is the fast sort.

Quote Daniel's ideas to explain the sorting algorithm. (article link: http://bubkoo.com/2014/01/12/sort-algorithm/quick-sort/#参考文章)

The quick Sort is the Turing Award winner, C. R. A. Hoare, proposed in 1960 as a sort of division exchange. It adopts a strategy of division, which is usually referred to as the Division method (Divide-and-conquermethod).

The basic idea of divide-and- Conquer method is: To decompose the original problem into several sub-problems with smaller size but similar structure to the original problem. Solve these sub-problems recursively, then combine the solutions of these sub-problems into the solution of the original problem.

Using the divide-and-conquer method can be divided into three steps of rapid sequencing:

    1. In the dataset, select an element as the datum (pivot).
    2. All elements that are smaller than the datum are moved to the left of the datum, and all elements that are larger than the datum are moved to the right of the datum. This operation is called a partition (partition) operation, where the base element is positioned after the final sort after the partition operation has ended.
    3. For the two subsets to the left and right of the Datum, repeat the first and second steps until only one element is left in all the subsets.

The following is a brief introduction to the implementation steps:

The above picture is the basic realization of ideas, a little incomplete, said in the fast row of the benchmark elements of the search and positioning, the benchmark elements to be larger than the benchmark elements and smaller than the benchmark elements to separate, and then always recursive, you can achieve the fast row, in this case, the benchmark element is given, but the process of their own implementation, Each time the first element is sorted as a datum element.

Words don't say much, on the code.

1  Public Static intGetmiddle (int[] A,intLowintHigh ) {2         //the index of the element that is smaller than the Datum element3         intStoreindex = 0;4         //default takes the first element as a datum element5         intPivot =A[low];6         //first move the datum element to the end of the array7         intTMP =A[low];8A[low] = a[a.length-1];9A[A.LENGTH-1] =tmp;Ten         //will be smaller than the base element, followed by a larger than the base element. One          for(inti = 0; i < a.length-1; i++) { A             if(A[i] <=pivot) { -                 inttemp =A[i]; -A[i] =A[storeindex]; theA[storeindex] =temp; -storeindex++; -             }             -         } +         //Place the datum element back in the middle position after the filtered element -         inttemp =A[storeindex]; +A[storeindex] = a[a.length-1]; AA[A.LENGTH-1] =temp; at         //System.out.println (Arrays.tostring (a)); -         returnStoreindex; -     } -      -      Public Static voidQuickSort (int[] A,intLowintHigh ) { -         if(Low <High ) {             in             intMiddleindex =Getmiddle (A, low, high); - System.out.println (Arrays.tostring (a)); to             //divided into two groups of fast-running, recursive thinking behind the idea of grouping on more +QuickSort (A, low, middleIndex-1); -QuickSort (A, Middleindex + 1, high); the         } *}

These two functions are the implementation of the ordering, the first function is the positioning of the base position, the second is divided into two groups according to the Datum position recursively, the code contains comments, you can better understand the code according to it.

If anything is wrong, you can talk in the comments section.

Quicksort (Java Edition)

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.