Changed swap in ShellSort and Heap

This commit is contained in:
unknown 2022-03-05 20:51:03 +01:00
parent 822d4e3ac7
commit a2514b9e7f
2 changed files with 2 additions and 6 deletions

View File

@ -21,9 +21,7 @@ export default class ShellSort extends Sort {
// 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[currentIndex])) {
const tmp = array[currentIndex]; [array[currentIndex], array[gapShiftedIndex]] = [array[gapShiftedIndex], array[currentIndex]];
array[currentIndex] = array[gapShiftedIndex];
array[gapShiftedIndex] = tmp;
} }
gapShiftedIndex = currentIndex; gapShiftedIndex = currentIndex;

View File

@ -95,9 +95,7 @@ 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;
} }
/** /**