Advertisement
agusilham01

max heap (desc order)

Jul 22nd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. void main()
  4. {
  5. int heap[10], no, i, j, c, root, temp;
  6. printf("\n Input elements number :");
  7. scanf("%d", &no);
  8. printf("\n Input element : ");
  9. for (i = 0; i < no; i++)
  10. scanf("%d", &heap[i]);
  11. for (i = 1; i < no; i++)
  12. {
  13. c = i;
  14. do
  15. {root = (c - 1) / 2;
  16. if (heap[root] < heap[c])
  17. {
  18. temp = heap[root];
  19. heap[root] = heap[c];
  20. heap[c] = temp;
  21. }
  22. c = root;
  23. } while (c != 0);
  24. }
  25. printf("Heap Sort (MAX): ");
  26. for (i = 0; i < no; i++)
  27. printf("%d\t ", heap[i]);
  28. for (j = no; j >= 0; j--)
  29. {
  30. temp = heap[0];
  31. heap[0] = heap[j];
  32. heap[j] = temp;
  33. root = 0;
  34. do
  35. {
  36. c = 2 * root + 1;
  37. if ((heap[c] > heap[c + 1]) && c < j - 1)
  38. c++;
  39. if (heap[root]>heap[c] && c<j)
  40. {
  41. temp = heap[root];
  42. heap[root] = heap[c];
  43. heap[c] = temp;
  44. }
  45. root = c;
  46. } while (c < j);
  47. }
  48. printf("\n Descending sort : ");
  49. for (i = 0; i < no; i++)
  50. printf("\t %d", heap[i]);
  51. getch();
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement