Advertisement
leomaster

TrollSort

May 15th, 2015
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.61 KB | None | 0 0
  1. /*
  2.  * trollsort - sorts an array of n integers - (the troll way) Kappa Keepo
  3.  * ----------------------------------------------------------------------
  4.  * just some code i wrote for fun. i think complexity should be same with
  5.  * bubblesort O(n^2) altho im not sure. feel free to explore it yourself.
  6.  * obviously its easy to break it but runs ok for most cases.
  7.  * kappasort O(nlogn) coming soon...
  8.  * ----------------------------------------------------------------------
  9.  * WARNING!!! the is a million undefined behavior things in this code plz
  10.  * follow instructions to compile it correctly-ish
  11.  * ----------------------------------------------------------------------
  12.  * Leonidas Giannoulis - masterleo@gmail.com - http://cdev.ml/ - May 2015
  13.  */
  14.  
  15.  
  16.  
  17.  
  18. int trollsort(int A[], int n)<%
  19.     return n?n<~n?n=-n,trollsort(A,-~-n),A[~-n:>>n<:A]&&(n[A]^=A[~-n]^=n[A]^=A[~-n]):trollsort(A,-~-n)|trollsort(A,~-n):420;
  20. %>
  21.  
  22.  
  23.  
  24.  
  25. /*
  26.  * You can use this code to test it.
  27.  * Runs smooth on http://www.tutorialspoint.com/unix_terminal_online.php
  28.  * compile: cc -O2 -std=c99 trollsort.c -o trollsort
  29.  * example run: ./trollsort -1 1 69 0 -666 420 1337
  30.  */
  31.  
  32. #include <stdio.h>
  33.  
  34. int print_array_ascending(int *A, int n)<%
  35.     return n&&printf("%d ",*A)|print_array_ascending(++A,--n)||putchar(0xA);
  36. }
  37.  
  38. int main(int argc, char *argv[])<%
  39.  
  40.         int n=--argc,A[n];
  41.        
  42.         /* Load */
  43.         while(argc--&&sscanf(argv<:-~argc],"%d",A+argc));
  44.  
  45.         /* Sort */
  46.         trollsort(A,n);
  47.        
  48.         /* Print */
  49.         fputs("Sorted Result: ",stdout)|print_array_ascending(A,n);
  50.        
  51.         return 0;
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement