Skip to content

Commit f745921

Browse files
committed
Merge branch 'master' of https://github.com/thinker3197/AlgorithmVisualizer into thinker3197-master
2 parents e54ab7c + 4d50aae commit f745921

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed

algorithm/category.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"selection": "Selection Sort",
2020
"bubble": "Bubble Sort",
2121
"quick": "Quicksort",
22-
"merge": "Mergesort"
22+
"merge": "Mergesort",
23+
"heap" : "Heap Sort"
2324
}
2425
},
2526
"etc": {

algorithm/sorting/heap/basic/code.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
tracer._print('Original array = [' + D.join(', ') + ']');
2+
3+
function heapSort(array, size) {
4+
var i, j, temp;
5+
6+
for (i = Math.ceil(size / 2) - 1; i >= 0; i--) {
7+
heapify(array, size, i);
8+
}
9+
10+
for (j = size - 1; j >= 0; j--) {
11+
temp = array[0];
12+
array[0] = array[j];
13+
array[j] = temp;
14+
15+
tracer._select(j);
16+
17+
heapify(array, j, 0);
18+
tracer._print('Swapping elemnts : ' + array[0] + ' & ' + array[j]);
19+
20+
tracer._deselect(j);
21+
}
22+
}
23+
24+
function heapify(array, size, root) {
25+
26+
var largest = root;
27+
var left = 2 * root + 1;
28+
var right = 2 * root + 2;
29+
var temp;
30+
31+
if (left < size && array[left] > array[largest]) {
32+
largest = left;
33+
}
34+
35+
if (right < size && array[right] > array[largest]) {
36+
largest = right;
37+
}
38+
39+
if (largest != root) {
40+
temp = array[root];
41+
array[root] = array[largest];
42+
array[largest] = temp;
43+
44+
tracer._notify(largest, root);
45+
46+
tracer._print('Swapping elemnts : ' + array[root] + ' & ' + array[largest]);
47+
48+
heapify(array, size, largest);
49+
}
50+
}
51+
52+
tracer._sleep(1000);
53+
tracer._pace(800);
54+
55+
heapSort(D, 10);
56+
57+
tracer._print('Final array = [' + D.join(', ') + ']');

algorithm/sorting/heap/basic/data.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var tracer = new Array1DTracer();
2+
var D = Array1D.random(10);
3+
tracer._setData(D);

algorithm/sorting/heap/desc.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"Heap Sort": "Heapsort is a comparison-based sorting algorithm. Heapsort can be thought of as an improved selection sort: like that algorithm, it divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element and moving that to the sorted region. The improvement consists of the use of a heap data structure rather than a linear-time search to find the maximum.",
3+
"Complexity": {
4+
"time": "worst O(n log n), best O(n log n), average O(n log n)",
5+
"space": "worst O(1) auxiliary"
6+
},
7+
"References": [
8+
"<a href='https://en.wikipedia.org/wiki/Heapsort'>Wikipedia</a>"
9+
],
10+
"files": {
11+
"basic": "Basic"
12+
}
13+
}

0 commit comments

Comments
 (0)