@@ -23,7 +23,7 @@ export default class SortingVisualizer extends React.Component{
23
23
24
24
componentDidMount ( ) {
25
25
this . resetArray ( ) ;
26
- this . setState ( { arrayToSort :[ ] , prevChanged :[ ] } ) ;
26
+ // this.setState({arrayToSort:[], prevChanged:[]});
27
27
}
28
28
29
29
resetArray ( ) {
@@ -35,8 +35,8 @@ export default class SortingVisualizer extends React.Component{
35
35
this . setState ( { arrayToSort, prevChanged } ) ;
36
36
}
37
37
38
- bubbleSort ( ) {
39
- let sortedArrayAnim = InsertionSort ( this . state . arrayToSort ) ;
38
+ SortArray ( algo ) {
39
+ let sortedArrayAnim = algo ( this . state . arrayToSort ) ;
40
40
let arrayToSort = this . state . arrayToSort ;
41
41
let prevChanged = this . state . prevChanged ;
42
42
@@ -86,34 +86,6 @@ export default class SortingVisualizer extends React.Component{
86
86
}
87
87
}
88
88
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
-
117
89
selectionSort ( ) {
118
90
let sortedArrayAnim = SelectionSort ( this . state . arrayToSort ) ;
119
91
let arrayToSort = this . state . arrayToSort ;
@@ -173,10 +145,10 @@ export default class SortingVisualizer extends React.Component{
173
145
) ) }
174
146
175
147
< 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 >
177
150
< 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 >
180
152
</ div >
181
153
) ;
182
154
}
0 commit comments