MCPcopy Index your code
hub / github.com/trekhleb/javascript-algorithms / sort

Method sort

src/algorithms/sorting/heap-sort/HeapSort.js:5–29  ·  view source on GitHub ↗
(originalArray)

Source from the content-addressed store, hash-verified

3
4export default class HeapSort extends Sort {
5 sort(originalArray) {
6 const sortedArray = [];
7 const minHeap = new MinHeap(this.callbacks.compareCallback);
8
9 // Insert all array elements to the heap.
10 originalArray.forEach((element) => {
11 // Call visiting callback.
12 this.callbacks.visitingCallback(element);
13
14 minHeap.add(element);
15 });
16
17 // Now we have min heap with minimal element always on top.
18 // Let's poll that minimal element one by one and thus form the sorted array.
19 while (!minHeap.isEmpty()) {
20 const nextMinElement = minHeap.poll();
21
22 // Call visiting callback.
23 this.callbacks.visitingCallback(nextMinElement);
24
25 sortedArray.push(nextMinElement);
26 }
27
28 return sortedArray;
29 }
30}

Callers

nothing calls this directly

Calls 4

pushMethod · 0.80
addMethod · 0.45
isEmptyMethod · 0.45
pollMethod · 0.45

Tested by

no test coverage detected