-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: quick sort now uses index rather than splice
- Loading branch information
Showing
4 changed files
with
19 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,169 +0,0 @@ | ||
import { sleep } from "../utils.js"; | ||
|
||
export async function mergeSort(bars, sleepTime) { | ||
await sortMerge(bars, 0, bars.length - 1, sleepTime); | ||
} | ||
|
||
async function sortMerge(bars, start, end, sleepTime) { | ||
if (start >= end) { | ||
return; | ||
} | ||
|
||
let mid = Math.floor((start + end) / 2); | ||
await sortMerge(bars, start, mid, sleepTime); | ||
await sortMerge(bars, mid + 1, end, sleepTime); | ||
|
||
await merge(bars, start, mid, end, sleepTime); | ||
} | ||
|
||
async function merge(bars, start, mid, end, sleepTime) { | ||
let left = bars.slice(start, mid + 1); | ||
let right = bars.slice(mid + 1, end + 1); | ||
|
||
left.forEach(element => { | ||
element.barElement.style.backgroundColor = 'yellow'; | ||
}); | ||
right.forEach(element => { | ||
element.barElement.style.backgroundColor = 'red'; | ||
}); | ||
|
||
let i = 0, j = 0, k = start; | ||
|
||
while (i < left.length && j < right.length) { | ||
bars[k].barElement.style.backgroundColor = 'yellow'; // Highlight the current bar being considered | ||
|
||
if (left[i].height <= right[j].height) { | ||
bars[k].height = left[i].height; | ||
bars[k].barElement.style.height = left[i].height + '%'; | ||
i++; | ||
} else { | ||
bars[k].height = right[j].height; | ||
bars[k].barElement.style.height = right[j].height + '%'; | ||
j++; | ||
} | ||
|
||
await sleep(sleepTime); // Pause for visualization | ||
bars[k].barElement.style.backgroundColor = 'blue'; // Reset color | ||
k++; | ||
} | ||
|
||
while (i < left.length) { | ||
bars[k].height = left[i].height; | ||
bars[k].barElement.style.height = left[i].height + '%'; | ||
bars[k].barElement.style.backgroundColor = 'yellow'; // Highlight the current bar being considered | ||
await sleep(sleepTime); // Pause for visualization | ||
bars[k].barElement.style.backgroundColor = 'blue'; // Reset color | ||
i++; | ||
k++; | ||
} | ||
|
||
while (j < right.length) { | ||
bars[k].height = right[j].height; | ||
bars[k].barElement.style.height = right[j].height + '%'; | ||
bars[k].barElement.style.backgroundColor = 'yellow'; // Highlight the current bar being considered | ||
await sleep(sleepTime); // Pause for visualization | ||
bars[k].barElement.style.backgroundColor = 'blue'; // Reset color | ||
j++; | ||
k++; | ||
} | ||
left.forEach(element => { | ||
element.barElement.style.backgroundColor = 'blue'; | ||
}); | ||
right.forEach(element => { | ||
element.barElement.style.backgroundColor = 'blue'; | ||
}); | ||
|
||
} | ||
|
||
|
||
// async function merge(left, right){ | ||
// let result = []; | ||
// let i = 0; | ||
// let j = 0; | ||
|
||
// while(i < left.length && j < right.length){ | ||
// if(left[i].height < right[j].height){ | ||
// result.push(left[i]); | ||
// i++; | ||
// } else { | ||
// result.push(right[j]); | ||
// j++; | ||
// } | ||
// } | ||
// while(i < left.length){ | ||
// result.push(left[i]); | ||
// i++; | ||
// } | ||
// while(j < right.length){ | ||
// result.push(right[j]); | ||
// j++; | ||
// } | ||
// console.log('result',result); | ||
// // result.forEach(element => {bars[element.index].barElement.height = element.height + '%';}); | ||
// return result; | ||
|
||
// } | ||
|
||
|
||
|
||
// import { sleep } from "../utils.js"; | ||
|
||
// export async function mergeSort(bars, sleepTime) { | ||
// await sortMerge(bars, 0, bars.length - 1, sleepTime); | ||
// } | ||
|
||
// async function sortMerge(bars, start, end, sleepTime) { | ||
// if (start >= end) { | ||
// return; | ||
// } | ||
|
||
// let mid = Math.floor((start + end) / 2); | ||
// await sortMerge(bars, start, mid, sleepTime); | ||
// await sortMerge(bars, mid + 1, end, sleepTime); | ||
// await merge(bars, start, mid, end, sleepTime); | ||
// } | ||
|
||
// async function merge(bars, start, mid, end, sleepTime) { | ||
// let left = bars.slice(start, mid + 1); | ||
// let right = bars.slice(mid + 1, end + 1); | ||
|
||
// let i = 0, j = 0, k = start; | ||
|
||
// while (i < left.length && j < right.length) { | ||
// bars[k].barElement.style.backgroundColor = 'yellow'; // Highlight the current bar being considered | ||
|
||
// if (left[i].height <= right[j].height) { | ||
// bars[k].height = left[i].height; | ||
// bars[k].barElement.style.height = left[i].height + '%'; | ||
// i++; | ||
// } else { | ||
// bars[k].height = right[j].height; | ||
// bars[k].barElement.style.height = right[j].height + '%'; | ||
// j++; | ||
// } | ||
|
||
// await sleep(sleepTime); // Pause for visualization | ||
// bars[k].barElement.style.backgroundColor = 'blue'; // Reset color | ||
// k++; | ||
// } | ||
|
||
// while (i < left.length) { | ||
// bars[k].height = left[i].height; | ||
// bars[k].barElement.style.height = left[i].height + '%'; | ||
// bars[k].barElement.style.backgroundColor = 'yellow'; // Highlight the current bar being considered | ||
// await sleep(sleepTime); // Pause for visualization | ||
// bars[k].barElement.style.backgroundColor = 'blue'; // Reset color | ||
// i++; | ||
// k++; | ||
// } | ||
|
||
// while (j < right.length) { | ||
// bars[k].height = right[j].height; | ||
// bars[k].barElement.style.height = right[j].height + '%'; | ||
// bars[k].barElement.style.backgroundColor = 'yellow'; // Highlight the current bar being considered | ||
// await sleep(sleepTime); // Pause for visualization | ||
// bars[k].barElement.style.backgroundColor = 'blue'; // Reset color | ||
// j++; | ||
// k++; | ||
// } | ||
// } | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,37 @@ | ||
import { sleep, swap, swapBars } from "../utils.js"; | ||
import { sleep, swapBars } from "../utils.js"; | ||
|
||
export async function quickSort(bars, sleepTime) { | ||
await sortQuick(bars, sleepTime); | ||
await sortQuick(bars, 0, bars.length - 1, sleepTime); | ||
console.log(bars); | ||
} | ||
|
||
async function sortQuick(bars, sleepTime) { | ||
if (bars.length <= 1) { | ||
return; | ||
async function sortQuick(bars, low, high, sleepTime) { | ||
if (low < high) { | ||
let pivotPos = await partition(bars, low, high, sleepTime); | ||
await sortQuick(bars, low, pivotPos - 1, sleepTime); | ||
await sortQuick(bars, pivotPos + 1, high, sleepTime); | ||
} | ||
|
||
let pivotPos = await partition(bars, sleepTime); | ||
|
||
console.log('pivotpos',pivotPos); | ||
console.log('bars lenght',bars.length); | ||
await sortQuick(bars.toSpliced(0, pivotPos), sleepTime); | ||
await sortQuick(bars.toSpliced(pivotPos), sleepTime); | ||
|
||
} | ||
|
||
async function partition(bars, sleepTime) { | ||
|
||
let pivotElement = bars[bars.length - 1]; | ||
|
||
async function partition(bars, low, high, sleepTime) { | ||
let pivotElement = bars[high]; | ||
pivotElement.barElement.style.backgroundColor = 'yellow'; | ||
|
||
let cursor = -1; | ||
let cursor = low - 1; | ||
|
||
for (let i = 0; i < bars.length; i++) { | ||
for (let i = low; i < high; i++) { | ||
if (bars[i].height <= pivotElement.height) { | ||
cursor++; | ||
await swapBars(bars, i, cursor); | ||
await swapBars(bars, i, cursor, sleepTime); | ||
} | ||
} | ||
|
||
cursor++; | ||
await swapBars(bars, cursor, high, sleepTime); | ||
|
||
await swapBars(bars, bars.length - 1, cursor); | ||
pivotElement.barElement.style.backgroundColor = 'blue'; | ||
|
||
return cursor; | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters