Java Sorting Algorithm for code-heap sorting
- /**
- * Java sorting algorithm implements code-heap sorting.
- *
- * @ Author old zizhu Java century network (java2000.net)
- *
- */
- Public class test {
- Public static int [] heap = {10, 32, 1, 9, 5, 7, 12, 0, 4, 3}; // preset data array
- Public static void main (string ARGs []) {
- Int I; // The variable of the cyclic count.
- Int Index = heap. length; // data index variable
- System. Out. Print ("Before sorting :");
- For (I = 1; I <index-1; I ++)
- System. Out. printf ("% 3 s", heap [I]);
- System. Out. println ("");
- Heapsort (index-2); // heap sorting
- System. Out. Print ("sorted :");
- For (I = 1; I <index-1; I ++)
- System. Out. printf ("% 3 s", heap [I]);
- System. Out. println ("");
- }
- /**
- * Create a heap
- */
- Public static void createheap (INT root, int index ){
- Int I, j; // cyclic count variable
- Int temp; // temporary variables
- Int finish; // determines whether the heap has been established.
- J = 2 * root; // index of the subnode
- Temp = heap [root]; // store the root value of heap
- Finish = 0; // the preset heap creation has not been completed.
- While (j <= index & finish = 0 ){
- If (j <index) // find the largest subnode
- If (heap [J]
- J ++;
- If (temp> = heap [J])
- Finish = 1; // heap created
- Else {
- Heap [J/2] = heap [J]; // parent node = current node
- J = 2 * J;
- }
- }
- Heap [J/2] = temp; // parent node = root Value
- }
- Public static void heapsort (INT index ){
- Int I, j, temp;
- // Convert binary tree into heap
- For (I = (INDEX/2); I> = 1; I --)
- Createheap (I, index );
- // Start heap sorting
- For (I = index-1; I> = 1; I --){
- Temp = heap [I + 1]; // The root value of heap is exchanged with the last value.
- Heap [I + 1] = heap [1];
- Heap [1] = temp;
- Createheap (1, I); // rebuild the heap for other values
- System. Out. Print ("sorting :");
- For (j = 1; j <= index; j ++)
- System. Out. printf ("% 3 s", heap [J]);
- System. Out. println ("");
- }
- }
- }
/*** Java sorting algorithm implements code-heap sorting. ** @ Author old zizhu Java century network (java2000.net) **/public class test {public static int [] heap = {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 = heap. length; // The data index variable system. out. print ("Before sorting:"); for (I = 1; I <index-1; I ++) system. out. printf ("% 3 s", heap [I]); system. out. println (""); heapsort (index-2); // heap sorting system. out. print ("sorted:"); for (I = 1; I <index-1; I ++) system. out. printf ("% 3 s", heap [I]); system. out. println ("");}/*** create heap */public static void createheap (INT root, int index) {int I, j; // The Int temp variable of the cyclic count; // Save the int finish variable; // determine whether the heap has been established. j = 2 * root; // The index temp = heap [root] of the subnode. // The root value of the temporary Heap Storage is finish = 0; // The default heap creation is not completed while (j <= index & finish = 0) {If (j <index) // find the largest subnode if (heap [J]
Running result
Before sorting: 32 1 9 5 7 12 0 4
Sorting: 12 7 9 5 1 4 0 32
Sorting: 9 7 4 5 1 0 12 32
Sorting: 7 5 4 0 1 9 12 32
Sorting: 5 1 4 0 7 9 12 32
Sorting: 4 1 0 5 7 9 12 32
Sorting: 1 0 4 5 7 9 12 32
Sorting: 0 1 4 5 7 9 12 32
After sorting: 0 1 4 5 7 9 12 32