Java and Java

Source: Internet
Author: User

Java and Java

Principles of fast sorting:

Select a key value as the reference value. All smaller than the reference value are in the left sequence (generally unordered), and all those larger than the reference value are on the right (generally unordered ). Select the first element of the sequence.

Compare from the back to the front, use the benchmark value to compare with the last value. If the swap location is smaller than the benchmark value, if the next one is not compared, it is not exchanged until the first value is smaller than the benchmark value is found. After finding this value, we start to compare it from the past and the future. If there is a larger value than the benchmark value, if the next one is not compared, it is not exchanged until the first value is found greater than the reference value. Until the previous and subsequent index comparison> the index from the back to the back ends the first cycle. At this time, for the reference value, the left and right sides are ordered.

Then compare the sequences on both sides and repeat the above loop.

Code example:

Public class FastSort {public static void main (String [] args) {System. out. println ("quick sequencing test"); int [] a = {121, 212, 665 }; int start = 0; int end =. length-1; sort (a, start, end); for (int I = 0; I <. length; I ++) {System. out. println (a [I]) ;}} public static void sort (int [] a, int low, int high) {int start = low; int end = high; int key = a [low]; while (end> start) {// compare while (end> start & a [end]> = key) // if there is no swap location smaller than the key value, compare the next one until there is a swap location smaller than the key value
// Then compare the end --; if (a [end] <= key) {int temp = a [end]; a [end] = a [start]; a [start] = temp;} // compare the while (end> start & a [start] <= key) from the past to the next. // if there is no greater value than the key value, compare the next one until there is a switch location greater than the key value start ++; if (a [start]> = key) {int temp = a [start]; a [start] = a [end]; a [end] = temp;} // at this time, the first loop comparison ends, and the position of the key value has been determined.
// The value on the left is smaller than the key value, and the value on the right is greater than the key value.
// But the order on both sides may be different. perform the following recursive call} // recursive if (start> low) sort (a, low, start-1 ); // left sequence. The first index location is the key value index-1 if (end

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.