Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <conio.h>
- void main()
- {
- int heap[10], no, i, j, c, root, temp;
- printf("\n Input elements number :");
- scanf("%d", &no);
- printf("\n Input element : ");
- for (i = 0; i < no; i++)
- scanf("%d", &heap[i]);
- for (i = 1; i < no; i++)
- {
- c = i;
- do
- {root = (c - 1) / 2;
- if (heap[root] < heap[c])
- {
- temp = heap[root];
- heap[root] = heap[c];
- heap[c] = temp;
- }
- c = root;
- } while (c != 0);
- }
- printf("Heap Sort (MAX): ");
- for (i = 0; i < no; i++)
- printf("%d\t ", heap[i]);
- for (j = no; j >= 0; j--)
- {
- temp = heap[0];
- heap[0] = heap[j];
- heap[j] = temp;
- root = 0;
- do
- {
- c = 2 * root + 1;
- if ((heap[c] > heap[c + 1]) && c < j - 1)
- c++;
- if (heap[root]>heap[c] && c<j)
- {
- temp = heap[root];
- heap[root] = heap[c];
- heap[c] = temp;
- }
- root = c;
- } while (c < j);
- }
- printf("\n Descending sort : ");
- for (i = 0; i < no; i++)
- printf("\t %d", heap[i]);
- getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement