-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleSortViz.pde
1706 lines (1490 loc) · 38.2 KB
/
SimpleSortViz.pde
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.util.*;
import java.lang.reflect.*;
import java.util.LinkedList;
int scl, w, pauseMS = 5;
int[] arr;
int items = 600;
boolean sort = false;
SortingAlgo sortingAlgorithm;
Scene currentScene;
int[] matchArr;
String[] sortAlgoArr =
new String[]{
"Settings", "Random Inf.",
"BubbleSort", "InsertionSort", "SelectionSort",
"MergeSort", "HeapSort", "QuickSort", "ShellSort",
"CountingSort", "RadixSort", "BucketSort", "TimSort",
"CombSort", "PigeonholeSort", "CycleSort", "CocktailSort",
"StrandSort", "BitonicSort", "PancakeSort", "BinaryInsertionSort",
"BogoSort", "GnomeSort", "StoogeSort", "TreeSort", "OddEvenSort", "MergeSort3Way",
"StalinSort"
};
enum Settings {
ITEMS("Set items"), SEED("Set seed"), RANDOM_SEED("Set random seed");
private String name;
private Settings(String name) {
this.name = name;
}
public String getName() {
return name;
}
public static Settings getType(String name) {
for (Settings s : Settings.values()) {
if (s.getName().equals(name)) return s;
}
return null;
}
}
String[] settings;
HashMap<String, Object> settingsMap = new HashMap();
void setup() {
size(1000, 800);
//fullScreen();
settings = new String[Settings.values().length+1];
settings[0] = "Back";
int i = 1;
for (Settings s : Settings.values()) {
settings[i++] = s.getName();
}
settingsMap.put(settings[1], 600);
settingsMap.put(settings[2], 101010101010l);
settingsMap.put(settings[3], null);
//int items = (int) settingsMap.get(Settings.ITEMS.getName());
if (items > width/2) {
items = width/2;
println("Limit hit, defaults to " + width/2);
}
reset();
noLoop();
currentScene = Scene.MENU;
}
enum Scene {
MENU,
SORT,
INF_SORT,
SETTINGS
}
void reset() {
List<Integer> sizeList = new ArrayList<Integer>();
//int items = (int) settingsMap.get(Settings.ITEMS.getName());
for (int i = 1; i < items+1; i++) {
sizeList.add(i);
}
int maxHeight = sizeList.get(sizeList.size()-1);
w = width/(sizeList.size());
scl = height/maxHeight;
matchArr = toIntArray(sizeList);
arr = shuffleArray(sizeList);
redIndex = -1;
blueIndex = -1;
greenIndex = -1;
sorting = false;
}
void keyPressed() {
if (key == CODED) {
if (keyCode == UP) {
if (currentScene == Scene.MENU)
decSelect(sortAlgoArr);
else if (currentScene == Scene.SETTINGS)
decSelect(settings);
} else if (keyCode == DOWN) {
if (currentScene == Scene.MENU)
incSelect(sortAlgoArr);
else if (currentScene == Scene.SETTINGS)
incSelect(settings);
}
} else if (keyCode == ENTER || key == ' ') {
if (currentScene == Scene.MENU) {
println(selectIndex);
if (sortAlgoArr[selectIndex] == "Settings") {
currentScene = Scene.SETTINGS;
redraw();
} else if (sortAlgoArr[selectIndex] == "Random Inf.") {
currentScene = Scene.INF_SORT;
} else {
initClass(sortAlgoArr[selectIndex]);
startAlgo();
}
} else if (currentScene == Scene.SETTINGS) {
String menuItem = settings[selectIndex];
if (menuItem == "Back")
toMenu();
if (menuItem == Settings.RANDOM_SEED.getName()) {
println("new Random");
Random r = new Random();
settingsMap.put(menuItem, r.nextLong());
redraw();
}
}
} else if (key == 'r') {
toMenu();
}
}
void toMenu() {
currentScene = Scene.MENU;
reset();
sort = false;
redraw();
}
void initClass(String str) {
try {
Class<?> innerClass = Class.forName(this.getClass().getCanonicalName() + "$" + str);
Constructor<?> ctor = innerClass.getDeclaredConstructor( this.getClass());
sortingAlgorithm = (SortingAlgo) ctor.newInstance(this);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (NoSuchMethodException e) {
e.printStackTrace();
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
catch (InstantiationException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
}
void startAlgo() {
currentScene = Scene.SORT;
redraw();
}
int selectIndex = 0;
void incSelect(Object[] arr) {
if (selectIndex + 1 > arr.length-1) selectIndex = 0;
else selectIndex++;
redraw();
}
void decSelect(Object[] arr) {
if (selectIndex - 1 < 0) selectIndex = arr.length-1;
else selectIndex--;
redraw();
}
void drawMenu() {
for (int y = 0; y < sortAlgoArr.length; y++) {
if (selectIndex == y) textSize(20);
else textSize(12);
text(sortAlgoArr[y], width/2, height/4+(y*20));
}
}
void drawSettings() {
for (int y = 0; y < settings.length; y++) {
if (selectIndex == y) textSize(20);
else textSize(12);
String val = getVal(settings[y]);
text((val.length()==0?"":val+" ") + settings[y], width/2, height/4+(y*20));
}
}
//"Set items","Set seed"
String getVal(String str) {
String val = "";
Settings s = null;
if (null == Settings.getType(str)) {
s = Settings.getType(str);
}
if (s == null) return "";
switch(s) {
case ITEMS :
val = (String)settingsMap.get(str).toString();
break;
case SEED:
val = (String)settingsMap.get(str).toString();
break;
default:
val = "";
}
return val;
}
void drawSortGraph() {
for (int i = 0; i < arr.length-1; i++) {
if (i == redIndex) fill(255, 0, 0);
else if (i == blueIndex) fill(0, 0, 255);
else if (i == greenIndex) fill(0, 255, 0);
else fill(255);
rect(i*w, height-(arr[i]*scl), w, (arr[i]*scl));
}
}
int redIndex = -1;
int blueIndex = -1;
int greenIndex = -1;
boolean sorting = false;
void draw() {
background(0);
switch (currentScene) {
case MENU:
drawMenu();
break;
case SORT:
sort = isSorted(arr);
if (!sort && !sorting) {
sorting = true;
thread("sort");
} else {
redraw();
}
if (sort) {
sorting = false;
}
drawSortGraph();
break;
case INF_SORT:
//
break;
case SETTINGS:
drawSettings();
break;
}
}
public void sort() {
sortingAlgorithm.sort(arr);
}
public void step() {
step(pauseMS, 0);
}
public void step(int ms) {
step(ms, 0);
}
public void step(int ms, int ns) {
try {
redraw();
Thread.sleep(ms, ns);
redraw();
}
catch (Exception x) {
x.printStackTrace();
}
}
public int[] toIntArray(List<Integer> list) {
int[] ret = new int[list.size()];
int i = 0;
for (Integer e : list)
ret[i++] = e;
return ret;
}
public List<Integer> toList(int[] list) {
List ret = new ArrayList();
for (int i = 0; i < list.length-1; i++) {
ret.add(list[i]);
}
return ret;
}
public int[] shuffleArray(List<Integer> a) {
List<Integer> b = new ArrayList<Integer>();
Object l = (Object) Settings.SEED.getName();
Random r = new Random((long)settingsMap.get(l));
while (a.size() != 0) {
int arrayIndex = (int) (r.nextFloat() * (a.size()));
b.add(a.get(arrayIndex));
a.remove(a.get(arrayIndex));
}
return toIntArray(b);
}
public boolean isSorted(int[] arr) {
boolean sorted = true;
for (int i = 0; i < arr.length-1; i++) {
if (arr[i] != matchArr[i]) sorted = false;
}
return sorted;
}
interface SortingAlgo {
boolean sort(int[] a);
}
//SORTING ALGORITHMS
class BubbleSort implements SortingAlgo {
boolean sort(int[] a) {
boolean sorted = false;
int temp;
while (!sorted) {
sorted = true;
for (int i = 0; i < a.length - 1; i++) {
if (a[i] > a[i+1]) {
redIndex = i;
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
sorted = false;
}
}
step();
}
return sorted;
}
}
class InsertionSort implements SortingAlgo {
boolean sort(int[] a) {
for (int i = 1; i < a.length; i++) {
int current = a[i];
int j = i - 1;
while (j >= 0 && current < a[j]) {
a[j+1] = a[j];
redIndex = j;
j--;
step();
}
// at this point we've exited, so j is either -1
// or it's at the first element where current >= a[j]
a[j+1] = current;
step();
}
step();
return true;
}
}
class SelectionSort implements SortingAlgo {
boolean sort(int[] a) {
for (int i = 0; i < a.length; i++) {
int min = a[i];
int minId = i;
for (int j = i+1; j < a.length; j++) {
if (a[j] < min) {
min = a[j];
minId = j;
}
}
// swapping
int temp = a[i];
redIndex = min;
a[i] = min;
a[minId] = temp;
step();
}
step();
return true;
}
}
class MergeSort implements SortingAlgo {
boolean sort(int[] a) {
mergeSort(a, 0, a.length-1);
step();
return true;
}
void mergeSort(int[] array, int left, int right) {
if (right <= left) return;
int mid = (left+right)/2;
mergeSort(array, left, mid);
mergeSort(array, mid+1, right);
merge(array, left, mid, right);
step();
}
void merge(int[] array, int left, int mid, int right) {
// calculating lengths
int lengthLeft = mid - left + 1;
int lengthRight = right - mid;
// creating temporary subarrays
int leftArray[] = new int [lengthLeft];
int rightArray[] = new int [lengthRight];
// copying our sorted subarrays into temporaries
for (int i = 0; i < lengthLeft; i++)
leftArray[i] = array[left+i];
for (int i = 0; i < lengthRight; i++)
rightArray[i] = array[mid+i+1];
// iterators containing current index of temp subarrays
int leftIndex = 0;
int rightIndex = 0;
// copying from leftArray and rightArray back into array
for (int i = left; i < right + 1; i++) {
// if there are still uncopied elements in R and L, copy minimum of the two
if (leftIndex < lengthLeft && rightIndex < lengthRight) {
if (leftArray[leftIndex] < rightArray[rightIndex]) {
array[i] = leftArray[leftIndex];
leftIndex++;
} else {
array[i] = rightArray[rightIndex];
rightIndex++;
}
}
// if all the elements have been copied from rightArray, copy the rest of leftArray
else if (leftIndex < lengthLeft) {
array[i] = leftArray[leftIndex];
leftIndex++;
}
// if all the elements have been copied from leftArray, copy the rest of rightArray
else if (rightIndex < lengthRight) {
array[i] = rightArray[rightIndex];
rightIndex++;
}
}
step();
}
}
class HeapSort implements SortingAlgo {
boolean sort(int[] a) {
heapSort(a);
step();
return true;
}
void heapSort(int[] array) {
if (array.length == 0) return;
// Building the heap
int length = array.length;
// we're going from the first non-leaf to the root
for (int i = length / 2-1; i >= 0; i--)
heapify(array, length, i);
for (int i = length-1; i >= 0; i--) {
int temp = array[0];
array[0] = array[i];
array[i] = temp;
heapify(array, i, 0);
}
}
void heapify(int[] array, int length, int i) {
int leftChild = 2*i+1;
int rightChild = 2*i+2;
int largest = i;
// if the left child is larger than parent
if (leftChild < length && array[leftChild] > array[largest]) {
largest = leftChild;
}
// if the right child is larger than parent
if (rightChild < length && array[rightChild] > array[largest]) {
largest = rightChild;
}
// if a swap needs to occur
if (largest != i) {
int temp = array[i];
array[i] = array[largest];
array[largest] = temp;
heapify(array, length, largest);
step();
}
}
}
class QuickSort implements SortingAlgo {
boolean sort(int[] a) {
quickSort(a, 0, a.length-1);
step();
return true;
}
int partition(int[] array, int begin, int end) {
int pivot = end;
int counter = begin;
for (int i = begin; i < end; i++) {
if (array[i] < array[pivot]) {
int temp = array[counter];
array[counter] = array[i];
array[i] = temp;
redIndex = i;
counter++;
step();
}
}
greenIndex = counter;
blueIndex = pivot;
int temp = array[pivot];
array[pivot] = array[counter];
array[counter] = temp;
return counter;
}
void quickSort(int[] array, int begin, int end) {
if (end <= begin) return;
int pivot = partition(array, begin, end);
quickSort(array, begin, pivot-1);
quickSort(array, pivot+1, end);
}
}
class ShellSort implements SortingAlgo {
boolean sort(int[] array) {
// first part uses the Knuth's interval sequence
int h = 1;
while (h <= array.length / 3) {
h = 3 * h + 1; // h is equal to highest sequence of h<=length/3
// (1,4,13,40...)
}
// next part
while (h > 0) { // for array of length 10, h=4
// This step is similar to insertion sort below
for (int i = 0; i < array.length; i++) {
int temp = array[i];
int j;
for (j = i; j > h - 1 && array[j - h] >= temp; j = j - h) {
array[j] = array[j - h];
step();
}
array[j] = temp;
}
h = (h - 1) / 3;
}
return true;
}
}
class CountingSort implements SortingAlgo {
boolean sort(int[] arr) {
int max = Arrays.stream(arr).max().getAsInt();
int min = Arrays.stream(arr).min().getAsInt();
int range = max - min + 1;
int count[] = new int[range];
int output[] = new int[arr.length];
for (int i = 0; i < arr.length; i++)
{
count[arr[i] - min]++;
redIndex = i;
step();
}
for (int i = 1; i < count.length; i++)
{
count[i] += count[i - 1];
redIndex = i;
step();
}
for (int i = arr.length - 1; i >= 0; i--)
{
output[count[arr[i] - min] - 1] = arr[i];
count[arr[i] - min]--;
redIndex = i;
step();
}
for (int i = 0; i < arr.length; i++)
{
arr[i] = output[i];
redIndex = i;
step();
}
return true;
}
}
class RadixSort implements SortingAlgo {
boolean sort(int[] arr) {
radixsort(arr, arr.length);
return true;
}
int getMax(int arr[], int n) {
int mx = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > mx)
mx = arr[i];
return mx;
}
void radixsort(int arr[], int n) {
// Find the maximum number to know number of digits
int m = getMax(arr, n);
// Do counting sort for every digit. Note that instead
// of passing digit number, exp is passed. exp is 10^i
// where i is current digit number
for (int exp = 1; m/exp > 0; exp *= 10) {
countSort(arr, n, exp);
}
}
void countSort(int arr[], int n, int exp) {
int output[] = new int[n]; // output array
int i;
int count[] = new int[10];
Arrays.fill(count, 0);
// Store count of occurrences in count[]
for (i = 0; i < n; i++) {
count[ (arr[i]/exp)%10 ]++;
}
// Change count[i] so that count[i] now contains
// actual position of this digit in output[]
for (i = 1; i < 10; i++) {
count[i] += count[i - 1];
}
// Build the output array
for (i = n - 1; i >= 0; i--)
{
output[count[ (arr[i]/exp)%10 ] - 1] = arr[i];
count[ (arr[i]/exp)%10 ]--;
redIndex = count[ (arr[i]/exp)%10 ] - 1;
blueIndex = i;
step();
}
// Copy the output array to arr[], so that arr[] now
// contains sorted numbers according to curent digit
for (i = 0; i < n; i++) {
arr[i] = output[i];
blueIndex = i;
step();
}
}
}
class BucketSort implements SortingAlgo {
boolean sort(int[] arr) {
bucketSort(arr);
return true;
}
void bucketSort(int[] input) {
// get hash codes
final int[] code = hash(input);
// create and initialize buckets to ArrayList: O(n)
List[] buckets = new List[code[1]];
for (int i = 0; i < code[1]; i++) {
buckets[i] = new ArrayList();
redIndex = i;
step();
}
// distribute data into buckets: O(n)
for (int i : input) {
buckets[hash(i, code)].add(i);
redIndex = i;
step();
}
// sort each bucket O(n)
for (List bucket : buckets) {
Collections.sort(bucket);
step();
}
int ndx = 0;
// merge the buckets: O(n)
for (int b = 0; b < buckets.length; b++) {
for (Object v : buckets[b]) {
input[ndx++] = (int)v;
redIndex = b;
step();
}
}
}
private int[] hash(int[] input) {
int m = input[0];
for (int i = 1; i < input.length; i++) {
if (m < input[i]) {
m = input[i];
redIndex = i;
}
}
return new int[] { m, (int) Math.sqrt(input.length) };
}
private int hash(int i, int[] code) {
return (int) ((double) i / code[0] * (code[1] - 1));
}
}
class TimSort implements SortingAlgo {
int RUN = 32;
boolean sort(int[] arr) {
timSort(arr, arr.length);
return true;
}
// this function sorts array from left index to
// to right index which is of size atmost RUN
void insertionSort(int[] arr, int left, int right)
{
for (int i = left + 1; i <= right; i++)
{
int temp = arr[i];
int j = i - 1;
while (arr[j] > temp && j >= left)
{
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = temp;
}
}
// merge function merges the sorted runs
void merge(int[] arr, int l, int m, int r)
{
// original array is broken in two parts
// left and right array
int len1 = m - l + 1, len2 = r - m;
int[] left = new int[len1];
int[] right = new int[len2];
for (int x = 0; x < len1; x++)
{
left[x] = arr[l + x];
}
for (int x = 0; x < len2; x++)
{
right[x] = arr[m + 1 + x];
}
int i = 0;
int j = 0;
int k = l;
// after comparing, we merge those two array
// in larger sub array
while (i < len1 && j < len2)
{
if (left[i] <= right[j])
{
arr[k] = left[i];
i++;
} else
{
arr[k] = right[j];
j++;
}
k++;
}
// copy remaining elements of left, if any
while (i < len1)
{
arr[k] = left[i];
k++;
i++;
}
// copy remaining element of right, if any
while (j < len2)
{
arr[k] = right[j];
k++;
j++;
}
}
// iterative Timsort function to sort the
// array[0...n-1] (similar to merge sort)
void timSort(int[] arr, int n)
{
// Sort individual subarrays of size RUN
for (int i = 0; i < n; i += RUN)
{
insertionSort(arr, i, Math.min((i + 31), (n - 1)));
}
// start merging from size RUN (or 32). It will merge
// to form size 64, then 128, 256 and so on ....
for (int size = RUN; size < n; size = 2 * size)
{
// pick starting point of left sub array. We
// are going to merge arr[left..left+size-1]
// and arr[left+size, left+2*size-1]
// After every merge, we increase left by 2*size
for (int left = 0; left < n; left += 2 * size)
{
// find ending point of left sub array
// mid+1 is starting point of right sub array
int mid = left + size - 1;
int right = Math.min((left + 2 * size - 1), (n - 1));
// merge sub array arr[left.....mid] &
// arr[mid+1....right]
merge(arr, left, mid, right);
}
}
}
}
class CombSort implements SortingAlgo {
boolean sort(int[] arr) {
combsort(arr);
return true;
}
// To find gap between elements
int getNextGap(int gap)
{
// Shrink gap by Shrink factor
gap = (gap*10)/13;
if (gap < 1)
return 1;
return gap;
}
// Function to sort arr[] using Comb Sort
void combsort(int arr[])
{
int n = arr.length;
// initialize gap
int gap = n;
// Initialize swapped as true to make sure that
// loop runs
boolean swapped = true;
// Keep running while gap is more than 1 and last
// iteration caused a swap
while (gap != 1 || swapped == true)
{
// Find next gap
gap = getNextGap(gap);
// Initialize swapped as false so that we can
// check if swap happened or not
swapped = false;
// Compare all elements with current gap
for (int i=0; i<n-gap; i++)
{
if (arr[i] > arr[i+gap])
{
// Swap arr[i] and arr[i+gap]
int temp = arr[i];
arr[i] = arr[i+gap];
arr[i+gap] = temp;
// Set swapped
swapped = true;
}
step(0, 10);
}
}
}
}
class PigeonholeSort implements SortingAlgo {
boolean sort(int[] arr) {
pigeonhole_sort(arr, arr.length);
return true;
}
void pigeonhole_sort(int arr[], int n)
{
int min = arr[0];
int max = arr[0];
int range, i, j, index;
for (int a=0; a<n; a++)
{
if (arr[a] > max)
max = arr[a];
if (arr[a] < min)
min = arr[a];
redIndex = a;
step();
}
range = max - min + 1;
int[] phole = new int[range];
Arrays.fill(phole, 0);
for (i = 0; i<n; i++) {
phole[arr[i] - min]++;
redIndex = range-i;
step();
}
index = 0;
for (j = 0; j<range; j++)
while (phole[j]-->0) {
arr[index++]=j+min;
redIndex = j;
step();
}
}
}
class CycleSort implements SortingAlgo {
boolean sort(int[] arr) {
cycleSort(arr, arr.length);
return true;
}
void cycleSort(int arr[], int n) {
// count number of memory writes
int writes = 0;
// traverse array elements and put it to on
// the right place
for (int cycle_start = 0; cycle_start <= n - 2; cycle_start++) {
// initialize item as starting point
int item = arr[cycle_start];
// Find position where we put the item. We basically
// count all smaller elements on right side of item.
int pos = cycle_start;
for (int i = cycle_start + 1; i < n; i++)
if (arr[i] < item) {
pos++;
}
// If item is already in correct position
if (pos == cycle_start)
continue;
// ignore all duplicate elements
while (item == arr[pos])
pos += 1;
// put the item to it's right position
if (pos != cycle_start) {
int temp = item;
item = arr[pos];
arr[pos] = temp;
writes++;
}
// Rotate rest of the cycle
while (pos != cycle_start) {
pos = cycle_start;
// Find position where we put the element
for (int i = cycle_start + 1; i < n; i++) {
if (arr[i] < item)
pos += 1;
}