A sample program that I wrote using JavaScript to show the concept of Heap Sort Algorithm.
If you have some questions please send me an email at jake.r.pomperada@gmail.com and jakerpomperada@yahoo.com.
My mobile number here in the Philippines is 09173084360.
Program Listing
If you have some questions please send me an email at jake.r.pomperada@gmail.com and jakerpomperada@yahoo.com.
My mobile number here in the Philippines is 09173084360.
Sample Program Output
<html>
<head>
<title> Heap Sort </title>
</head>
<body>
<script>
var values=[];
document.write("<font face='arial' size='4'>HEAP SORT </font>");
document.write("<br><br>");
for (a=0; a<10; a++) {
values.push(Number(prompt("Enter item value at no. " + (a+1))));
}
document.write("<font face='arial' size='4'>Numbers");
document.write(" given by the user </font>");
document.write("<br><br>");
for (a=0;a<10; a++) {
document.write("<font face='arial' size='4'> " +values[a] + "");
}
function swap(data, i, j) {
var tmp = data[i];
data[i] = data[j];
data[j] = tmp;
}
function heap_sort(arr) {
put_array_in_heap_order(arr);
end = arr.length - 1;
while(end > 0) {
swap(arr, 0, end);
sift_element_down_heap(arr, 0, end);
end -= 1
}
}
function put_array_in_heap_order(arr) {
var i;
i = arr.length / 2 - 1;
i = Math.floor(i);
while (i >= 0) {
sift_element_down_heap(arr, i, arr.length);
i -= 1;
}
}
function sift_element_down_heap(heap, i, max) {
var i_big, c1, c2;
while(i < max) {
i_big = i;
c1 = 2*i + 1;
c2 = c1 + 1;
if (c1 < max && heap[c1] > heap[i_big])
i_big = c1;
if (c2 < max && heap[c2] > heap[i_big])
i_big = c2;
if (i_big == i) return;
swap(heap,i, i_big);
i = i_big;
}
}
heap_sort(values);
document.write("<br><br>");
document.write("<font face='arial' size='4'>Sorted List of Numbers </font>");
document.write("<br><br>");
for (a=0; a<10; a++) {
document.write("<font face='arial' size='4'> " + values[a]+"</font>");
}
</script>
</body>
</html>
No comments:
Post a Comment