Quick sorting of classic sorting

Source: Internet
Author: User

Quick sorting is the most classic sorting. The idea is to select a benchmark value, usually the first. Then, all values are compared with the reference values, and the positions in the larger values remain unchanged,

The small position is exchanged with the first large position. In this way, the first round of sorting is performed, and the two sides of the benchmark value are then sorted, and so on until there is one left.

The following is the code for the quick release:

Public class quicksort {public void quicksort (int A [], int start, int end) {// compare whether only one value of this array is not sorted quickly if (start <End) {// reference value: int keyword = A [start]; int I = start; For (Int J = I + 1; j <= end; j ++) {// compare with the reference value, the large position remains the same, and the small one exchanges with the first large if (a [J] <keyword) {int temp = A [J]; A [J] = A [I + 1]; A [I + 1] = temp; I ++;} A [start] = A [I]; A [I] = keyword; // and so on, recursively sort all the quicksort (A, start, I-1); quicksort (A, I + 1, end) ;}} public static void main (string [] ARGs) {random = new random (); int [] A = new int [10000]; for (INT I = 0; I <. length; I ++) {A [I] = random. nextint (10000);} Long starttime = system. currenttimemillis (); quicksort = new quicksort (); quicksort. quicksort (A, 0,. length-1); long endtime = system. currenttimemillis (); system. out. println ("program running time:" + (endtime-starttime) + "Ms"); // The sorted sequence system. out. println ("sorted sequence:"); For (INT I = 0; I <. length; I ++) {system. out. println (A [I]) ;}}

The code is very simple.

Quick sorting of classic sorting

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.