Skip to content

Commit

Permalink
fix: quick sort now uses index rather than splice
Browse files Browse the repository at this point in the history
  • Loading branch information
SdSadat committed Jun 15, 2024
1 parent 3efbbf3 commit 71d6d58
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 196 deletions.
169 changes: 0 additions & 169 deletions script/Algorithms/MergeSort.js
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++;
// }
// }
36 changes: 14 additions & 22 deletions script/Algorithms/QuickSort.js
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;
}



6 changes: 3 additions & 3 deletions script/sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { sleep, swap } from "./utils.js";
import { bubbleSort } from "./Algorithms/BubbleSort.js";
import { insertionSort } from "./Algorithms/InsertionSort.js";
import { selectionSort } from "./Algorithms/SelectionSort.js";
import { mergeSort } from "./Algorithms/MergeSort.js";
import { quickSort } from "./Algorithms/QuickSort.js";
// import { mergeSort } from "./Algorithms/MergeSort.js";
let width = 100;

if (screen.width < 786) {
Expand All @@ -18,8 +18,8 @@ let sortingAlgorithms = new Map([
['BubbleSort', bubbleSort],
['InsertionSort', insertionSort],
['SelectionSort', selectionSort],
['MergeSort', mergeSort],
['QuickSort', quickSort],
// ['MergeSort', mergeSort],
// ['HeapSort', heapSort],
]);

Expand Down Expand Up @@ -50,7 +50,7 @@ document.addEventListener('DOMContentLoaded', () => {
case 'medium':
return 50;
case 'slow':
return 1000;
return 500;
default:
return 50;
}
Expand Down
4 changes: 2 additions & 2 deletions script/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ export async function swapBars(bars, i, cursor) {
bars[i].barElement.style.height = bars[i].height + '%';
bars[cursor].barElement.style.height = bars[cursor].height + '%';

await sleep(500);
await sleep(50);
}

bars[i].barElement.style.backgroundColor = 'blue';
bars[cursor].barElement.style.backgroundColor = 'blue';
bars[i].barElement.style.transition = '';
bars[cursor].barElement.style.transition = '';
}
}

0 comments on commit 71d6d58

Please sign in to comment.