-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sort.c
358 lines (298 loc) · 7.71 KB
/
Sort.c
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include "Sort.h"
#include "List/List.h"
#include "SortTest.h"
#include <stdio.h>
static size_t comp;
static size_t swap;
void status_reset()
{
comp=0;
swap=0;
}
size_t getComp()
{
return comp;
}
size_t getSwap()
{
return swap;
}
static void Swap(List l, size_t a, size_t b)
{
swap++;
List_swap(l, a, b);
GUI_update(0);
GUI_wait();
}
static int Compare(List l, size_t a, size_t b)
{
static long long int a_last=-1, b_last=-1;
comp++;
GUI_update(0);
GUI_wait();
if(l){
if(a_last!=-1){
GUI_Window_marks_remove(0, a_last);
GUI_Window_marks_remove(0, b_last);
}
GUI_Window_marks_add(0, a, (struct Color){255,0,0});
GUI_Window_marks_add(0, b, (struct Color){0,255,0});
a_last=a;
b_last=b;
return LIST_get(S_TYPE)(l, a)>LIST_get(S_TYPE)(l, b);
}
return a>b;
}
void BubbleSort(List l)
{
char sorted=0;
int size=List_size(l);
while(!sorted){
sorted=1;
for(int i=0; i<size-1; i++){
if(Compare(l,i,i+1)){
Swap(l,i,i+1);
sorted=0;
}
}
size--;
}
}
void Bad_BubbleSort(List l)
{
for(int i=0; i<List_size(l); i++){
for(int x=0; x<List_size(l)-1; x++){
if(Compare(l,x,x+1)){
Swap(l,x,x+1);
}
}
}
}
void SelectionSort(List l)
{
int smallest_index;
for(int i=0; i<List_size(l)-1;i++){
smallest_index=i;
for(int e=i+1; e<List_size(l);e++){
if(Compare(l,smallest_index,e))
smallest_index=e;
}
if(smallest_index!=i){
Swap(l,i,smallest_index);
}
}
}
void StalinSort(List l)
{
for(int i=0; i<List_size(l)-1;i++){
while(i<List_size(l)-1){
if(Compare(l,i,i+1)){
List_rmi(l, i+1);
continue;
}
break;
}
}
}
#ifdef NSPIRE
#define RADIX 4
#else
#define RADIX 2 // FASTEST 256
#endif
#ifdef TCC
void memcpy(unsigned char *a, unsigned char *b, int size){
for(int i=0; i<size; i++){
a[i]=b[i];
}
}
#endif
static void ClearArray(int* a){
for(int i=0; i<RADIX; i++){
a[i]=0;
}
}
void LSD_Radix(List l)
{
int Counts[RADIX],
size = List_size(l),
*data;
List l2 = LIST_create(S_TYPE),
*input=&l,
*output=&l2;
List_resize(l2, size);
// backup main window title
const char *title_backup = GUI_Window_title_get(0);
GUI_Window_title_set(0, "primary buffer");
// setup second buffer window
GUI_Window_id win = GUI_windows_append();
GUI_Window_set_list(win, l2);
GUI_Window_do_render(win, 1);
GUI_Window_title_set(win, "secondary buffer");
void *tmp;
int iter = (int)(log2(get_max(l))/log2(RADIX)+1);
for(int level=0; level<iter; level++){
data = List_start(*output);
for(int i=0; i<size; i++)
data[i]=0;
int shift=(level*(int)log2(RADIX));
ClearArray(Counts);
for(int i=0; i<List_size(*input);i++){
Counts[(LIST_get(S_TYPE)(*input, i)>>shift)&(RADIX-1)]++;
}
for(int i=1; i<RADIX; i++){
Counts[i]+=Counts[i-1];
}
for(int i=List_size(*input)-1; i>=0; i--){
LIST_get(S_TYPE)(*output,--Counts[(LIST_get(S_TYPE)(*input, i)>>shift)&(RADIX-1)]) = LIST_get(S_TYPE)(*input, i);
GUI_wait();
GUI_update(0);
}
// swap input / output
tmp=input;
input=output;
output=tmp;
}
// uneven iteration -> swap to main list
if(iter%2)
List_copy(l,l2);
GUI_windows_remove(win);
List_free(l2);
// reset main window title
GUI_Window_title_set(0, title_backup);
}
void InsertionSort(List l)
{
int j, p1=-1, p2=-1;
S_TYPE tmp;
for(int i=1; i<List_size(l);i++){
tmp=LIST_get(S_TYPE)(l, i);
if(p1!=-1)
GUI_Window_marks_remove(0, p1);
GUI_Window_marks_add(0, i, (struct Color){255,0,0});
p1=i;
j=i-1;
while(j >=0 && Compare(NULL,LIST_get(S_TYPE)(l, j),tmp)){
if(p2!=-1)
GUI_Window_marks_remove(0, p2);
GUI_Window_marks_add(0, j, (struct Color){0,255,0});
p2=j;
LIST_get(S_TYPE)(l, j+1)=LIST_get(S_TYPE)(l, j);
j--;
}
LIST_get(S_TYPE)(l, j+1)=tmp;
}
}
void CocktailshakerSort(List l)
{
unsigned int iteration=0, i=0, swaped=2;
int direction=1;
do{
swaped--;
for(i+=direction; (i<List_size(l)-iteration-1) && (i>=iteration); i+=direction){
if(Compare(l,i,i-direction)^(direction>0)){
Swap(l,i,i-direction);
swaped=2;
}
}
direction=-direction;
if(direction>0){
iteration++;
i=iteration;
}
}while(swaped>0);
}
void BogoSort(List l)
{
#ifdef NSPIRE
while (List_verify(l) && !isKeyPressed(KEY_NSPIRE_ESC)){
#else
while (List_verify(l)){
#endif
int i=(float)rand()/RAND_MAX*(List_size(l)-1);
int j=(float)rand()/RAND_MAX*(List_size(l)-1);
Swap(l,i,j);
}
}
void QuickSort(int left, int right, List l)
{
if(left<right){
unsigned int pivot = right;
int i=left, j=right-1;
while(i<j){
for(;i<right && Compare(l,pivot,i);i++);
for(;j>left && !Compare(l,pivot,j);j--);
if(i<j)
Swap(l,i,j);
}
if(Compare(l,i,pivot))
Swap(l,i,pivot);
QuickSort(left,i-1,l);
QuickSort(i+1,right,l);
}
}
void QuickSort_launcher(List l)
{
QuickSort(0,List_size(l)-1,l);
}
void MergeSort(List a, List b) {
}
static GUI_Window_id u, o;
void _ReculinSort(List l, int iter)
{
{
List urlaub = List_create(sizeof(S_TYPE)),
output = List_create(sizeof(S_TYPE));
GUI_Window_set_list(u, urlaub);
GUI_Window_set_list(o, output);
GUI_Window_marks_clear(0);
{
int largest = 0,
i;
List_push(output, List_at(l, 0));
GUI_Window_marks_add(0, 0, (struct Color){0,255,0});
for(i=1; i<List_size(l)-iter; i++)
{
if(LIST_get(S_TYPE)(l, i)>LIST_get(S_TYPE)(l, largest)){
List_push(output, List_at(l, i));
GUI_Window_marks_add(0, i, (struct Color){0,255,0});
largest=i;
}else{
List_push(urlaub, List_at(l, i));
GUI_Window_marks_add(0, i, (struct Color){255,0,0});
}
GUI_wait();
GUI_update(0);
}
for(; i<List_size(l); i++)
List_push(output, List_at(l, i));
}
GUI_Window_do_render(u, 0);
GUI_Window_do_render(o, 0);
//printf("%d | %d\n", urlaub.filled, output.filled);
if(!List_size(urlaub)){
List_copy(l, output);
List_free(urlaub);
List_free(output);
return;
}
List_copy(l, urlaub);
List_concat(l, output);
List_free(urlaub);
List_free(output);
}
_ReculinSort(l, iter+1);
}
void ReculinSort(List l)
{
u=GUI_windows_append();
GUI_Window_foreground_set(u, (struct Color){255,0,0});
GUI_Window_background_set(u, (struct Color){127,127,127});
GUI_Window_title_set(u, "Urlaub");
o=GUI_windows_append();
GUI_Window_foreground_set(o, (struct Color){0,255,0});
GUI_Window_background_set(o, (struct Color){127,127,127});
GUI_Window_title_set(o, "Output");
_ReculinSort(l, 0);
GUI_windows_remove(u);
GUI_windows_remove(o);
}