-
-
Save leomastoras/063787bf54d563ddc92c3ea261d649b7 to your computer and use it in GitHub Desktop.
Quicksort oneliner in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* code by fb.me/masterleo | |
* di.uoa.gr 10/08/2015 | |
* gcc -O3 -o qs qs.c | |
* | |
* QuickSort Brainfuck | |
* >>+>>>>>,[>+>>,]>+[--[+<<<-]<[<+>-]<[<[->[<<<+>>>>+<-]<<[>>+>[->]<<[<] | |
* <-]>]>>>+<[[-]<[>+<-]<]>[[>>>]+<<<-<[<<[<<<]>>+>[>>>]<-]<<[<<<]>[>>[>> | |
* >]<+<<[<<<]>-]]+<<<]+[->>>]>>]>>[.>>>] | |
*/ | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <time.h> | |
#define N 100 | |
qs(int *a,int o,int n) <% | |
return n<~n?n<:a:><*a&&a[n:>!=a<:++o]&&(a[n]^=a[o]^=a[n]^=a[o]),qs(a,o,-~n):n^n<~-n?o=n+qs(a+~-n,-n,-~-n),o<:a]!=a[~-n:>&&(a[o]^=a[~-n]^=a[o]^=a[~-n]),qs(a,o-o,o),qs(a+-~o,n%n,~-n-o):o; | |
%> | |
main() { | |
int n=N, *a = malloc (N * sizeof (int)); | |
srand(time(NULL)); | |
while (n--) printf("%d ", a[n] = rand() % N); | |
puts(""); | |
clock_t t1 = clock(); | |
qs(a, 0, n=N); | |
clock_t t2 = clock(); | |
puts(""); | |
while (n--) printf("%d ",*a++); | |
printf("\n%f seconds\n", ((double)(t2 - t1)) / CLOCKS_PER_SEC ); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment