Java Sorting Algorithm for code-heap sorting

Source: Internet
Author: User

Java Sorting Algorithm for code-heap sorting
  1. /**
  2. * Java sorting algorithm implements code-heap sorting.
  3. *
  4. * @ Author old zizhu Java century network (java2000.net)
  5. *
  6. */
  7. Public class test {
  8. Public static int [] heap = {10, 32, 1, 9, 5, 7, 12, 0, 4, 3}; // preset data array
  9. Public static void main (string ARGs []) {
  10. Int I; // The variable of the cyclic count.
  11. Int Index = heap. length; // data index variable
  12. System. Out. Print ("Before sorting :");
  13. For (I = 1; I <index-1; I ++)
  14. System. Out. printf ("% 3 s", heap [I]);
  15. System. Out. println ("");
  16. Heapsort (index-2); // heap sorting
  17. System. Out. Print ("sorted :");
  18. For (I = 1; I <index-1; I ++)
  19. System. Out. printf ("% 3 s", heap [I]);
  20. System. Out. println ("");
  21. }
  22. /**
  23. * Create a heap
  24. */
  25. Public static void createheap (INT root, int index ){
  26. Int I, j; // cyclic count variable
  27. Int temp; // temporary variables
  28. Int finish; // determines whether the heap has been established.
  29. J = 2 * root; // index of the subnode
  30. Temp = heap [root]; // store the root value of heap
  31. Finish = 0; // the preset heap creation has not been completed.
  32. While (j <= index & finish = 0 ){
  33. If (j <index) // find the largest subnode
  34. If (heap [J]
  35. J ++;
  36. If (temp> = heap [J])
  37. Finish = 1; // heap created
  38. Else {
  39. Heap [J/2] = heap [J]; // parent node = current node
  40. J = 2 * J;
  41. }
  42. }
  43. Heap [J/2] = temp; // parent node = root Value
  44. }
  45. Public static void heapsort (INT index ){
  46. Int I, j, temp;
  47. // Convert binary tree into heap
  48. For (I = (INDEX/2); I> = 1; I --)
  49. Createheap (I, index );
  50. // Start heap sorting
  51. For (I = index-1; I> = 1; I --){
  52. Temp = heap [I + 1]; // The root value of heap is exchanged with the last value.
  53. Heap [I + 1] = heap [1];
  54. Heap [1] = temp;
  55. Createheap (1, I); // rebuild the heap for other values
  56. System. Out. Print ("sorting :");
  57. For (j = 1; j <= index; j ++)
  58. System. Out. printf ("% 3 s", heap [J]);
  59. System. Out. println ("");
  60. }
  61. }
  62. }
/*** 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

Related Article

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.