Java Sorting Algorithm Implementation Code-select-type sorting
- /**
- * The Java sorting algorithm implements code-select-based 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 ("");
- Selectsort (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 selectsort (INT index ){
- Int I, j, k; // cyclic count variable
- Int minvalue; // minimum variable
- Int indexmin; // minimum index variable
- Int temp; // temporary variables
- For (I = 0; I <index-1; I ++ ){
- Minvalue = 32767; // current minimum value
- Indexmin = 0; // store the index value of the minimum value
- For (j = I; j <index; j ++ ){
- If (A [J] <minvalue) // locate the minimum value
- {
- Minvalue = A [J]; // minimum value for storage
- Indexmin = J;
- }
- Temp = A [I]; // exchanges two values.
- A [I] = A [indexmin];
- A [indexmin] = temp;
- }
- System. Out. Print ("sorting :");
- For (k = 0; k <index; k ++)
- System. Out. printf ("% 3 s", a [k]);
- System. Out. println ("");
- }
- }
- }
/*** Java sorting algorithm implements code-select-based 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 (""); selectsort (index-1); // select the sorting 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 selectsort (INT index) {int I, j, k; // The Int minvalue variable of the cyclic count; // The Int indexmin variable of the minimum value; // minimum index variable int temp; // temporary variable for (I = 0; I <index-1; I ++) {minvalue = 32767; // currently, the minimum value indexmin = 0; // store the index value of the minimum value for (j = I; j <index; j ++) {if (a [J] <minvalue) // find the minimum value {minvalue = A [J]; // store the minimum value indexmin = J;} temp = A [I]; // exchange two values a [I] = A [indexmin]; A [indexmin] = temp;} system. out. print ("in sorting:"); For (k = 0; k <index; k ++) system. out. printf ("% 3 s", a [k]); system. out. println ("");}}}
Running result
Before sorting: 10 32 1 9 5 7 12 0 4
Sorting: 1 32 10 9 5 7 12 0 4
Sorting: 1 5 32 10 9 7 12 0 4
Sorting: 1 5 9 32 10 7 12 0 4
Sorting: 1 5 9 10 32 7 12 0 4
Sorting: 1 5 9 10 32 7 12 0 4
Sorting: 1 5 9 10 32 7 12 0 4
Sorting: 1 5 9 10 32 7 12 0 4
Sorting: 1 5 9 10 32 7 12 0 4
After sorting: 1 5 9 10 32 7 12 0 4