diff --git a/algorithm/category.json b/algorithm/category.json index 81b8abb0..3e5eb7e1 100644 --- a/algorithm/category.json +++ b/algorithm/category.json @@ -18,7 +18,8 @@ "insertion": "Insertion Sort", "selection": "Selection Sort", "bubble": "Bubble Sort", - "quick": "Quicksort" + "quick": "Quicksort", + "heap" : "Heap Sort" } }, "etc": { diff --git a/algorithm/sorting/heap/basic/code.js b/algorithm/sorting/heap/basic/code.js new file mode 100644 index 00000000..fbd2e82e --- /dev/null +++ b/algorithm/sorting/heap/basic/code.js @@ -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(', ') + ']'); diff --git a/algorithm/sorting/heap/basic/data.js b/algorithm/sorting/heap/basic/data.js new file mode 100644 index 00000000..313a5a2e --- /dev/null +++ b/algorithm/sorting/heap/basic/data.js @@ -0,0 +1,3 @@ +var tracer = new Array1DTracer(); +var D = Array1D.random(10); +tracer._setData(D); diff --git a/algorithm/sorting/heap/desc.json b/algorithm/sorting/heap/desc.json new file mode 100644 index 00000000..0cbe1f4e --- /dev/null +++ b/algorithm/sorting/heap/desc.json @@ -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": [ + "Wikipedia" + ], + "files": { + "basic": "Basic" + } +} \ No newline at end of file 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