-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap_soort.cpp
49 lines (48 loc) · 850 Bytes
/
heap_soort.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
#include<iostream>
using namespace std;
void swap(int a,int b)
{
int temp=a;
a=b;
b=temp;
}
void heapify(int a[],int n,int i)
{
int root=i;
int lc=2*i+1;
int rc=2*i+2;
if(lc<n && a[lc]>a[root])
root=lc;
if(rc<n && a[rc]>a[root])
root=rc;
if(root!=i)
{
swap(a[i],a[root]);
heapify(a,n,root);
}
}
void heap_sort(int a[],int n)
{
for(int i=n/2-1;i>0;i--)
heapify(a,n,i);
for(int i=n-1;i>0;i--)
{
swap(a[i],a[0]);
heapify(a,i,0);
}
}
int main()
{
int n;
cout<<"enter number of elements : "<<endl;
cin>>n;
int a[n];
cout<<"enter elements : "<<endl;
for(int i=0;i<n;i++)
cin>>a[i];
heap_sort(a,n);
cout<<"sorted elements : "<<endl;
for(int i=0;i<n;i++)
cout<<a[i]<<endl;
return 0;
}