Skip to content

Commit

Permalink
Added Quick Sort Algorithm & Overflow-y : Hidden
Browse files Browse the repository at this point in the history
  • Loading branch information
anugraheeth committed Nov 4, 2024
1 parent f1cec77 commit e9229a2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
35 changes: 35 additions & 0 deletions src/algorithms/quickSort.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
export default function quickSort(array) {
const auxiliaryArray = [...array];
const animations = [];
quickSortHelper(auxiliaryArray, 0, auxiliaryArray.length - 1, animations);
console.log("Sorted array:", auxiliaryArray);
return animations;
}

function quickSortHelper(array, startIdx, endIdx, animations) {
if (startIdx < endIdx) {
const pivotIdx = partition(array, startIdx, endIdx, animations);
quickSortHelper(array, startIdx, pivotIdx - 1, animations);
quickSortHelper(array, pivotIdx + 1, endIdx, animations);
}
}

function partition(array, startIdx, endIdx, animations) {
const pivotValue = array[endIdx];
let pivotIdx = startIdx;
for (let i = startIdx; i < endIdx; i++) {
animations.push([i, endIdx, false]);
if (array[i] < pivotValue) {
if (i !== pivotIdx) {
animations.push([i, pivotIdx, true]);
[array[i], array[pivotIdx]] = [array[pivotIdx], array[i]];
}
pivotIdx++;
}
}
if (pivotIdx !== endIdx) {
animations.push([pivotIdx, endIdx, true]);
[array[pivotIdx], array[endIdx]] = [array[endIdx], array[pivotIdx]];
}
return pivotIdx;
}
1 change: 1 addition & 0 deletions src/assets/graph.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
body {
background-color: var(--bg-primary);
color: var(--text-primary);
overflow-y: hidden;
}

/* Graph Container */
Expand Down
12 changes: 10 additions & 2 deletions src/components/graph.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useState, useEffect, useRef } from "react";
import "../assets/graph.css";
import bubbleSort from "../algorithms/bubbleSort";
import quickSort from "../algorithms/quickSort";

function Graph() {
const [array, setArray] = useState([]);
Expand Down Expand Up @@ -61,7 +62,14 @@ function Graph() {
setIsSorting(false);
return;
}
}
} else if (algo === "quick") {
animation = quickSort(array);
if (!Array.isArray(animation)) {
console.error("Invalid animation returned from sorting algorithm");
setIsSorting(false);
return;
}
}

setAnimations(animation);
visualizeSort(animation);
Expand Down Expand Up @@ -160,7 +168,7 @@ function Graph() {
{isSorting ? 'Stop' : 'Reset'}
</button>
</div>
<p className="note">Note: Currently only Bubble Sort is functioning. More sorting algorithms will be added soon.</p>
<p className="note">Note: Currently only Bubble Sort & Quick Sort is functioning. More sorting algorithms will be added soon.</p>
</div>
<div className="section-container">
<div className="graph">
Expand Down

0 comments on commit e9229a2

Please sign in to comment.