-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathHeapSort.cpp
88 lines (74 loc) · 1.48 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
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
83
84
85
86
87
88
//Heap Sort
//Author: Ravi Prakash (https://github.com/ravi-prakash1907)
#include <iostream.h>
#include <conio.h>
int comparison = 0;
void display(int *a, int size) {
cout<<"{ ";
for(int i=0; i<size; i++ )
cout<<a[i]<<' ';
cout<<"}"<<endl;
}
void swap(int *a, int x, int y){
int temp = a[y];
a[y] = a[x];
a[x] = temp;
}
void maxHeapify(int *a, int index, int heapSize)
{
int left = index*2 + 1;
int right = index*2 + 2;
int largest = index;
if(left < heapSize && a[left] > a[largest]){
largest = left;
comparison+=2;
}
if(right < heapSize && a[right] > a[largest]){
largest = right;
comparison+=2;
}
if(largest != index){
comparison++;
swap(a, largest, index);
maxHeapify(a, largest, heapSize);
}
}
void buildMaxHeap(int *a, int n)
{
for (int i = (n/2) - 1; i >= 0; i--) {
maxHeapify(a, i, n);
comparison++;
}
}
void heapSort(int *a, int size)
{
buildMaxHeap(a, size);
int heapSize = size, i;
for(i=size-1; i>=0; i--) {
swap(a, 0, i);
heapSize--;
comparison++;
maxHeapify(a,0,heapSize);
}
}
void main()
{
clrscr();
int size, i, *arr;
cout<<"\nEnter the size of array (max. 10): ";
cin>>size;
arr = new int[size];
cout<<"\nEnter the array: \n";
for(i=0; i<size; i++)
cin>>arr[i];
clrscr();
cout<<"\n Your array: \n";
display(arr, size);
getch();
clrscr();
heapSort(arr, size);
cout<<"\n\nTotal comperision made: "<<comparison;
cout<<"\n Sorted array: \n";
display(arr, size);
getch();
}