// Header file where the generic magic happens
// Must run g++ -E to see what's happening
#include <time.h>
#define TOKEN(x, y) x ## y
#define QUICK_SORT(T) TOKEN(quick_sort_, T)
#define PARTITION(T) TOKEN(partition_, T)
#define PRINT_ARR(T) TOKEN(print_arr_, T)
#define CHOOSE(T) TOKEN(choose_type_, T)
int PARTITION (TYPE) (TYPE * nums, int lo, int hi){
TYPE pivot = nums[hi];
int i = lo; //Place for swapping
int j;
for (j = lo; j < hi; ++j){
if (nums[j] <= pivot){
TYPE temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
i = i + 1;
}
}
TYPE temp = nums[i];
nums[i] = nums[hi];
nums[hi] = temp;
return i;
}
TYPE* QUICK_SORT (TYPE) (TYPE * nums, int lo, int hi){
if (lo < hi){
int p = PARTITION (TYPE) (nums, lo, hi);
QUICK_SORT (TYPE) (nums, lo, p - 1);
QUICK_SORT (TYPE) (nums, p + 1, hi);
}
}
void PRINT_ARR (TYPE) (TYPE * nums, int size, int type){
for (int i = 0; i < size; i++){
PRINT ;
}
printf("%s", "\n");
}
void CHOOSE (TYPE) (int argc, char** argv, int type){
// Creates character array with 255 bytes
char buff[255];
char *end;
// Makes argv[2] lowercase
int size = strtol(argv[3], &end, 10);
TYPE nums[size];
// Declare and try to open file by name
FILE *f;
f = fopen(argv[1], "r");
int first = 0;
// Error block
if (f==NULL) perror("Error opening file");
else {
while (!feof(f)) {
//Doesn't print empty buff
if (first != 0){
nums[first-1] = READ ;
}
fscanf(f, "%s", buff);
first = first + 1;
}
}
//PRINT_ARR (TYPE) (nums, size, type);
time_t tstart, tend;
tstart = clock();
QUICK_SORT (TYPE) (nums, 0, size-1);
tend=clock();
long t = (tend-tstart); // CLOCKS_PER_SEC;
long C = CLOCKS_PER_SEC;
printf("%Ld\n", t);
printf("CLOCKS_PER_SEC: %Ld\n", C);
//PRINT_ARR (TYPE) (nums, size, type);
}
Comments