Skip to content

Commit 74d986e

Browse files
committed
refactoring and bug fixing
1 parent 6cb6b7f commit 74d986e

File tree

3 files changed

+8
-36
lines changed

3 files changed

+8
-36
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
# testing
99
/coverage
10-
10+
/.vscode
1111
# production
1212
/build
1313

src/SortingAlgorithms/BubbleSort.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ export default function BubbleSort(array){
1818
break;
1919
}
2020
console.log(arr);
21-
return arr;
21+
return animations;
2222
}

src/SortingVisualizer/SortingVisualizer.jsx

+6-34
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default class SortingVisualizer extends React.Component{
2323

2424
componentDidMount(){
2525
this.resetArray();
26-
this.setState({arrayToSort:[], prevChanged:[]});
26+
//this.setState({arrayToSort:[], prevChanged:[]});
2727
}
2828

2929
resetArray(){
@@ -35,8 +35,8 @@ export default class SortingVisualizer extends React.Component{
3535
this.setState({ arrayToSort, prevChanged });
3636
}
3737

38-
bubbleSort(){
39-
let sortedArrayAnim = InsertionSort(this.state.arrayToSort);
38+
SortArray(algo){
39+
let sortedArrayAnim = algo(this.state.arrayToSort);
4040
let arrayToSort = this.state.arrayToSort;
4141
let prevChanged = this.state.prevChanged;
4242

@@ -86,34 +86,6 @@ export default class SortingVisualizer extends React.Component{
8686
}
8787
}
8888

89-
insertionSort(){
90-
let sortedArrayAnim = InsertionSort(this.state.arrayToSort);
91-
let arrayToSort = this.state.arrayToSort;
92-
let prevChanged = this.state.prevChanged;
93-
94-
//loop through all the animations
95-
for (let index = 0; index < sortedArrayAnim.length; index++) {
96-
const [i,j] = sortedArrayAnim[index];
97-
98-
setTimeout(() => {
99-
//change array
100-
let temp = arrayToSort[i];
101-
arrayToSort[i] = arrayToSort[j];
102-
arrayToSort[j] = temp;
103-
104-
prevChanged.push(i,j);
105-
106-
if(index == sortedArrayAnim.length - 1){
107-
prevChanged.push(arrayToSort.length + 1, arrayToSort.length + 1);
108-
this.setState({prevChanged});
109-
}
110-
111-
this.setState({ arrayToSort, prevChanged })
112-
113-
}, index * 10);
114-
}
115-
}
116-
11789
selectionSort(){
11890
let sortedArrayAnim = SelectionSort(this.state.arrayToSort);
11991
let arrayToSort = this.state.arrayToSort;
@@ -173,10 +145,10 @@ export default class SortingVisualizer extends React.Component{
173145
))}
174146

175147
<button onClick={() => this.resetArray()}>Generate new array</button>
176-
<button onClick={() => this.insertionSort()}>Insertion Sort</button>
148+
<button onClick={() => this.SortArray(InsertionSort)}>Insertion Sort</button>
149+
<button onClick={() => this.SortArray(BubbleSort)}>Bubble Sort</button>
177150
<button onClick={() => this.selectionSort()}>Selection Sort</button>
178-
<button onClick={() => this.bubbleSort()}>Bubble Sort</button>
179-
<button onClick={() => this.quickSort()}>Quick Sort</button>
151+
<button onClick={() => this.SortArray(GetQuickSortAnimation)}>Quick Sort</button>
180152
</ div>
181153
);
182154
}

0 commit comments

Comments
 (0)