-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheapcode.html
More file actions
63 lines (56 loc) · 1.46 KB
/
heapcode.html
File metadata and controls
63 lines (56 loc) · 1.46 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
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<style>
body {padding-top:50px ; margin-top: auto; margin-left: 30px;background-color:#0c7b93;color: white}
h1 {text-align: center; color: black;}
</style>
</head>
<body>
<h1>HEAP SORT
<div class="">
<button onclick="location.href='index.html'">Go to Home Page</button> <br /><br />
</div></h1>
#include <stdio.h><br /> <br>
using namespace std; <br>
void heapify(int arr[], int n, int i) <br>
{ <br>
int max = i; <br>
int l = 2*i + 1; <br>
int r = 2*i + 2; <br>
if (l < n && arr[l] > arr[max]) <br>
max = l; <br>
if (r < n && arr[r] > arr[max]) <br>
max = r; <br>
if (max != i)
{ <br>
swap(arr[i], arr[max]); <br>
heapify(arr, n, max); <br>
}
} <br>
void heapSort(int arr[], int n)
{ <br>
for (int i = (n / 2) - 1; i >= 0; i--) <br>
heapify(arr, n, i); <br>
for (int i=n-1; i>=0; i--)
{ <br>
swap(arr[0], arr[i]);<br>
heapify(arr, i, 0); <br>
}
} <br>
int main() <br>
{ <br>
int arr[10],n;<br>
cout<<"Enter size of array\n";<br>
cin>>n;<br>
for(int i=0;i<n;i++)<br>
cin>>arr[i];<br>
heapSort(arr, n); <br>
cout << "Sorted array is \n"; <br>
for (int i=0; i<n; ++i) <br>
cout << arr[i] << " "; <br>
} <br>
</body>
</html>