From 822d4e3ac72116a156a79c285d22aa06fd284608 Mon Sep 17 00:00:00 2001 From: Gustav Ekner Date: Thu, 3 Mar 2022 20:33:34 +0100 Subject: [PATCH 1/3] Changed swap in QuickSortInPlace and InsertionSort --- .../sorting/insertion-sort/InsertionSort.js | 18 ++++++------------ .../sorting/quick-sort/QuickSortInPlace.js | 4 +--- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/src/algorithms/sorting/insertion-sort/InsertionSort.js b/src/algorithms/sorting/insertion-sort/InsertionSort.js index 7a801613..83e79170 100644 --- a/src/algorithms/sorting/insertion-sort/InsertionSort.js +++ b/src/algorithms/sorting/insertion-sort/InsertionSort.js @@ -6,7 +6,7 @@ export default class InsertionSort extends Sort { // Go through all array elements... for (let i = 1; i < array.length; i += 1) { - let currentIndex = i; + let j = i; // Call visiting callback. this.callbacks.visitingCallback(array[i]); @@ -14,23 +14,17 @@ export default class InsertionSort extends Sort { // Check if previous element is greater than current element. // If so, swap the two elements. while ( - array[currentIndex - 1] !== undefined - && this.comparator.lessThan(array[currentIndex], array[currentIndex - 1]) + array[j - 1] !== undefined + && this.comparator.lessThan(array[j], array[j - 1]) ) { // Call visiting callback. - this.callbacks.visitingCallback(array[currentIndex - 1]); + this.callbacks.visitingCallback(array[j - 1]); // Swap the elements. - [ - array[currentIndex - 1], - array[currentIndex], - ] = [ - array[currentIndex], - array[currentIndex - 1], - ]; + [array[j - 1], array[j]] = [array[j], array[j - 1]]; // Shift current index left. - currentIndex -= 1; + j -= 1; } } diff --git a/src/algorithms/sorting/quick-sort/QuickSortInPlace.js b/src/algorithms/sorting/quick-sort/QuickSortInPlace.js index cc1f5e50..b2c9ae52 100644 --- a/src/algorithms/sorting/quick-sort/QuickSortInPlace.js +++ b/src/algorithms/sorting/quick-sort/QuickSortInPlace.js @@ -39,9 +39,7 @@ export default class QuickSortInPlace extends Sort { * @param {number} rightIndex */ const swap = (leftIndex, rightIndex) => { - const temp = array[leftIndex]; - array[leftIndex] = array[rightIndex]; - array[rightIndex] = temp; + [array[leftIndex], array[rightIndex]] = [array[rightIndex], array[leftIndex]]; }; const pivot = array[highIndex]; From a2514b9e7f425e3e41e99a39a4f3d16febce05eb Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 5 Mar 2022 20:51:03 +0100 Subject: [PATCH 2/3] Changed swap in ShellSort and Heap --- src/algorithms/sorting/shell-sort/ShellSort.js | 4 +--- src/data-structures/heap/Heap.js | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/algorithms/sorting/shell-sort/ShellSort.js b/src/algorithms/sorting/shell-sort/ShellSort.js index cdbba997..6113bc36 100644 --- a/src/algorithms/sorting/shell-sort/ShellSort.js +++ b/src/algorithms/sorting/shell-sort/ShellSort.js @@ -21,9 +21,7 @@ export default class ShellSort extends Sort { // Compare and swap array elements if needed. if (this.comparator.lessThan(array[gapShiftedIndex], array[currentIndex])) { - const tmp = array[currentIndex]; - array[currentIndex] = array[gapShiftedIndex]; - array[gapShiftedIndex] = tmp; + [array[currentIndex], array[gapShiftedIndex]] = [array[gapShiftedIndex], array[currentIndex]]; } gapShiftedIndex = currentIndex; diff --git a/src/data-structures/heap/Heap.js b/src/data-structures/heap/Heap.js index 45dfcfa2..b3ae7068 100644 --- a/src/data-structures/heap/Heap.js +++ b/src/data-structures/heap/Heap.js @@ -95,9 +95,7 @@ export default class Heap { * @param {number} indexTwo */ swap(indexOne, indexTwo) { - const tmp = this.heapContainer[indexTwo]; - this.heapContainer[indexTwo] = this.heapContainer[indexOne]; - this.heapContainer[indexOne] = tmp; + [this.heapContainer[indexOne], this.heapContainer[indexTwo]] = [this.heapContainer[indexTwo], this.heapContainer[indexOne]]; } /** From c5016a0229e092d80c8973f1ce4e21c0230c15e4 Mon Sep 17 00:00:00 2001 From: Gustav Ekner Date: Sun, 6 Mar 2022 20:43:04 +0100 Subject: [PATCH 3/3] Fixed max 100 column width issue --- src/algorithms/sorting/shell-sort/ShellSort.js | 14 +++++++------- src/data-structures/heap/Heap.js | 3 ++- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/algorithms/sorting/shell-sort/ShellSort.js b/src/algorithms/sorting/shell-sort/ShellSort.js index 6113bc36..58859acb 100644 --- a/src/algorithms/sorting/shell-sort/ShellSort.js +++ b/src/algorithms/sorting/shell-sort/ShellSort.js @@ -12,20 +12,20 @@ export default class ShellSort extends Sort { while (gap > 0) { // Go and compare all distant element pairs. for (let i = 0; i < (array.length - gap); i += 1) { - let currentIndex = i; + let j = i; let gapShiftedIndex = i + gap; - while (currentIndex >= 0) { + while (j >= 0) { // Call visiting callback. - this.callbacks.visitingCallback(array[currentIndex]); + this.callbacks.visitingCallback(array[j]); // Compare and swap array elements if needed. - if (this.comparator.lessThan(array[gapShiftedIndex], array[currentIndex])) { - [array[currentIndex], array[gapShiftedIndex]] = [array[gapShiftedIndex], array[currentIndex]]; + if (this.comparator.lessThan(array[gapShiftedIndex], array[j])) { + [array[j], array[gapShiftedIndex]] = [array[gapShiftedIndex], array[j]]; } - gapShiftedIndex = currentIndex; - currentIndex -= gap; + gapShiftedIndex = j; + j -= gap; } } diff --git a/src/data-structures/heap/Heap.js b/src/data-structures/heap/Heap.js index b3ae7068..e12b7afc 100644 --- a/src/data-structures/heap/Heap.js +++ b/src/data-structures/heap/Heap.js @@ -95,7 +95,8 @@ export default class Heap { * @param {number} indexTwo */ swap(indexOne, indexTwo) { - [this.heapContainer[indexOne], this.heapContainer[indexTwo]] = [this.heapContainer[indexTwo], this.heapContainer[indexOne]]; + [this.heapContainer[indexOne], this.heapContainer[indexTwo]] = [ + this.heapContainer[indexTwo], this.heapContainer[indexOne]]; } /**