-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap sort.cpp
More file actions
82 lines (82 loc) · 1.11 KB
/
heap sort.cpp
File metadata and controls
82 lines (82 loc) · 1.11 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*second program*/
#include<iostream.h>
#include<dos.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void buildheap(int [],int);
void heapsort(int [],int);
void heapify(int [],int,int);
void main()
{
int a[3000],i,n;
clock_t start,end;
clrscr();
cout<<"enter no. of elements\n";
cin>>n;
cout<<"array before sorting\n";
for(i=0;i<n;i++)
{
a[i]=rand()%100;
cout<<a[i]<<" ";
}
cout<<"\n";
start=clock();
delay(100);
buildheap(a,n);
heapsort(a,n);
end=clock();
cout<<"the sorted array is\n";
for(i=0;i<=n-1;i++)
cout<<a[i]<<" ";
cout<<"\n";
cout<<"the time taken is"<<(end-start)/CLK_TCK;
getch();
}
void buildheap(int a[],int n)
{
int p;
for(p=(n-1)/2;p>=0;p--)
heapify(a,p,n);
}
void heapify(int a[],int p,int n)
{
int item,le,re,de;
item=a[p];
le=2*p+1;
while(le<=n-1)
{
de=le;
re=2*p+2;
if(re<=n-1)
{
if(a[re]>a[de])
{
de=re;
}
}
if(item>a[de])
{
break;
}
else
{
a[p]=a[de];
p=de;
le=2*n+1;
}
}
a[p]=item;
}
void heapsort(int a[],int n)
{
int i,temp;
for(i=n-1;i>=0;i--)
{
temp=a[0];
a[0]=a[i];
a[i]=temp;
buildheap(a,i);
}
}