-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap.html
More file actions
107 lines (86 loc) · 2.71 KB
/
heap.html
File metadata and controls
107 lines (86 loc) · 2.71 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title></title></title>
<style>
body {padding-top:50px ; text-align: center;background-color:#0c7b93; margin-top: auto;}
div {color: white; }
</style>
</head>
<body>
<h1>HEAP SORT</h1>
<button onclick="location.href='heapcode.html'">See Heap Sort C++ Code</button> <br /><br />
<button onclick="location.href='index.html'">Go to Home Page</button> <br /><br />
<input placeholder="ADD ONE NUMBER AT A TIME" type="text" id="text1" size="30"></input>
<input type="button" id="button1" value="ADD" onclick="add_element_to_array(); disp()"></input><br><br>
<input type="button" id="button2" value="SORT" onclick="display_array();"><br /><br />
<button onclick="location.href='heap.html'">RESET</button><br />
<div id="Temp">
</div>
<div id="Result"></div>
<script type="text/javascript">
var x = 0;
var array = Array();
function add_element_to_array()
{
array[x] = parseFloat(document.getElementById("text1").value);
// alert("Element: " + array[x] + " Added at index " + x);
x++;
document.getElementById("text1").value = "";
}
function disp(){
var t="<hr/>";
for(var i=0;i<array.length;i++){
t += "Element " + i + " = " + array[i] + "<br/>";
}
document.getElementById("Temp").innerHTML = t;
}
var array_length;
/* to create MAX array */
function heap_root(input, i) {
var left = 2 * i + 1;
var right = 2 * i + 2;
var max = i;
if (left < array_length && input[left] > input[max]) {
max = left;
}
if (right < array_length && input[right] > input[max]) {
max = right;
}
if (max != i) {
swap(input, i, max);
heap_root(input, max);
}
}
function swap(input, index_A, index_B) {
var temp = input[index_A];
input[index_A] = input[index_B];
input[index_B] = temp;
}
function display_array()
{
var e = "<hr/>";
array_length = array.length;
var start = performance.now();
for (var i = Math.floor(array_length / 2); i >= 0; i -= 1) {
heap_root(array, i);
}
for (i = array.length - 1; i > 0; i--) {
swap(array, 0, i);
array_length--;
heap_root(array, 0);
}
var end = performance.now();
var time = end - start;
e+="SORTED BY HEAP SORT"+"<br />"
for (var y=0; y<array.length; y++)
{
e += "Element " + y + " = " + array[y] + "<br/>";
}
e+="TIME COMPLEXITY = O(Nlog(N))"+"<br/>"+"TIME TAKEN = "+time+"ms<br/>";
document.getElementById("Result").innerHTML = e;
}
</script>
</body>
</html>