-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSorting-algorithms-2.0.ino
204 lines (164 loc) · 4.61 KB
/
Sorting-algorithms-2.0.ino
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//bibliotheek voor een betere timer / library for a more precise timer
#include <eRCaGuy_Timer2_Counter.h>
#include<stdio.h>
//setup output pins to control gears
const int swap = 8;
const int diff = 9;
const int times = 10;
const int compare = 11;
const int memory = 12;
void setup() {
//configure Timer2
timer2.setup();
//unsigned long timeStart = micros(); //units of 0.5us; the count accumulated by Timer2_Counter
//acquire time stamps
//unsigned long t_T2_count = micros(); //units of 0.5us; the count accumulated by Timer2_Counter
//int normal_mem = freeMemory();
//Serial.println("Free memory:" + normal_mem);
//setup serial
Serial.begin(9600);
pinMode(swap, OUTPUT);
pinMode(diff, OUTPUT);
pinMode(times, OUTPUT);
pinMode(compare, OUTPUT);
pinMode(memory, OUTPUT);
//digitalWrite(8, HIGH);// sets the digital pin 13 as output
start();
}
//TODO
//wisselen van elementen input van fysisch systeem
//knoppen voor bijv voorgesorteerde arrays - keuzemenu > wachten op led
// lesplan > na fysieke constructie
// gears maken
//setup variables
int i, count = 10 ;
float timer_step = 0;
static const unsigned long REFRESH_INTERVAL = 1000000;
long lastRefreshTime = 0;
float loopTime, loopTime2, loopTime3, loopTime4, loopTime5, loopTime6, loopTime7;
float timeLost = 0;
float timeForStep;
void start() {
int arr[count], arr_res[count];
int arr_size = sizeof(arr) / sizeof(arr[0]);
int mnd = random(count);
// verschillende arrays
//pre-sorted array
// for (i = 0; i < count; i++) {
// arr[i] = i;
// //Serial.print(arr[i]);
// }
//reverse array
// for (i = 0; i < count; i++) {
// arr[i] = count-i;
// //Serial.print(arr[i]);
// }
//random array
for (i = 0; i < count; i++) {
arr[i] = random(0, 100);
//Serial.print(arr[i]);
}
//zero array
// for (i = 0; i < count; i++) {
// arr[i] = 0;
// //Serial.print(arr[i]);
// }
//reserve array om later array terug te kunnen vullen
for (i = 0; i < count; i++) {
arr_res[i] = arr[i];
}
//seriële print van startarray
Serial.println("Startarray");
printArray(arr, arr_size);
//quicksort methode
Serial.println("quicksort");
timeLost = 0;
timer2.reset();
quickSort(arr, 0, arr_size - 1);
loopTime = (timer2.get_count() - timeLost)/2;
Serial.println();
Serial.println("finished");
printArray(arr, arr_size);
fillArray(count, arr, arr_res);
Serial.println("Bubblesort");
timeLost = 0;
timer2.reset();
bubbleSort(arr, 0, arr_size - 1);
loopTime2 = (timer2.get_count() - timeLost)/2;
Serial.println();
Serial.println("finished");
printArray(arr, arr_size);
fillArray(count, arr, arr_res);
Serial.println("Bogosort");
timeLost = 0;
timer2.reset();
//bogosort(arr, arr_size);
loopTime3 = (timer2.get_count() - timeLost)/2;
Serial.println();
Serial.println("finished");
printArray(arr, arr_size);
fillArray(count, arr, arr_res);
Serial.println("mergeSort");
timeLost = 0;
timer2.reset();
mergeSort(arr, 0, arr_size - 1);
loopTime4 = (timer2.get_count() - timeLost)/2;
Serial.println();
Serial.println("finished");
printArray(arr, arr_size);
fillArray(count, arr, arr_res);
Serial.println("selectionSort");
timeLost = 0;
timer2.reset();
selectionSort(arr, arr_size);
loopTime5 = (timer2.get_count() - timeLost)/2;
Serial.println();
Serial.println("finished");
printArray(arr, arr_size);
fillArray(count, arr, arr_res);
Serial.println("Timsort");
timeLost = 0;
timer2.reset();
timSort(arr, arr_size);
loopTime6 = (timer2.get_count() - timeLost)/2;
Serial.println();
Serial.println("finished");
printArray(arr, arr_size);
fillArray(count, arr, arr_res);
//werk niet naar behoren
// timeStart = timer2.get_count()
//treeSort(arr7, arr_size);
// unsigned long loopTime7 = (timer2.get_count() - timeLost)/2;
// Serial.println("finished");
// printArray(arr, arr_size);
// fillArray(count, arr, arr_res);
//
//
Serial.println("");
//Serial.print("Quick: ");
Serial.println(loopTime);
//Serial.print("Bubble: ");
Serial.println(loopTime2);
//Serial.print("Bogo: ");
Serial.println(loopTime3);
//Serial.print("Mergesort: ");
Serial.println(loopTime4);
//Serial.print("Selection: ");
Serial.println(loopTime5);
//Serial.print("Tim: ");
Serial.println(loopTime6);
//Serial.println(loopTime7);
}
void printArray(int arr[], int n)
{
for (int i = 0; i < n; ++i) {
Serial.print(arr[i]);
Serial.print(" ");
}
Serial.print("\n");
}
void fillArray(int count, int arr[],int arr_res[]){
for (int i = 0; i < count; i++) {
arr[i] = arr_res[i];
}
}