stupid-sorting.org (4120B)
1 #+TITLE: Stupid Sorting 2 #+DATE: <2023-09-12 Tue 10:02> 3 #+LANGUAGE: en 4 5 The first topic in "Algoritmos e Estruturas de Dados II" ("Algorithms and Data Structures II") classes are sorting algorithms. The first one we learn is the stupid Selection Sort. It's not really that usefull but it's a great example to learn. Then we learn stuff like the Insertion Sort, which's actually usefull and more complex to understand. 6 7 But, as stupid as Selection Sort is, it's kinda straightforward. What about some really, really stupid and useless sorting algorithms? 8 9 For this algorithms I used some =#includes= and a simple function that checks if an array is sorted. The entire source is available [[https://gist.github.com/gboncoffee/2ecfacb0bc7f03626e4c1d2e4d00253f][here at GitHub]]. 10 11 ** Kill Sort 12 Sorts the array by removing unsorted itens. Returns the final size. 13 14 #+begin_src C 15 int kill_sort(int *v, int n) 16 { 17 int pad = 0; 18 for (int i = 1; i < n; i++) { 19 if (v[i] < v[i - 1 - pad]) 20 pad++; 21 else 22 v[i - pad] = v[i]; 23 } 24 25 return n - pad; 26 } 27 #+end_src 28 29 The cool thing about this one is that it keeps the pointer valid. Of course, it's usefull if we want a sorted array but don't need all the elements. It's linear too, which is very cheap, but it writes a lot, and that's not great if writes are expensive. 30 31 ** Thanos Sort 32 Sorts the array by removing a random half of it until it's sorted. Returns the final array pointer and sets n to it's size. 33 34 #+begin_src C 35 int* thanos_sort(int *v, int *n) 36 { 37 srand(time(NULL)); 38 while (*n > 1 && !sorted(v, *n)) { 39 if (random() % 2) { 40 v += *n / 2; 41 } 42 43 ,*n = *n / 2; 44 } 45 46 return v; 47 } 48 #+end_src 49 50 Very similar to Kill Sort. Unfortunatelly, this one leads to much more data loss and does not have predictable behaviour. But it does not make a single write in the array and runs in logarithmic time. Extremelly cheap. Also, it may not keep the pointer valid, and that's why it returns a new one. This should be used with caution to not cause memory leaks. 51 52 ** Random Sort 53 Sorts the array by randomizing a sequence and them checking if it's sorted. 54 55 #+begin_src C 56 void random_sort(int *v, int n) 57 { 58 int x, y, tmp; 59 srand(time(NULL)); 60 while (!sorted(v, n)) { 61 for (int i = 0; i < n; i++) { 62 x = random() % n; 63 y = random() % n; 64 tmp = v[x]; 65 v[x] = v[y]; 66 v[y] = tmp; 67 } 68 } 69 } 70 #+end_src 71 72 This one is fun. It has a little chance of sorting the array in a single iteration. I tested with an array with 10 elements and it usuallys takes between 300 thousand and 700 thousand iterations to finish. As it have a chance of running a single iteration, it have a chance of never sorting at all too. 73 74 ** Intelligent Design Sort 75 Sorts the array by the following principle: 76 77 - There's n! chance of the array being sorted; 78 - So it must have been sorted by an intelligent designer. 79 80 #+begin_src C 81 void intelligent_design_sort(int *v, int n) 82 { 83 (void) v; 84 (void) n; 85 } 86 #+end_src 87 88 This is the cheapest algorithm here. Runs in constant time and uses only a single stack frame of memory /at all, without compiler optimization/. I've only ever saw very few functions cheapest than this in computer science, and they're usually not usefull at all. 89 90 ** Miracle Sort 91 Sorts the array by waiting and hoping cosmic rays are going to flip the memory so the array get's sorted. 92 93 #+begin_src C 94 void miracle_sort(int *v, int n) 95 { 96 while (!sorted(v, n)) 97 sleep(5); 98 } 99 #+end_src 100 101 Yeah it just sits there and patiently waits for a miracle. You can adjust the sleep time to change the behaviour of the algorithm: more time will lead to a slightly slower sort, but less CPU usage. Less time will lead to a slightly faster sort, but more CPU usage. 102 103 ** Schrodinger Sort 104 It's not sorted nor unsorted until you check it. 105 106 #+begin_src C 107 int schrodinger_sort(int *v, int n) 108 { 109 return sorted(v, n); 110 } 111 #+end_src 112 113 It sorts the array by telling you if it's sorted or not, so you decides what to do. Quantum mechanics are really cool, aren't they?