Skip to content

Commit 5e5ab02

Browse files
committed
copied form git
1 parent 1ff2e2f commit 5e5ab02

File tree

4 files changed

+166
-65
lines changed

4 files changed

+166
-65
lines changed

README.md

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
<p align="center">
2+
<img src="https://i.imgur.com/8Wa1gR2.png">
3+
</p>
14
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
25

36
## Available Scripts
@@ -12,11 +15,6 @@ Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
1215
The page will reload if you make edits.<br />
1316
You will also see any lint errors in the console.
1417

15-
### `npm test`
16-
17-
Launches the test runner in the interactive watch mode.<br />
18-
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
19-
2018
### `npm run build`
2119

2220
Builds the app for production to the `build` folder.<br />
@@ -27,42 +25,6 @@ Your app is ready to be deployed!
2725

2826
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
2927

30-
### `npm run eject`
31-
32-
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
33-
34-
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
35-
36-
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
37-
38-
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
39-
40-
## Learn More
41-
42-
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
43-
44-
To learn React, check out the [React documentation](https://reactjs.org/).
45-
46-
### Code Splitting
47-
48-
This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting
49-
50-
### Analyzing the Bundle Size
51-
52-
This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size
53-
54-
### Making a Progressive Web App
55-
56-
This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app
57-
58-
### Advanced Configuration
59-
60-
This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration
61-
62-
### Deployment
63-
64-
This section has moved here: https://facebook.github.io/create-react-app/docs/deployment
65-
66-
### `npm run build` fails to minify
28+
## 😍 Like this project 😍
6729

68-
This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify
30+
#### please give a star it. 🚀

src/components/SortingVisualizer.css

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.array-container {
2+
position: absolute;
3+
left: 100px;
4+
/* -ms-transform: rotate(180deg); */
5+
/* transform: rotate(180deg); */
6+
}
7+
8+
.array-bar {
9+
width: 3px;
10+
background-color: rgb(39, 139, 221);
11+
display: inline-block;
12+
margin: 0 1px;
13+
}

src/components/SortingVisualizer.jsx

Lines changed: 71 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
import React from "react";
2-
class SortingVisualizer extends React.Component {
2+
import { getMergeSortAnimations } from "../sortingAlgorithms/sortingAlgorithms.js";
3+
import "./SortingVisualizer.css";
4+
5+
// Change this value for the speed of the animations.
6+
const ANIMATION_SPEED_MS = 1;
7+
8+
// Change this value for the number of bars (value) in the array.
9+
const NUMBER_OF_ARRAY_BARS = 200;
10+
11+
// This is the main color of the array bars.
12+
const PRIMARY_COLOR = "turquoise";
13+
14+
// This is the color of array bars that are being compared throughout the animations.
15+
const SECONDARY_COLOR = "red";
16+
17+
export default class SortingVisualizer extends React.Component {
318
constructor(props) {
419
super(props);
520

@@ -12,42 +27,76 @@ class SortingVisualizer extends React.Component {
1227
this.resetArray();
1328
}
1429

15-
/**
16-
* this method will reset global array
17-
*/
18-
1930
resetArray() {
2031
const array = [];
21-
for (let i = 0; i < 100; i++) {
22-
array.push(randomIntFromInterval(5, 1000));
32+
for (let i = 0; i < NUMBER_OF_ARRAY_BARS; i++) {
33+
array.push(randomIntFromInterval(5, 730));
2334
}
2435
this.setState({ array });
2536
}
2637

38+
mergeSort() {
39+
const animations = getMergeSortAnimations(this.state.array);
40+
for (let i = 0; i < animations.length; i++) {
41+
const arrayBars = document.getElementsByClassName("array-bar");
42+
const isColorChange = i % 3 !== 2;
43+
if (isColorChange) {
44+
const [barOneIdx, barTwoIdx] = animations[i];
45+
const barOneStyle = arrayBars[barOneIdx].style;
46+
const barTwoStyle = arrayBars[barTwoIdx].style;
47+
const color = i % 3 === 0 ? SECONDARY_COLOR : PRIMARY_COLOR;
48+
setTimeout(() => {
49+
barOneStyle.backgroundColor = color;
50+
barTwoStyle.backgroundColor = color;
51+
}, i * ANIMATION_SPEED_MS);
52+
} else {
53+
setTimeout(() => {
54+
const [barOneIdx, newHeight] = animations[i];
55+
const barOneStyle = arrayBars[barOneIdx].style;
56+
barOneStyle.height = `${newHeight}px`;
57+
}, i * ANIMATION_SPEED_MS);
58+
}
59+
}
60+
}
61+
62+
quickSort() {
63+
// We leave it as an exercise to the viewer of this code to implement this method.
64+
}
65+
66+
heapSort() {
67+
// We leave it as an exercise to the viewer of this code to implement this method.
68+
}
69+
70+
bubbleSort() {
71+
// We leave it as an exercise to the viewer of this code to implement this method.
72+
}
73+
2774
render() {
2875
const { array } = this.state;
76+
2977
return (
30-
<>
78+
<div className="array-container">
3179
{array.map((value, idx) => (
32-
<div className="array-bar" key={idx}>
33-
{value}
34-
</div>
80+
<div
81+
className="array-bar"
82+
key={idx}
83+
style={{
84+
backgroundColor: PRIMARY_COLOR,
85+
height: `${value}px`,
86+
}}
87+
></div>
3588
))}
36-
</>
89+
<br />
90+
<button onClick={() => this.resetArray()}>Generate New Array</button>
91+
<button onClick={() => this.mergeSort()}>Merge Sort</button>
92+
<button onClick={() => this.quickSort()}>Quick Sort</button>
93+
<button onClick={() => this.heapSort()}>Heap Sort</button>
94+
<button onClick={() => this.bubbleSort()}>Bubble Sort</button>
95+
</div>
3796
);
3897
}
3998
}
4099

41-
SortingVisualizer.propTypes = {};
42-
43-
export default SortingVisualizer;
44-
45-
/**
46-
*
47-
* @param {int} min
48-
* @param {int} max
49-
*/
50-
51100
function randomIntFromInterval(min, max) {
52101
return Math.floor(Math.random() * (max - min + 1) + min);
53102
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
export function getMergeSortAnimations(array) {
2+
const animations = [];
3+
if (array.length <= 1) return array;
4+
const auxiliaryArray = array.slice();
5+
mergeSortHelper(array, 0, array.length - 1, auxiliaryArray, animations);
6+
return animations;
7+
}
8+
9+
function mergeSortHelper(
10+
mainArray,
11+
startIdx,
12+
endIdx,
13+
auxiliaryArray,
14+
animations
15+
) {
16+
if (startIdx === endIdx) return;
17+
const middleIdx = Math.floor((startIdx + endIdx) / 2);
18+
mergeSortHelper(auxiliaryArray, startIdx, middleIdx, mainArray, animations);
19+
mergeSortHelper(auxiliaryArray, middleIdx + 1, endIdx, mainArray, animations);
20+
doMerge(mainArray, startIdx, middleIdx, endIdx, auxiliaryArray, animations);
21+
}
22+
23+
function doMerge(
24+
mainArray,
25+
startIdx,
26+
middleIdx,
27+
endIdx,
28+
auxiliaryArray,
29+
animations
30+
) {
31+
let k = startIdx;
32+
let i = startIdx;
33+
let j = middleIdx + 1;
34+
while (i <= middleIdx && j <= endIdx) {
35+
// These are the values that we're comparing; we push them once
36+
// to change their color.
37+
animations.push([i, j]);
38+
// These are the values that we're comparing; we push them a second
39+
// time to revert their color.
40+
animations.push([i, j]);
41+
if (auxiliaryArray[i] <= auxiliaryArray[j]) {
42+
// We overwrite the value at index k in the original array with the
43+
// value at index i in the auxiliary array.
44+
animations.push([k, auxiliaryArray[i]]);
45+
mainArray[k++] = auxiliaryArray[i++];
46+
} else {
47+
// We overwrite the value at index k in the original array with the
48+
// value at index j in the auxiliary array.
49+
animations.push([k, auxiliaryArray[j]]);
50+
mainArray[k++] = auxiliaryArray[j++];
51+
}
52+
}
53+
while (i <= middleIdx) {
54+
// These are the values that we're comparing; we push them once
55+
// to change their color.
56+
animations.push([i, i]);
57+
// These are the values that we're comparing; we push them a second
58+
// time to revert their color.
59+
animations.push([i, i]);
60+
// We overwrite the value at index k in the original array with the
61+
// value at index i in the auxiliary array.
62+
animations.push([k, auxiliaryArray[i]]);
63+
mainArray[k++] = auxiliaryArray[i++];
64+
}
65+
while (j <= endIdx) {
66+
// These are the values that we're comparing; we push them once
67+
// to change their color.
68+
animations.push([j, j]);
69+
// These are the values that we're comparing; we push them a second
70+
// time to revert their color.
71+
animations.push([j, j]);
72+
// We overwrite the value at index k in the original array with the
73+
// value at index j in the auxiliary array.
74+
animations.push([k, auxiliaryArray[j]]);
75+
mainArray[k++] = auxiliaryArray[j++];
76+
}
77+
}

0 commit comments

Comments
 (0)