Implement a Heap ADT using vector.
In the main function, declare an array of N (10 < N < 30) elements and fill it with random numbers between 1 and 100. Then fill the heap with these number and then remove all items by displaying them on-screen (heap sort demonstration)
#include <iostream>
#include <vector>
#include <algorithm>
#include <iostream>
#include <random>
#include <ctime>
using namespace std;
int main()
{
srand(time(0));
const int size = 30;
int A[size];
cout << "Initial array: ";
for (int i = 0; i < size; i++)
{
A[i] = rand() % 100 + 1;
cout << A[i] << " ";
}
cout << endl;
vector<int> V;
for (int i = 0; i < size; i++)
{
V.push_back(A[i]);
}
sort(V.begin(), V.end());
cout << "******Remove elements******" << endl;
while(!V.empty())
{
cout << "Remove element: " << V[V.size() - 1] << endl;
V.pop_back();
}
system("pause");
return 0;
}
Comments
Leave a comment