Use Java to implement a fast Sorting Algorithm

Source: Internet
Author: User

1. algorithm ideas
Quick sorting is a sort by Division and exchange proposed by C. R. A. Hoare in 1962. It adopts a sub-Governance Policy, usually called divide-and-conquermethod ).

(1) Basic Idea of divide and conquer Law
The basic idea of the division and control law is to break down the original problem into several subproblems with smaller sizes but similar structures as the original problem. Recursively solve these subproblems, and then combine the solutions of these subproblems into the solutions of the original problem.

(2) Basic Idea of fast sorting
Set the unordered area to R [low .. High], and describe the basic idea of fast sorting using the division and control method as follows:
① Decomposition:
In R [low .. select a record in high] as the benchmark to divide the current disordered zoning into two smaller subintervals (left and right) R [low .. pivotpos-1) and R [pivotpos + 1 .. high], and make the keywords of all records in the left subinterval less than or equal to the benchmark record (may be recorded as the benchmark) keyword. key, the key of all records in the subinterval on the right is greater than or equal to limit. the benchmark record is located at the correct position (pivotpos), and does not need to be sorted in the future.
Note:
The key to division is to determine the location of the benchmark record, TPOs. The division result can be simply expressed as (note that partition = R [pivotpos]):
R [low... pivotpos-1]. Keys ≤ r [small TPOs]. Key ≤ r [small TPOs + 1 .. High]. Keys
Here, low ≤ TPOs ≤ High.
② Solution:
Use recursive call to quickly sort the Left and Right subintervals R [low... pivotpos-1] and R [small TPOs + 1 .. High.
③ Combination:
Because when the two recursive calls in the "solving" Step end, the left and right subintervals are ordered. For quick sorting, the "Combination" step does not need to be done, and can be considered as a null operation.

2. Quick Sorting Algorithm quicksort

Void quicksort (seqlist R, int low, int high) {// pair R [low .. high] fast sorting int pivotpos; // the location of the benchmark record after division if (low <pigh) {// The sequence TPOs = partition (R, low, high) must be sorted only when the Interval Length is greater than 1; // for R [low .. high] Do division quicksort (R, low, pivotpos-1); // recursive sorting of the Left range quicksort (R, pivotpos + 1, high ); // recursive sorting of the right range} // quicksort

Note:
To sort the entire file, you only need to call quicksort (R, 1, n) to sort the R [L. N.

 

3. Partition Algorithm
(1) Simple Partitioning Method
① Specific practices
Step 1: (initialize) set two pointers, I and j. Their initial values are the lower and upper bounds of the interval, that is, I = low and I = high; select the first record R [I] (R [low]) in the unordered zone as the benchmark record and save it in the variable comment;
Step 2: Make J scan left from high until 1st keywords are found less than limit. key record R [J], move R [J]) to the position indicated by I, which is equivalent to R [J] and reference R [I] (that is, benchmark) the keyword is exchanged so that the keyword is smaller than the benchmark keyword. the key record is moved to the left of the benchmark. After the switch, the record in R [J] is equivalent to the cursor. Then, the I pointer starts scanning right from the position I + 1, until 1st keywords are found to be greater than limit. key record R [I], moving R [I] to the position indicated by I, which is equivalent to exchanging R [I] and reference R [J], move the record whose keyword is greater than the benchmark keyword to the right of the benchmark, after the exchange, R [I] is equivalent to storing the cursor; then let the pointer J from position J-1 start to left scan, in this way, the scanning direction is changed alternately, and the two ends move closer to the center until I = J, I is the final position of the benchmark worker, where the worker is placed to complete a division.

② Division process
Specific changes during a Division [see animation demonstration]

③ Division algorithm:

Int partition (seqlist R, int I, Int J) {// when calling partition (R, low, high .. high] division, // return the location of the benchmark record recetype Limit = R [I]; // use the 1st records of the interval as the benchmark 'while (I <j) {// scanning from the two ends of the interval to the middle until I = J while (I <J & R [J]. key> = large. key) // locate is equivalent to j -- On position I; // scan right to left to find 1st keywords less than limit. key record R [J] if (I <j) // indicates the keyword of the found R [J] <j. key R [I ++] = R [J]; // equivalent to switching R [I] and R [J], after the switch, the I pointer is added with 1 while (I <J & R [I]. key <= signature. key) // locate is equivalent to I ++ on Position J; // scan left to right to search for 1st keywords greater than limit. key record R [I] if (I <j) // indicates that R [I] is found, so that R [I]. key> keys. key R [j --] = R [I]; // equivalent to switching R [I] and R [J], after switching, the J pointer minus 1} // endwhile R [I] = cursor; // The reference record has been finally positioned return I;} // Partition

 

Java source code:

Public Class Partition {public static void main (string ARGs []) {int [] A = {15, 24, 25, 68, 12, 10, 45, 18, 27}; quicksort (A, 0,. length-1); For (INT I = 0; I <. length; I ++) {system. out. print (A [I] + "") ;}} static int partition (INT [] array, int I, Int J) {int TMP; int occurrence = array [I]; while (I <j) {While (I <J & array [J]> = bytes) -- J; TMP = array [I]; array [I] = array [J]; array [J] = TMP; while (I <J & array [I] <= struct) ++ I; TMP = array [I]; array [I] = array [J]; array [J] = TMP;} array [I] = struct; return I ;} public static void quicksort (INT [] array, int I, Int J) {if (I> = J-1) return; int partition = partition (array, I, j ); quicksort (array, I, delimiter-1); quicksort (array, delimiter + 1, J );}} 

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.