From f7dae6f15d0d068db47bedced7479b8fda439fbb Mon Sep 17 00:00:00 2001 From: hiluan Date: Sat, 16 Dec 2023 18:50:23 -0800 Subject: [PATCH 1/3] testing heap --- _heapMax.js | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 _heapMax.js diff --git a/_heapMax.js b/_heapMax.js new file mode 100644 index 0000000..c0be074 --- /dev/null +++ b/_heapMax.js @@ -0,0 +1,83 @@ +// // 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); + } +} + +function buildMaxHeap(arr) { + const n = arr.length; + + // Build a max heap + 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); + console.log(n, arr); + } + return arr; +} + +const a = [null, 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 +// From fec388048c13c208fee8c054fcd8943ee0ed1ce5 Mon Sep 17 00:00:00 2001 From: hiluan Date: Sat, 16 Dec 2023 20:19:23 -0800 Subject: [PATCH 2/3] testing max heap --- _heap.js | 95 ++++++++++++++++++++++++++++++++++++++++++++++------- _heapMax.js | 40 +++++++++++----------- 2 files changed, 103 insertions(+), 32 deletions(-) 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.js b/_heapMax.js index c0be074..0ab9bed 100644 --- a/_heapMax.js +++ b/_heapMax.js @@ -20,7 +20,12 @@ // // / \ // // 3 1 -function maxHeapify(arr, n, i) { +// original [null, 0, 5, 20, 6, 12, 65, 1, 4, 9, 3, 89, 22, 25, 28, 10]; +// L +// i l r +// running [null, 20, 5, 0, 6, 12, 65, 1, 4, 9, 3, 89, 22, 25, 28, 10]; + +const maxHeapify = (arr, n, i) => { let largest = i; const left = 2 * i + 1; const right = 2 * i + 2; @@ -34,44 +39,39 @@ function maxHeapify(arr, n, i) { } 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); } -} +}; -function buildMaxHeap(arr) { +const buildMaxHeap = (arr) => { const n = arr.length; - // 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 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); - console.log(n, arr); } - return arr; -} -const a = [null, 0, 5, 20, 6, 12, 65, 1, 4, 9, 3, 89, 22, 25, 28, 10]; + return arr; +}; -// console.log(heapSort(a)); +// 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 // / \ From 987981a6a311bee271fccef41badc0ebd5478424 Mon Sep 17 00:00:00 2001 From: hiluan Date: Sun, 17 Dec 2023 17:25:23 -0800 Subject: [PATCH 3/3] practicing maxheap O(nlogn) --- _heapMax-231217.js | 72 ++++++++++++++++++++++++++++++++++++++++ _heapMax-xx.js | 11 ++++++ _heapMax.js | 83 ---------------------------------------------- 3 files changed, 83 insertions(+), 83 deletions(-) create mode 100644 _heapMax-231217.js create mode 100644 _heapMax-xx.js delete mode 100644 _heapMax.js 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 diff --git a/_heapMax.js b/_heapMax.js deleted file mode 100644 index 0ab9bed..0000000 --- a/_heapMax.js +++ /dev/null @@ -1,83 +0,0 @@ -// // 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 - -// original [null, 0, 5, 20, 6, 12, 65, 1, 4, 9, 3, 89, 22, 25, 28, 10]; -// L -// i l r -// running [null, 20, 5, 0, 6, 12, 65, 1, 4, 9, 3, 89, 22, 25, 28, 10]; - -const 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) { - [arr[i], arr[largest]] = [arr[largest], arr[i]]; - maxHeapify(arr, n, largest); - } -}; - -const buildMaxHeap = (arr) => { - const n = arr.length; - // 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); - } -}; - -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; -}; - -// 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 -//