-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
158 lines (145 loc) · 3.69 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import React from 'react';
import { StyleSheet, Text, View, Button, Slider } from 'react-native';
import { VictoryBar } from "victory-native";
import BubbleSort from './src/algorithms/BubbleSort';
import LinearSort from './src/algorithms/LinearSort';
import QuickSort from './src/algorithms/QuickSort';
import MergeSort from './src/algorithms/MergeSort';
// 0: slowest, 500: fastest
const DEFAULT_SORT_SPEED = 0;
const MAX_SPEED = 500;
const MIN_SPEED = 1;
const NUMBERS_AMOUNT = 50;
export default class App extends React.Component {
sortAlgorithm;
intervalId;
componentWillMount() {
this.state = {
data: [],
speed: DEFAULT_SORT_SPEED
};
}
componentDidMount() {
this.resetTimer();
}
resetTimer() {
if (this.intervalId)
clearInterval(this.intervalId);
this.intervalId = setInterval(() => {
if (!this.sortAlgorithm)
return;
while (!this.sortAlgorithm.isCompleted() && !this.sortAlgorithm.next()) {
}
this.setState(() => {
return {
data: this.sortAlgorithm.getData()
};
});
}, MAX_SPEED - this.state.speed);
}
generateRandomValues(amount) {
values = [];
for (var i = 0; i < amount; i++)
values[i] = parseInt(Math.random() * 100);
return values;
}
setBubbleSort() {
this.sortAlgorithm = new BubbleSort(this.generateRandomValues(NUMBERS_AMOUNT));
this.setState(() => {
return {
data: this.sortAlgorithm.getData()
};
});
}
setLinearSort() {
this.sortAlgorithm = new LinearSort(this.generateRandomValues(NUMBERS_AMOUNT));
this.setState(() => {
return {
data: this.sortAlgorithm.getData()
};
});
}
setQuickSort() {
this.sortAlgorithm = new QuickSort(this.generateRandomValues(NUMBERS_AMOUNT));
this.setState(() => {
return {
data: this.sortAlgorithm.getData()
};
});
}
setMergeSort() {
this.sortAlgorithm = new MergeSort(this.generateRandomValues(NUMBERS_AMOUNT));
this.setState(() => {
return {
data: this.sortAlgorithm.getData()
};
});
}
render() {
return (
<View style={styles.container}>
<Text
style={styles.title}>
sort algorithms
</Text>
<VictoryBar
data={this.state.data}
style={ { data: { fill: '#ff66cc' } }}
/>
<Text
style={styles.text}>
Speed: {Math.floor(this.state.speed * 100 / (MAX_SPEED - MIN_SPEED))}%
</Text>
<Slider
maximumTrackTintColor='white'
minimumTrackTintColor='#ff66cc'
style={{ width: 300 }}
step={2}
minimumValue={0}
maximumValue={MAX_SPEED - MIN_SPEED}
onValueChange={val => {
this.setState({ speed: val })
this.resetTimer();
}}
/>
<Button
color='#ff66cc'
title='bubble sort'
onPress={this.setBubbleSort.bind(this)}
/>
<Button
color='#ff66cc'
title='linear sort'
onPress={this.setLinearSort.bind(this)}
/>
<Button
color='#ff66cc'
title='quick sort'
onPress={this.setQuickSort.bind(this)}
/>
<Button
color='#ff66cc'
title='merge sort'
onPress={this.setMergeSort.bind(this)}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'flex-start',
alignItems: 'center',
backgroundColor: '#222',
paddingTop: 50
},
title: {
color: 'white',
fontSize: 20,
fontWeight: '900'
},
text: {
color: 'white'
}
});