-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprg28.cpp
More file actions
61 lines (60 loc) · 873 Bytes
/
Copy pathprg28.cpp
File metadata and controls
61 lines (60 loc) · 873 Bytes
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
//heapsort
#include<iostream>
using namespace std;
class heapsort
{
private: int arr[20],n;
public: void sort();
};
void heapsort::sort()
{
int ivalue,elt,f,s,i;
cout<<"Enter the number of elements: ";
cin>>n;
cout<<"Enter the array elements: \n";
for(i=0;i<n;i++)
cin>>arr[i];
//preprocessing phase
//s=0;
for(i=0;i<n;i++){
s=i;
elt=arr[i];
f=(s-1)/2;
while(s>0 && arr[f]<elt){
arr[s]=arr[f];
s=f;
f=(s-1)/2;
}
arr[s]=elt;
}
for(i=n-1;i>0;i--){
ivalue=arr[i];
f=0;
arr[i]=arr[0];
if(i==1)
s=-1;
else
s=1;
if(i>2 && arr[2]>arr[1])
s=2;
while(s>=0 && arr[s]>ivalue){
arr[f]=arr[s];
f=s;
s=2*f+1;
if(s+1<=i-1 && arr[s]<arr[s+1])
s=s+1;
if(s>i-1)
s=-1;
}
arr[f]=ivalue;
}
cout<<"\nAfter sorting\n";
for(i=0;i<n;i++)
cout<<arr[i]<<"\n";
}
main()
{
heapsort h;
int i;
h.sort();
}