Created
August 4, 2009 02:43
-
-
Save pi8027/160994 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#include <stdio.h> | |
#include <stddef.h> | |
#define GENERATE_SORTING_FUNCTION(function_name,data_type) \ | |
void function_name(data_type *base \ | |
,const size_t size,int (*compare)(const data_type,const data_type)) \ | |
{ \ | |
size_t pivot_pos = 0,first2last = 0,last2first = size-1; \ | |
data_type temp,pivot; \ | |
while(pivot_pos+1 != size && !compare(base[pivot_pos],base[pivot_pos+1])){ \ | |
pivot_pos++; \ | |
} \ | |
if(pivot_pos+1 == size){ \ | |
return; \ | |
} \ | |
pivot = base[pivot_pos+(0 > compare(base[pivot_pos],base[pivot_pos+1]))]; \ | |
while(first2last < last2first){ \ | |
while(0 < compare(pivot,base[first2last]) && size-1 != first2last){ \ | |
first2last++; \ | |
} \ | |
while(0 >= compare(pivot,base[last2first]) && last2first){ \ | |
last2first--; \ | |
} \ | |
if(first2last < last2first){ \ | |
temp = base[first2last]; \ | |
base[first2last] = base[last2first]; \ | |
base[last2first] = temp; \ | |
} \ | |
} \ | |
function_name(base,first2last,compare); \ | |
function_name(&base[first2last],size-first2last,compare); \ | |
} | |
GENERATE_SORTING_FUNCTION(sort,int); | |
int compare_integer(int a,int b) | |
{ | |
return a-b; | |
} | |
int main(void) | |
{ | |
int array[32]; | |
size_t counter = 0; | |
sort(array,32,compare_integer); | |
while(counter != 32){ | |
printf("%d\n",array[counter]); | |
counter++; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment