Skip to content

Commit

Permalink
only bubble sort for now
Browse files Browse the repository at this point in the history
  • Loading branch information
musa211 committed Jan 20, 2022
1 parent ab6b29e commit 5bf99d2
Show file tree
Hide file tree
Showing 11 changed files with 712 additions and 60 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@material-ui/core": "^4.12.3",
"@material-ui/icons": "^4.11.2",
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "^12.0.0",
"@testing-library/user-event": "^13.2.1",
Expand Down
62 changes: 40 additions & 22 deletions src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,55 @@
text-align: center;
}

.App-logo {
height: 40vmin;
pointer-events: none;
.frame {
margin: 0;
display: flex;
justify-content: center;
align-items: center;
}

.container {
margin: 5em 0;
display: flex;
justify-content: center;
align-items: center;
height: 220px;
}

@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
.card {
border-radius: 2em;
padding: 0 1em;
background-color: #818181;
}

.App-header {
background-color: #282c34;
min-height: 100vh;
.controller {
background: #555;
margin: 0 1em;
border: 1px solid;
height: 3em;
width: 3em;
border-radius: 100%;
background: transparent;
cursor: pointer;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}

.control-buttons {
height: 4em;
width: 15em;
background-color: #eee;
display: flex;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
align-items: center;
}

.App-link {
color: #61dafb;
.control-pannel {
display: flex;
justify-content: center;
align-items: center;
}

@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
.pannel {
}
220 changes: 200 additions & 20 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,205 @@
import logo from './logo.svg';
import React, { Component } from 'react';

// Algorithms
import BubbleSort from './algorithms/BS';

// Icons
import Play from '@material-ui/icons/PlayCircleOutlineRounded';
import Forward from '@material-ui/icons/SkipNextRounded';
import Backward from '@material-ui/icons/SkipPreviousRounded';
import RotateLeft from '@material-ui/icons/RotateLeft';

import Bar from './components/Bar';
//CSS
import './App.css';

function App() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
);
class App extends Component {
state = {
array: [],
arraySteps: [],
colorKey: [],
colorSteps: [],
currentStep: 0,
count: 15,
delay: 500,
algorithm: 'Bubble Sort',
timeouts: [],
};

ALGORITHMS = {
'Bubble Sort': BubbleSort,
};

componentDidMount() {
this.generateRandomArray();
}

generateSteps = () => {
let array = this.state.array.slice();
let steps = this.state.arraySteps.slice();
let colorSteps = this.state.colorSteps.slice();

this.ALGORITHMS[this.state.algorithm](array, 0, steps, colorSteps);

this.setState({
arraySteps: steps,
colorSteps: colorSteps,
});
};

clearTimeouts = () => {
this.state.timeouts.forEach((timeout) => clearTimeout(timeout));
this.setState({
timeouts: [],
});
};

clearColorKey = () => {
let blankKey = new Array(this.state.count).fill(0);

this.setState({
colorKey: blankKey,
colorSteps: [blankKey],
});
};

generateRandomNumber = (min, max) => {
return Math.floor(Math.random() * (max - min) + min);
};

generateRandomArray = () => {
this.clearTimeouts();
this.clearColorKey();
const count = this.state.count;
const temp = [];

for (let i = 0; i < count; i++) {
temp.push(this.generateRandomNumber(50, 200));
}

this.setState(
{
array: temp,
arraySteps: [temp],
currentStep: 0,
},
() => {
this.generateSteps();
}
);
};

changeArray = (index, value) => {
let arr = this.state.array;
arr[index] = value;
this.setState(
{
array: arr,
arraySteps: [arr],
currentStep: 0,
},
() => {
this.generateSteps();
}
);
};

previousStep = () => {
let currentStep = this.state.currentStep;
if (currentStep === 0) return;
currentStep -= 1;
this.setState({
currentStep: currentStep,
array: this.state.arraySteps[currentStep],
colorKey: this.state.colorSteps[currentStep],
});
};

nextStep = () => {
let currentStep = this.state.currentStep;
if (currentStep >= this.state.arraySteps.length - 1) return;
currentStep += 1;
this.setState({
currentStep: currentStep,
array: this.state.arraySteps[currentStep],
colorKey: this.state.colorSteps[currentStep],
});
};

start = () => {
let steps = this.state.arraySteps;
let colorSteps = this.state.colorSteps;

this.clearTimeouts();

let timeouts = [];
let i = 0;

while (i < steps.length - this.state.currentStep) {
let timeout = setTimeout(() => {
let currentStep = this.state.currentStep;
this.setState({
array: steps[currentStep],
colorKey: colorSteps[currentStep],
currentStep: currentStep + 1,
});
timeouts.push(timeout);
}, this.state.delay * i);
i++;
}

this.setState({
timeouts: timeouts,
});
};

render() {
let bars = this.state.array.map((value, index) => (
<Bar
key={index}
index={index}
length={value}
color={this.state.colorKey[index]}
changeArray={this.changeArray}
/>
));

let playButton;

if (this.state.arraySteps.length === this.state.currentStep) {
playButton = (
<button className='controller' onClick={this.generateRandomArray}>
<RotateLeft />
</button>
);
} else {
playButton = (
<button className='controller' onClick={this.start}>
<Play />
</button>
);
}

return (
<div className='app'>
<div className='frame'>
<div className='barsDiv container card'>{bars}</div>
</div>
<div className='control-pannel'>
<div className='control-buttons'>
<button className='controller' onClick={this.previousStep}>
<Backward />
</button>
{playButton}
<button className='controller' onClick={this.nextStep}>
<Forward />
</button>
</div>
</div>
<div className='pannel'></div>
</div>
);
}
}

export default App;
8 changes: 0 additions & 8 deletions src/App.test.js

This file was deleted.

26 changes: 26 additions & 0 deletions src/algorithms/BS.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {swap} from './helpers';

const bs = (array, position, arraySteps, colorSteps) => {
let colorKey = colorSteps[colorSteps.length - 1].slice();

for (let i = 0; i < array.length - 1; i++) {
for (let j = 0; j < array.length - i - 1; j++) {
if (array[j] > array[j + 1]) {
array = swap(array, j, j + 1);
}
arraySteps.push(array.slice());
colorKey[j] = 1;
colorKey[j + 1] = 1;
colorSteps.push(colorKey.slice());
colorKey[j] = 0;
colorKey[j + 1] = 0;
}
colorKey[arraySteps.length - 1 - i] = 2;
arraySteps.push(array.slice());
colorSteps.push(colorKey.slice());
}
colorSteps[colorSteps.length - 1] = new Array(array.length).fill(2);
return;
};

export default bs;
6 changes: 6 additions & 0 deletions src/algorithms/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function swap(array, i, j) {
let c = array[i];
array[i] = array[j];
array[j] = c;
return array;
}
Loading

0 comments on commit 5bf99d2

Please sign in to comment.