Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- void PrintArray(int* a, int n)
- {
- for (int i = 0; i < n; i++)
- cout << a[i] << " ";
- cout << "\n";
- }
- void TopDown(int* a, int n, int position, int* heap)
- {
- int parent = (position - 1) / 2;
- int x = position + 1;
- heap[position] = a[position];
- while (position > 0 && heap[position] > heap[parent])
- {
- //move parent down
- swap(heap[position], heap[parent]);
- /*if (heap[position] > heap[position-1]) swap(heap[position-1], heap[position]);
- position = parent;
- */
- parent = (position - 1) / 2;
- }
- PrintArray(heap, x);
- }
- void TopDownDemo()
- {
- int a[6] = { 3,1,6,5,2,4 };
- int* heap = (int*)malloc(6* sizeof(int));
- for (int i = 0; i < 6; i++)
- TopDown(a, 6, i, heap);
- }
- int main()
- {
- TopDownDemo();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement