Skip to content

Commit 31b2128

Browse files
author
Oskar Widmark
committed
fix: improve push sort somewhat
1 parent 984a5ec commit 31b2128

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/sorting-algorithms.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -664,12 +664,16 @@ public async mergeSort(arr, start, end){
664664

665665
public async pushSort(arr: SortValue[]) {
666666
let isSorted = false;
667-
let completed = 0;
667+
let lastIndex = arr.length - 1;
668+
668669
while (!isSorted) {
669670
isSorted = true;
670671
let stackSize = 0;
671-
for (let i = 0; i < arr.length - 1 - completed; i++) {
672+
const end = lastIndex;
673+
674+
for (let i = 0; i < end; i++) {
672675
if (await this.context.compare(arr, i, '>', i + 1)) {
676+
lastIndex = i;
673677
isSorted = false;
674678
await this.context.drawAndSwap(arr, i, i + 1);
675679
for (let j = i; j > i - stackSize; j--) {
@@ -679,7 +683,6 @@ public async mergeSort(arr, start, end){
679683
stackSize++;
680684
}
681685
}
682-
completed++;
683686
}
684687
}
685688
}

0 commit comments

Comments
 (0)