Skip to content

Commit ce6b945

Browse files
Added Insertion Sort Logic
1 parent c3fbe11 commit ce6b945

File tree

5 files changed

+33
-6
lines changed

5 files changed

+33
-6
lines changed

public/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
work correctly both with client-side routing and a non-root public URL.
2525
Learn how to configure a non-root public URL by running `npm run build`.
2626
-->
27-
<title>React App</title>
27+
<title>Sort Visualizer</title>
2828
</head>
2929
<body>
3030
<noscript>You need to enable JavaScript to run this app.</noscript>

src/App.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ import { useEffect } from "react";
88
import generateArray from "./helpers/generateArray";
99
import bubbleSort from "./helpers/bubbleSort";
1010
import selectionSort from "./helpers/selectionSort";
11+
import insertionSort from "./helpers/insertionSort";
1112

1213
function App() {
1314
const [data, setData] = useState([]);
1415
const [isSorting, setIsSorting] = useState(false);
1516
const [currIJ, setCurrIJ] = useState({ i: -1, j: -1 });
17+
const [color, setColor] = useState("red");
1618
const [completed, setCompleted] = useState([]);
1719
const [size, setSize] = useState(10);
1820
const [speed, setSpeed] = useState(1);
@@ -24,11 +26,12 @@ function App() {
2426
setData([...arr]);
2527
}, [size, speed, algType]);
2628

27-
const alterStateAfterTimeOut = (arr, i, j, c, cond, index) => {
29+
const alterStateAfterTimeOut = (arr, i, j, c, cond, index, color) => {
2830
setTimeout(
2931
(arr, i, j, c) => {
3032
setData(arr);
3133
setCurrIJ({ i, j });
34+
setColor(color ? color : "red");
3235
// if (c === arr.length) setIsSorting(false);
3336
if (cond) {
3437
setCompleted((prev) => {
@@ -77,6 +80,7 @@ function App() {
7780
const algorithms = {
7881
0: bubbleSort,
7982
1: selectionSort,
83+
2: insertionSort,
8084
};
8185

8286
return (
@@ -91,7 +95,7 @@ function App() {
9195
speed={speed}
9296
speedChangeHandler={speedChangeHandler}
9397
/>
94-
<Chart data={data} currIJ={currIJ} completed={completed} />
98+
<Chart data={data} currIJ={currIJ} completed={completed} color={color} />
9599
<Button
96100
btnName="Sort"
97101
onClickHandler={sortClickHandler}

src/components/Chart/Chart.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const Chart = (props) => {
4444
};
4545
const backgroundColor = props.data.map((el, i) => {
4646
return i === props.currIJ.i || i === props.currIJ.j
47-
? "red"
47+
? props.color
4848
: "rgb(248,248,248)";
4949
});
5050

src/components/Navbar/Navbar.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ const Navbar = (props) => {
1515
<Select
1616
value={props.algType}
1717
onChangeHandler={props.algChangeHandler}
18-
values={[0, 1]}
19-
options={["Bubble Sort", "Selection Sort"]}
18+
values={[0, 1, 2]}
19+
options={["Bubble Sort", "Selection Sort", "Insertion Sort"]}
2020
isDisabled={props.isSorting}
2121
/>
2222
<Select

src/helpers/insertionSort.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const insertionSort = (alterStateAfterTimeOut, data) => {
2+
const array = [...data];
3+
let count = 0;
4+
for (let i = 1; i < array.length; i++) {
5+
let key = array[i];
6+
let j = i - 1;
7+
while (j >= 0 && array[j] > key) {
8+
alterStateAfterTimeOut(array, i, j, count);
9+
count++;
10+
array[j + 1] = array[j];
11+
j--;
12+
}
13+
array[j + 1] = key;
14+
alterStateAfterTimeOut(array, -1, j + 1, count, false, -1, "yellow");
15+
count++;
16+
}
17+
for (let i = 0; i < array.length; i++) {
18+
alterStateAfterTimeOut(array, 0, 0, count, true, i);
19+
}
20+
alterStateAfterTimeOut(array, -1, -1, count);
21+
};
22+
23+
export default insertionSort;

0 commit comments

Comments
 (0)