Skip to content

Add Heap Sort #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion algorithm/category.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"insertion": "Insertion Sort",
"selection": "Selection Sort",
"bubble": "Bubble Sort",
"quick": "Quicksort"
"quick": "Quicksort",
"heap" : "Heap Sort"
}
},
"etc": {
Expand Down
57 changes: 57 additions & 0 deletions algorithm/sorting/heap/basic/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
tracer._print('Original array = [' + D.join(', ') + ']');

function heapSort(array, size) {
var i, j, temp;

for (i = Math.ceil(size / 2) - 1; i >= 0; i--) {
heapify(array, size, i);
}

for (j = size - 1; j >= 0; j--) {
temp = array[0];
array[0] = array[j];
array[j] = temp;

tracer._select(j);

heapify(array, j, 0);
tracer._print('Swapping elemnts : ' + array[0] + ' & ' + array[j]);

tracer._deselect(j);
}
}

function heapify(array, size, root) {

var largest = root;
var left = 2 * root + 1;
var right = 2 * root + 2;
var temp;

if (left < size && array[left] > array[largest]) {
largest = left;
}

if (right < size && array[right] > array[largest]) {
largest = right;
}

if (largest != root) {
temp = array[root];
array[root] = array[largest];
array[largest] = temp;

tracer._notify(largest, root);

tracer._print('Swapping elemnts : ' + array[root] + ' & ' + array[largest]);

heapify(array, size, largest);
}
}

tracer._sleep(1000);
tracer._pace(800);

heapSort(D, 10);

tracer._print('Final array = [' + D.join(', ') + ']');
3 changes: 3 additions & 0 deletions algorithm/sorting/heap/basic/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var tracer = new Array1DTracer();
var D = Array1D.random(10);
tracer._setData(D);
13 changes: 13 additions & 0 deletions algorithm/sorting/heap/desc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"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.",
"Complexity": {
"time": "worst O(n log n), best O(n log n), average O(n log n)",
"space": "worst O(1) auxiliary"
},
"References": [
"<a href='https://en.wikipedia.org/wiki/Heapsort'>Wikipedia</a>"
],
"files": {
"basic": "Basic"
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy