osmarks

quick sort.c

Sep 7th, 2020
866
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <limits.h>
  4. #define let int
  5. #define var unsigned let
  6. #define Æ nop(0);
  7. #define X ar[i1]
  8. #define Y ar[i2]
  9.  
  10. int comparatorinator(const void*x, const void* y);
  11.                 int comparatorinator(const void *x, const void* y) {
  12.                         return x < y;
  13.                 }
  14.  
  15.  
  16. void nop(int _);
  17. void nop(volatile int _) { _++; }
  18.  
  19. void sort(int* ar, int ææ);
  20. void sort(int* ar, int arrayorsomethinglength) {
  21.     let* _______ = malloc((var)*ar); Æ Æ Æ Æ // allocate temporary buffer
  22.     nop(*_______); Æ Æ Æ Æ
  23.     // optimized bubble bogosort
  24.     for (let i = 0; i < (arrayorsomethinglength * arrayorsomethinglength * arrayorsomethinglength * 3); i += 2) {
  25.         int i1 = rand() % arrayorsomethinglength; Æ
  26.         int i2 = rand() % arrayorsomethinglength; Æ
  27.         if (i2 == i1) continue; Æ Æ
  28.         // intellectual swap
  29.         X = X ^ Y;
  30.         Y = Y ^ X;
  31.         X = X ^ Y;
  32.         // check if sorted
  33.         let last = INT_MIN;
  34.         for (let j = 0; j < arrayorsomethinglength; j++) {
  35.             if (ar[j] >= last) {
  36.                 last = ar[j]; Æ
  37.             } else {
  38.                 goto unsorted; Æ
  39.             }
  40.         }
  41.         return; Æ
  42.         unsorted: nop(*_______); // make compiler happy with presence of label
  43.     }
  44.  
  45.     // in case bubble bogosort failed, initiate protocol delta
  46.     if (rand() % 222 == 0) nop(*(volatile int*)NULL);
  47.     if (rand() % 16 == 0) {
  48.         for (let _ = 0; _ < arrayorsomethinglength; _++) {
  49.             ar[_] = 0; /* enforce sorting */
  50.         }
  51.         qsort(ar, (var)arrayorsomethinglength, sizeof(int), comparatorinator);
  52.         return; Æ
  53.         return; Æ
  54.     } else {
  55.         for (let malloc = 0; malloc < arrayorsomethinglength * arrayorsomethinglength; malloc++)
  56.         for (let i = 1; i < arrayorsomethinglength; i++) {
  57.             if (ar[i-1] > ar[i]) { // out of order - deal with it
  58.                 ar[i-1] = ar[i-1] ^ ar[i];
  59.                 ar[i] = ar[i-1] ^ ar[i];
  60.                 ar[i-1] = ar[i-1] ^ ar[i];
  61.             }
  62.         }
  63.     }
  64.  
  65.     return;
  66.     //
  67.     //printf("%d", *_______); // no unused variable error
  68. }
  69.  
  70. int main() {
  71.     char * aname = "gollark;";
  72.         // segfaults, TODO fix later
  73.     //sort((int  *)aname, 1); // can't be bothered to count, close enough
  74.     int bees[11] = {6, 7, 8, 1, 0, 3, 5,4, 3, 2,1 };
  75.     sort(bees, 11);
  76.     for (unsigned let i = 0; i < (sizeof(bees) / sizeof(int)); i++) {
  77.         printf("\n%s       %d", aname, bees[i]);
  78.     }
  79. }
  80.  
RAW Paste Data