Commit 266b71d1 authored by Adam Blank's avatar Adam Blank
Browse files

Initial commit

parents
No related merge requests found
Showing with 165 additions and 0 deletions
+165 -0
main:
clang -g -fsanitize=address sort.c comparator.c main.c -o main
clean:
rm -f main
#include <string.h>
#include "comparator.h"
int int_asc(const int a, const int b) {
return a - b;
}
int int_desc(const int a, const int b) {
return b - a;
}
int int_p_asc(const int *a, const int *b) {
return *a - *b;
}
int int_p_desc(const int *a, const int *b) {
return *b - *a;
}
#ifndef __COMPARATOR_H
#define __COMPARATOR_H
#include <stddef.h>
typedef int (*IntComparator)(const int a, const int b);
int int_asc(const int a, const int b);
int int_desc(const int a, const int b);
typedef int (*StringComparator)(const char *a, const char *b);
typedef int (*Comparator)(const void *a, const void *b);
int int_p_asc(const int *a, const int *b);
int int_p_desc(const int *a, const int *b);
#endif /* __COMPARATOR_H */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sort.h"
void print_int_array(size_t length, int arr[]) {
for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
void print_int_p_array(size_t length, int *arr[]) {
for (int i = 0; i < length; i++) {
printf("%d ", *arr[i]);
}
printf("\n");
}
void print_str_array(size_t length, char *arr[]) {
for (int i = 0; i < length; i++) {
printf("%s ", arr[i]);
}
printf("\n");
}
const int INT_TEST_ARRAY[] = {15, 10, 20, 15, 5, 15, 100, 15, 15, 15, 200, 15, 150};
const char *STR_TEST_ARRAY[] = {"aaaa", "aa", "h", "aaaa", "a", "aaaa", "l", "d", "d", "d", "p", "d", "m"};
const size_t NUM_ELEMENTS = 13;
int *get_int_array() {
int *test_array = malloc(NUM_ELEMENTS * sizeof(int));
for (size_t i = 0; i < NUM_ELEMENTS; i++) {
test_array[i] = INT_TEST_ARRAY[i];
}
return test_array;
}
int **get_int_p_array() {
int **test_array = malloc(NUM_ELEMENTS * sizeof(int *));
for (size_t i = 0; i < NUM_ELEMENTS; i++) {
test_array[i] = (int *)&INT_TEST_ARRAY[i];
}
return test_array;
}
char **get_str_array() {
char **test_array = malloc(NUM_ELEMENTS * sizeof(char *));
for (size_t i = 0; i < NUM_ELEMENTS; i++) {
test_array[i] = (char *)STR_TEST_ARRAY[i];
}
return test_array;
}
#define RUN_FUNC(TYPE, FUNC, ...) do { \
printf(#FUNC"(" #__VA_ARGS__ "):\n ");\
TYPE *arr = get_ ## TYPE ## _array();\
print_ ## TYPE ## _array(NUM_ELEMENTS, arr);\
FUNC((void *)arr, __VA_ARGS__);\
printf(" ");\
print_ ## TYPE ## _array(NUM_ELEMENTS, arr);\
free(arr);\
} while(0);
typedef int* int_p;
typedef char* str;
int main(int argc, char *argv[]) {
RUN_FUNC(int, ascending_int_sort_whole, NUM_ELEMENTS);
RUN_FUNC(int, ascending_int_sort, 0, NUM_ELEMENTS);
RUN_FUNC(int, ascending_int_sort, 0, 2);
RUN_FUNC(int, ascending_int_sort, 0, 4);
RUN_FUNC(int, descending_int_sort, 0, NUM_ELEMENTS);
RUN_FUNC(int, descending_int_sort, 0, 2);
RUN_FUNC(int, descending_int_sort, 0, 4);
RUN_FUNC(int, int_sort, 0, NUM_ELEMENTS, int_asc);
RUN_FUNC(int, int_sort, 0, NUM_ELEMENTS, int_desc);
RUN_FUNC(str, string_sort, 0, NUM_ELEMENTS, strcmp);
RUN_FUNC(int_p, sort, 0, NUM_ELEMENTS, (Comparator)int_p_asc);
RUN_FUNC(int_p, sort, 0, NUM_ELEMENTS, (Comparator)int_p_desc);
RUN_FUNC(str, sort, 0, NUM_ELEMENTS, (Comparator)strcmp);
}
#include "sort.h"
void ascending_int_sort_whole(int arr[], size_t nelements) {
}
void ascending_int_sort(int arr[], size_t lo, size_t hi) {
}
void descending_int_sort(int arr[], size_t lo, size_t hi) {
}
void int_sort(int arr[], size_t lo, size_t hi, IntComparator compare) {
}
void string_sort(char *arr[], size_t lo, size_t hi, StringComparator compare) {
}
void sort(void *arr[], size_t lo, size_t hi, Comparator compare) {
}
#ifndef __SORT_H
#define __SORT_H
#include <stddef.h>
#include "comparator.h"
void ascending_int_sort_whole(int arr[], size_t nelements);
void ascending_int_sort(int arr[], size_t lo, size_t hi);
void descending_int_sort(int arr[], size_t lo, size_t hi);
void int_sort(int arr[], size_t lo, size_t hi, IntComparator compare);
void string_sort(char *arr[], size_t lo, size_t hi, StringComparator compare);
void sort(void *arr[], size_t lo, size_t hi, Comparator compare);
#endif /* __SORT_H */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment