Java Sorting Algorithm for code-insert sorting
- /**
- * The Java sorting algorithm implements code-insert 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. Print ("" + A [I] + "");
- System. Out. println ("");
- Insertsort (index-1); // select sorting
- // Sorted result
- System. Out. Print ("sorted :");
- For (I = 0; I <index-1; I ++)
- System. Out. Print ("" + A [I] + "");
- System. Out. println ("");
- }
- Public static void insertsort (INT index ){
- Int I, j, k; // cyclic count variable
- Int insertnode; // The data variable to be inserted.
- For (I = 1; I <index; I ++) // insert values in sequence
- {
- Insertnode = A [I]; // set the value to be inserted
- J = I-1; // the start position of the array to be inserted
- // Find the appropriate Insert Location
- While (j> = 0 & insertnode <A [J]) {
- A [J + 1] = A [J];
- J --;
- }
- A [J + 1] = insertnode; // insert a value
- // Print the current sorting result
- System. Out. Print ("sorting :");
- For (k = 0; k <index; k ++)
- System. Out. Print ("" + A [k] + "");
- System. Out. println ("");
- }
- }
- }
/*** Java sorting algorithm implements code-insert 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. print ("" + A [I] + ""); system. out. println (""); insertsort (index-1); // select the sorting result system. out. print ("sorted:"); for (I = 0; I <index-1; I ++) system. out. print ("" + A [I] + ""); system. out. println ("");} public static void insertsort (INT index) {int I, j, k; // The cyclic count variable int insertnode; // to insert the data variable for (I = 1; I <index; I ++) // Insert the value in sequence {insertnode = A [I]; // set the value J = I-1; // start position of the array to be inserted // find the appropriate insert position while (j> = 0 & insertnode <A [J]) {A [J + 1] = A [J]; j --;} A [J + 1] = insertnode; // Insert the value into the system. // print the current sorting result. out. print ("in sorting:"); For (k = 0; k <index; k ++) system. out. print ("" + A [k] + ""); system. out. println ("");}}}
Running result
Before sorting: 10 32 1 9 5 7 12 0 4
Sorting: 10 32 1 9 5 7 12 0 4
Sorting: 1 10 32 9 5 7 12 0 4
Sorting: 1 9 10 32 5 7 12 0 4
Sorting: 1 5 9 10 32 7 12 0 4
Sorting: 1 5 7 9 10 32 12 0 4
Sorting: 1 5 7 9 10 12 32 0 4
Sorting: 0 1 5 7 9 10 12 32 4
Sorting: 0 1 4 5 7 9 10 12 32
After sorting: 0 1 4 5 7 9 10 12 32