Java Sorting Algorithm Implementation Code-shell sorting
- /**
- * The Java sorting algorithm implements code-shell sorting.
- *
- * @ Author old zizhu Java century network (java2000.net)
- *
- */
- Public class test {
- Public static int [] A = {10, 32, 1, 9, 5, 7, 12, 0, 4, 3}; // default data array
- Public static void main (string ARGs []) {
- Int I; // The variable of the cyclic count.
- Int Index = A. length; // data index variable
- System. Out. Print ("Before sorting :");
- For (I = 0; I <index-1; I ++)
- System. Out. printf ("% 3 s", a [I]);
- System. Out. println ("");
- Shellsort (index-1); // select sorting
- // Sorted result
- System. Out. Print ("sorted :");
- For (I = 0; I <index-1; I ++)
- System. Out. printf ("% 3 s", a [I]);
- 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 split 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 split
- {
- // 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 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 :");
- 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 split
- }
- }
- }
/*** Java sorting algorithm implements code-shell sorting. ** @ Author old zizhu Java century network (java2000.net) **/public class test {public static int [] A = {10, 32, 1, 9, 5, 7, 12, 0, 4, 3}; // default data array public static void main (string ARGs []) {int I; // The cyclic count variable int Index =. length; // The data index variable system. out. print ("Before sorting:"); for (I = 0; I <index-1; I ++) system. out. printf ("% 3 s", a [I]); system. out. println (""); shellsort (index-1); // select the sorting result system. out. print ("after sorting: "); For (I = 0; I <index-1; I ++) system. out. printf ("% 3 s", a [I]); system. out. println ("");} public static void shellsort (INT index) {int I, j, k; // The cyclic count variable int temp; // The temporary variable Boolean change; // whether the data changes int datalength; // whether the Interval Length of the split set is int pointer; // The processing position datalength = (INT) index/2; // The Interval Length of the initial set while (datalength! = 0) // The sequence can still be split {// process each set for (j = datalength; j <index; j ++) {change = false; temp = A [J]; // Save the value of data [J]. Use Pointer = J-datalength to exchange the value; // calculate the processing position // compare the values in the set with the exchange value while (temp <A [pointer] & pointer> = 0 & pointer <= index) {A [pointer + datalength] = A [pointer]; // calculate the next vertex to be processed. pointer = pointer-datalength; change = true; if (pointer <0 | pointer> index) break;} // exchange a [pointer + datalength] = temp with the final value; If (change) {// print the current sorting result system. out. print ("in sorting:"); 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 split }}}
Running result
Before sorting: 10 32 1 9 5 7 12 0 4
Sorting: 5 32 1 9 10 7 12 0 4
Sorting: 5 7 1 9 10 32 12 0 4
Sorting: 5 7 1 0 10 32 12 9 4
Sorting: 4 7 1 0 5 32 12 9 10
Sorting: 1 7 4 0 5 32 12 9 10
Sorting: 1 0 4 7 5 32 12 9 10
Sorting: 1 0 4 7 5 9 12 32 10
Sorting: 1 0 4 7 5 9 10 32 12
Sorting: 0 1 4 7 5 9 10 32 12
Sorting: 0 1 4 5 7 9 10 32 12
Sorting: 0 1 4 5 7 9 10 12 32
After sorting: 0 1 4 5 7 9 10 12 32