Hill sorting and high-speed sorting

Source: Internet
Author: User
// Sort by hill
In the direct insertion sorting algorithm, a number is inserted each time, so that only one node is added to the ordered sequence, and no help is provided for inserting the next number.
If the number of elements that are separated by a longer distance (called an increment) is used to move the number across multiple elements, a specific comparison may eliminate the exchange of multiple elements.
D. L. Shell implemented this idea in the sorting algorithm named by him in 1959. The number of groups to be sorted by the algorithm is divided into several groups according to an incremental D,
The subscript difference of records in each group D. Sort all elements in each group, and then sort them with a small increment.
When the increment is reduced to 1, the entire number to be sorted is divided into one group and sorted.
Public class testxe {
Public static int [] A = {10, 32, 1, 9, 5, 8, 88, 12, 0, 4, 3, 66}; // default data array
Public static void main (string ARGs []) {
Int I; // The variable of the cyclic count.
Int Index = A. length; // data index variable
For (I = 0; I <index-1; I ++)
System. Out. printf ("% 3 s", );
System. Out. println ("");
Shellsort (index-1); // select sorting
For (I = 0; I <index-1; I ++)
System. Out. printf ("% 3 s", );
System. Out. println ("");
}

Public static void shellsort (INT index ){
Int I, j, k; // cyclic count variable
Int temp; // temporary variables
Boolean change; // whether the data is changed
Int datalength; // Interval Length of the Cut Set
Int pointer; // The processing position.
Datalength = (INT) index/2; // The Interval Length of the initial set.
While (datalength! = 0) // The sequence can still be cut
{
// Process each set
For (j = datalength; j <index; j ++ ){
Change = false;
Temp = A [J]; // Save the value of data [J], which is used for exchanging values
Pointer = J-datalength; // the location where the computation is processed
// Compare and exchange the values in the Set
While (temp <A [pointer] & pointer> = 0 & pointer <= index ){
A [pointer + datalength] = A [pointer];
// Calculate the next location for processing
Pointer = pointer-datalength;
Change = true;
If (pointer <0 | pointer> index)
Break;
}
// Exchange with the final value
A [pointer + datalength] = temp;
If (Change ){
// Print the current sorting result
System. Out. Print ("sorting result :");
For (k = 0; k <index; k ++)
System. Out. printf ("% 3 s", a [k]);
System. Out. println ("");
}
}
Datalength = datalength/2; // calculate the Interval Length of the next cut
}
}

}

--------------------------------------------------------------

By means of sorting, the data to be sorted is cut into two independent parts, and all the data in one part is smaller than all the data in the other part, then, sort the two data parts in a high-speed manner based on this method. The entire sorting process can be recursive to convert the entire data into an ordered sequence. ① Take the first keyword K 1 as the control word, convert [K 1, K 2 ,..., K N] is divided into two subareas, so that all keyword in the left area is less than or equal to K 1, all keyword in the right area is greater than or equal to K 1, and finally the control word occupies the appropriate position in the middle of the two subareas. Data in the subarea is still unordered. ② Take the left area as a whole, and use the ① step for processing, and the right area for the same processing. (That is, recursion) ③ repeats steps ① and ② until the processing in the left area is completed.

Public class quicksort {
/**
* High-speed sorting for integer array o
*/
Public static void quitesort (INT [] O, int low, int hight ){
If (low Int poviteposition = adjust (O, low, hight );
Quitesort (O, low, poviteposition-1 );
Quitesort (O, poviteposition + 1, hight );
}
}

/**
* Adjust
*/
Private Static int adjust (INT [] O, int low, int hight) {// select the value corresponding to the low pivot.
Int destination TE = O [low];
While (low While (hight> low & compare (large te, O [hight]) <= 0) {// if the high position is found to be greater than that of povite, it meets the requirements and continues searching
Hight --;
}
O [low] = O [hight];
While (low Low ++;
}
O [hight] = O [low];
}
O [low] = tE;
Return low;
}

/**
* @ Param num1 subtrahend
* @ Param num2 subtrahend
*/
Private Static int compare (INT num1, int num2 ){
Return num1-num2;
}

Public static void main (string [] ARGs ){
Int [] I = {26, 53, 48, 15, 13, 46, 32, 18, 16 };
Quitesort (I, 0, I. Length-1 );
For (int ii: I ){
System. Out. Print (II + "");
}
}
}

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.