forked from coder2hacker/Explore-open-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HeapSort.cpp
53 lines (43 loc) · 1.13 KB
/
HeapSort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<bits/stdc++.h>
using namespace std;
void heapify(int arr[], int n,int i) // for maxheap
{
int largest = i;
int left=2*i;
int right=2*i+1;
if(left<=n && arr[largest]<arr[left])
largest=left;
if(right<=n && arr[largest]<arr[right])
largest=right;
if (largest!=i)
{
swap(arr[i],arr[largest]);
i=largest;
}
else
return;
}
void heapSort(int arr[], int n,int i)
{
if(n==1)
return;
// Step 1: heapify the remaining elements(from n/2 to 1)
for (int i = n/2; i >= 1; i--)
heapify(arr,n,i);
// Step 2:swap first(root) and last element as root element is always maximum(already sorted) in maxheap
swap(arr[i],arr[n]);
// Step 3:As first(root) element is already sorted, so decreasing Size after swapping
n--;
// Step 4:Recursive call for sorting remaining elements
heapSort(arr,n,i);
}
int main()
{
int arr[]={-1,2,6,8,3,7};
int n=5;
heapSort(arr,n,1);
for (int i = 1; i <= n; i++)
cout<<arr[i]<<" ";
cout<<endl;
return 0;
}