Skip to content

Instantly share code, notes, and snippets.

@leomastoras
Created April 14, 2024 09:36
Show Gist options
  • Save leomastoras/fb5366636d97692d2719b28a321102ac to your computer and use it in GitHub Desktop.
Save leomastoras/fb5366636d97692d2719b28a321102ac to your computer and use it in GitHub Desktop.
trollsort - sorts an array of n integers - (the troll way) Kappa Keepo
/*
* trollsort - sorts an array of n integers - (the troll way) Kappa Keepo
* ----------------------------------------------------------------------
* just some code i wrote for fun. i think complexity should be same with
* bubblesort O(n^2) altho im not sure. feel free to explore it yourself.
* obviously its easy to break it but runs ok for most cases.
* qs O(nlogn) coming soon...
* qs is now ready
* ----------------------------------------------------------------------
* WARNING!!! the is a million undefined behavior things in this code plz
* follow instructions to compile it correctly-ish
* ----------------------------------------------------------------------
* Leonidas Giannoulis - masterleo@gmail.com - http://cdev.ml/ - May 2015
*/
int trollsort(int A[], int n)<%
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;
%>
/*
* You can use this code to test it.
* Runs smooth on http://www.tutorialspoint.com/unix_terminal_online.php
* compile: cc -O2 -std=c99 trollsort.c -o trollsort
* example run: ./trollsort -1 1 69 0 -666 420 1337
*/
#include <stdio.h>
int print_array_ascending(int *A, int n)<%
return n&&printf("%d ",*A)|print_array_ascending(++A,--n)||putchar(0xA);
}
int main(int argc, char *argv[])<%
int n=--argc,A[n];
/* Load */
while(argc--&&sscanf(argv<:-~argc],"%d",A+argc));
/* Sort */
trollsort(A,n);
/* Print */
fputs("Sorted Result: ",stdout)|print_array_ascending(A,n);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment