diff --git a/_heap.js b/_heap.js index b9e9536..c7e70b6 100644 --- a/_heap.js +++ b/_heap.js @@ -1,17 +1,88 @@ -class MinHeap { - constructor() { - this.heap = []; +// // Original Array: [4, 10, 3, 5, 1] +// // 4 +// // / \ +// // 10 3 +// // / \ +// // 5 1 + +// // running [10, 4, 3, 5, 1] +// // M r +// // 10 +// // / \ +// // 4 3 +// // / \ +// // 5 1 + +// // Max Heap: +// // 10 +// // / \ +// // 5 4 +// // / \ +// // 3 1 + +function maxHeapify(arr, n, i) { + let largest = i; + const left = 2 * i + 1; + const right = 2 * i + 2; + + if (left < n && arr[left] > arr[largest]) { + largest = left; + } + + if (right < n && arr[right] > arr[largest]) { + largest = right; + } + + if (largest !== i) { + // Swap arr[i] and arr[largest] + [arr[i], arr[largest]] = [arr[largest], arr[i]]; + + // Recursively heapify the affected sub-tree + maxHeapify(arr, n, largest); } +} - insert(num) { - this.heap.push(num); - this.heapifyUp(); -// }does (3.) mean sorting an exist collection either descending or ascending? what does 'collection' means here? please give a simplest example for (3.) +function buildMaxHeap(arr) { + const n = arr.length; - heapifyUp() { - let index = this.heap.length - 1; - while (index > 0) { - let parentIndex = Math.floor((index - 1) / 2); - } + // Build a max heap + // Math.floor(n / 2) - 1: + // start the heapify process from the last non-leaf node in the array. + // This speeds up the algorithm because leaf nodes, + // which make up the bottom half of the heap, + // do not need to be heapified since they have no children. + for (let i = Math.floor(n / 2) - 1; i >= 0; i--) { + maxHeapify(arr, n, i); } } + +function heapSort(arr) { + const n = arr.length; + + // Build a max heap + buildMaxHeap(arr); + + // Extract elements one by one from the heap + for (let i = n - 1; i > 0; i--) { + // Move current root to the end + [arr[0], arr[i]] = [arr[i], arr[0]]; + + // Call maxHeapify on the reduced heap + maxHeapify(arr, i, 0); + } + return arr; +} + +// const a = [null, 0, 5, 20, 6, 12, 65, 1, 4, 9, 3, 89, 22, 25, 28, 10]; +const a = [0, 5, 20, 6, 12, 65, 1, 4, 9, 3, 89, 22, 25, 28, 10]; + +console.log(heapSort(a)); +heapSort([12, 11, 13, 5, 6, 7]); +// 89 +// / \ +// 12 65 +// / \ / \ +// 9 5 25 28 +// / \ / \ / \ / \ +// 4 6 3 0 22 20 1 10 +// diff --git a/_heapMax-231217.js b/_heapMax-231217.js new file mode 100644 index 0000000..e02b117 --- /dev/null +++ b/_heapMax-231217.js @@ -0,0 +1,72 @@ +// index: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]; +const a = [0, 5, 20, 6, 12, 65, 1, 4, 9, 3, 89, 22, 25, 28, 10]; +// heapSort([12, 11, 13, 5, 6, 7]); + +// s 0 +// / \ +// 5 20 +// / \ / \ +// 6 12 65 1 +// / \ / \ / \ / \ +// 4 9 3 89 22 25 28 10 + +// current: i +// left = i * 2 + 1 +// right = i * 2 + 2 +// parent = Math.floor((i - 1) / 2) +// leaves = Math.floor(n / 2) to (n - 1) + +const maxHeapify = (arr, n, i) => { + let left = i * 2 + 1; + let right = i * 2 + 2; + let largest = i; + + if (left < n && arr[largest] < arr[left]) { + largest = left; + } + + if (right < n && arr[largest] < arr[right]) { + largest = right; + } + + // heapSort([12, 11, 13, 5, 6, 7]); + // i + // s 12 + // l / \ r L + // 11 13 + // / \ / + // 5 6 7 + + if (largest !== i) { + [arr[i], arr[largest]] = [arr[largest], arr[i]]; + // i + // s 13 + // l / \ r L + // 11 12 + // / \ / + // 5 6 7 + maxHeapify(arr, n, largest); + } +}; + +const buildMaxHeap = (arr) => { + const n = arr.length; + const leaves = Math.floor(n / 2); + for (let i = leaves - 1; i >= 0; i--) { + maxHeapify(arr, n, i); + } +}; + +const heapSort = (arr) => { + const n = arr.length; + buildMaxHeap(arr); + + for (let i = n - 1; i > 0; i--) { + [arr[0], arr[i]] = [arr[i], arr[0]]; + maxHeapify(arr, i, 0); + } + + return arr; +}; + +console.log(heapSort(a)); diff --git a/_heapMax-xx.js b/_heapMax-xx.js new file mode 100644 index 0000000..9bbdacf --- /dev/null +++ b/_heapMax-xx.js @@ -0,0 +1,11 @@ +// index: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]; +const a = [0, 5, 20, 6, 12, 65, 1, 4, 9, 3, 89, 22, 25, 28, 10]; +// heapSort([12, 11, 13, 5, 6, 7]); + +// s 0 +// / \ +// 5 20 +// / \ / \ +// 6 12 65 1 +// / \ / \ / \ / \ +// 4 9 3 89 22 25 28 10