-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
712 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.