Compare commits

...

4 Commits

Author SHA1 Message Date
Gustav Ekner
d01c4ed12d
Merge c5016a0229 into 2c67b48c21 2024-04-25 08:30:27 +08:00
Gustav Ekner
c5016a0229 Fixed max 100 column width issue 2022-03-06 20:43:04 +01:00
unknown
a2514b9e7f Changed swap in ShellSort and Heap 2022-03-05 20:51:03 +01:00
Gustav Ekner
822d4e3ac7 Changed swap in QuickSortInPlace and InsertionSort 2022-03-03 20:33:34 +01:00
4 changed files with 16 additions and 27 deletions

View File

@ -6,7 +6,7 @@ export default class InsertionSort extends Sort {
// Go through all array elements... // Go through all array elements...
for (let i = 1; i < array.length; i += 1) { for (let i = 1; i < array.length; i += 1) {
let currentIndex = i; let j = i;
// Call visiting callback. // Call visiting callback.
this.callbacks.visitingCallback(array[i]); this.callbacks.visitingCallback(array[i]);
@ -14,23 +14,17 @@ export default class InsertionSort extends Sort {
// Check if previous element is greater than current element. // Check if previous element is greater than current element.
// If so, swap the two elements. // If so, swap the two elements.
while ( while (
array[currentIndex - 1] !== undefined array[j - 1] !== undefined
&& this.comparator.lessThan(array[currentIndex], array[currentIndex - 1]) && this.comparator.lessThan(array[j], array[j - 1])
) { ) {
// Call visiting callback. // Call visiting callback.
this.callbacks.visitingCallback(array[currentIndex - 1]); this.callbacks.visitingCallback(array[j - 1]);
// Swap the elements. // Swap the elements.
[ [array[j - 1], array[j]] = [array[j], array[j - 1]];
array[currentIndex - 1],
array[currentIndex],
] = [
array[currentIndex],
array[currentIndex - 1],
];
// Shift current index left. // Shift current index left.
currentIndex -= 1; j -= 1;
} }
} }

View File

@ -39,9 +39,7 @@ export default class QuickSortInPlace extends Sort {
* @param {number} rightIndex * @param {number} rightIndex
*/ */
const swap = (leftIndex, rightIndex) => { const swap = (leftIndex, rightIndex) => {
const temp = array[leftIndex]; [array[leftIndex], array[rightIndex]] = [array[rightIndex], array[leftIndex]];
array[leftIndex] = array[rightIndex];
array[rightIndex] = temp;
}; };
const pivot = array[highIndex]; const pivot = array[highIndex];

View File

@ -12,22 +12,20 @@ export default class ShellSort extends Sort {
while (gap > 0) { while (gap > 0) {
// Go and compare all distant element pairs. // Go and compare all distant element pairs.
for (let i = 0; i < (array.length - gap); i += 1) { for (let i = 0; i < (array.length - gap); i += 1) {
let currentIndex = i; let j = i;
let gapShiftedIndex = i + gap; let gapShiftedIndex = i + gap;
while (currentIndex >= 0) { while (j >= 0) {
// Call visiting callback. // Call visiting callback.
this.callbacks.visitingCallback(array[currentIndex]); this.callbacks.visitingCallback(array[j]);
// Compare and swap array elements if needed. // Compare and swap array elements if needed.
if (this.comparator.lessThan(array[gapShiftedIndex], array[currentIndex])) { if (this.comparator.lessThan(array[gapShiftedIndex], array[j])) {
const tmp = array[currentIndex]; [array[j], array[gapShiftedIndex]] = [array[gapShiftedIndex], array[j]];
array[currentIndex] = array[gapShiftedIndex];
array[gapShiftedIndex] = tmp;
} }
gapShiftedIndex = currentIndex; gapShiftedIndex = j;
currentIndex -= gap; j -= gap;
} }
} }

View File

@ -95,9 +95,8 @@ export default class Heap {
* @param {number} indexTwo * @param {number} indexTwo
*/ */
swap(indexOne, indexTwo) { swap(indexOne, indexTwo) {
const tmp = this.heapContainer[indexTwo]; [this.heapContainer[indexOne], this.heapContainer[indexTwo]] = [
this.heapContainer[indexTwo] = this.heapContainer[indexOne]; this.heapContainer[indexTwo], this.heapContainer[indexOne]];
this.heapContainer[indexOne] = tmp;
} }
/** /**