Skip to content

Commit 0d44d55

Browse files
committed
Sorting Algorithms added
1 parent 60f2544 commit 0d44d55

File tree

12 files changed

+613
-19
lines changed

12 files changed

+613
-19
lines changed

package-lock.json

Lines changed: 112 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"@testing-library/user-event": "^7.2.1",
99
"react": "^16.13.1",
1010
"react-dom": "^16.13.1",
11-
"react-scripts": "3.4.1"
11+
"react-scripts": "3.4.1",
12+
"semantic-ui-react": "^0.88.2"
1213
},
1314
"scripts": {
1415
"start": "react-scripts start",

public/index.html

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
<html lang="en">
33
<head>
44
<meta charset="utf-8" />
5-
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
65
<meta name="viewport" content="width=device-width, initial-scale=1" />
76
<meta name="theme-color" content="#000000" />
87
<meta
98
name="description"
10-
content="Web site created using create-react-app"
9+
content="Website for Visualizing Sorting Algorithms"
1110
/>
12-
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
1311
<!--
1412
manifest.json provides metadata used when your web app is installed on a
1513
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
@@ -24,7 +22,8 @@
2422
work correctly both with client-side routing and a non-root public URL.
2523
Learn how to configure a non-root public URL by running `npm run build`.
2624
-->
27-
<title>React App</title>
25+
<title>Sorting Visualizer</title>
26+
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/[email protected]/dist/semantic.min.css" />
2827
</head>
2928
<body>
3029
<noscript>You need to enable JavaScript to run this app.</noscript>

public/logo192.png

-5.22 KB
Binary file not shown.

public/logo512.png

-9.44 KB
Binary file not shown.

src/App.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,12 @@
11
import React from 'react';
22
import logo from './logo.svg';
33
import './App.css';
4+
import SortingVisualizer from './SortingVisualizer/SortingVisualizer';
45

56
function App() {
67
return (
78
<div className="App">
8-
<header className="App-header">
9-
<img src={logo} className="App-logo" alt="logo" />
10-
<p>
11-
Edit <code>src/App.js</code> and save to reload.
12-
</p>
13-
<a
14-
className="App-link"
15-
href="https://reactjs.org"
16-
target="_blank"
17-
rel="noopener noreferrer"
18-
>
19-
Learn React
20-
</a>
21-
</header>
9+
<SortingVisualizer />
2210
</div>
2311
);
2412
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export function bubbleSort(array){
2+
const animations = [];
3+
const auxillaryArray = array.slice();
4+
let len = array.length;
5+
for (let i = 0; i < len; i++) {
6+
for (let j = 0; j < len-i-1; j++) {
7+
animations.push([j,j+1])
8+
animations.push([j,j+1])
9+
if (auxillaryArray[j] > auxillaryArray[j + 1]) {
10+
animations.push([j,auxillaryArray[j+1],j+1,auxillaryArray[j]])
11+
let tmp = auxillaryArray[j];
12+
auxillaryArray[j] = auxillaryArray[j + 1];
13+
auxillaryArray[j + 1] = tmp;
14+
}
15+
else{
16+
animations.push([j,auxillaryArray[j],j+1,auxillaryArray[j+1]])
17+
}
18+
}
19+
}
20+
return animations;
21+
}

src/SortingAlgorithms/heapSort.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
export function heapSort(array) {
2+
const auxillaryArray = array.slice()
3+
const animations = []
4+
heapSortHelper(array,animations)
5+
return animations
6+
}
7+
8+
function heapSortHelper(array,animations){
9+
let n = array.length
10+
for (let i=n;i>=0;i--){
11+
heapify(array,n,i,animations)
12+
}
13+
14+
for (let i=n-1;i>=0;i--){
15+
animations.push(["swap",0,array[i],i,array[0]])
16+
let temp = array[i]
17+
array[i] = array[0]
18+
array[0] = temp
19+
20+
heapify(array,i,0,animations)
21+
}
22+
}
23+
24+
function heapify(array,n,i,animations){
25+
let largest = i
26+
let left = 2*i+1
27+
let right = 2*i+2
28+
29+
if (left<n && array[left]>array[largest]){
30+
animations.push(["compare1",left,largest])
31+
animations.push(["compare2",left,largest])
32+
largest = left
33+
34+
}
35+
36+
if (right<n && array[right]>array[largest]){
37+
animations.push(["compare1",right,largest])
38+
animations.push(["compare2",right,largest])
39+
largest = right
40+
41+
}
42+
43+
if (largest!==i){
44+
animations.push(["swap",largest,array[i],i,array[largest]])
45+
let temp = array[largest]
46+
array[largest] = array[i]
47+
array[i] = temp
48+
49+
heapify(array,n,largest,animations)
50+
}
51+
52+
}

0 commit comments

Comments
 (0)