Skip to content

Instantly share code, notes, and snippets.

@sr6033
Last active August 11, 2018 17:57
Show Gist options
  • Select an option

  • Save sr6033/bade1a135c6152dc737b5bcf478c23ef to your computer and use it in GitHub Desktop.

Select an option

Save sr6033/bade1a135c6152dc737b5bcf478c23ef to your computer and use it in GitHub Desktop.
Heap Sort using C++ STL
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
vector<int> arr(n);
// Generating random numbers in a range of 0-100
for(int i = 0; i < n; i++)
arr[i] = rand()%100+1;
// Unsorted array
for(int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
make_heap(arr.begin(), arr.end()); // Building the heap
sort_heap(arr.begin(), arr.end()); // Heap sort
// Sorted array
for(int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment