Created
September 25, 2018 01:22
-
-
Save The0x539/603a0d5f1d5cfb1c07a813a79c5b90e8 to your computer and use it in GitHub Desktop.
now featuring arbitrary value support
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
#include <stdlib.h> | |
#include <string.h> | |
typedef unsigned int uint; | |
static void split_array(void *array[], uint len, void ***left, uint *llen, void ***right, uint *rlen) { | |
*llen = *rlen = len / 2; | |
if (*llen + *rlen < len) { | |
*llen = *llen + 1; | |
} | |
size_t lsize = *llen * sizeof (void *); | |
size_t rsize = *rlen * sizeof (void *); | |
*left = malloc(lsize); | |
*right = malloc(rsize); | |
memcpy(*left, array, lsize); | |
memcpy(*right, array + *llen, rsize); | |
} | |
static void **merge_arrays(void *left[], uint llen, void *right[], uint rlen, int (*compare)(void *, void *)) { | |
void **merged; | |
merged = malloc((llen + rlen) * sizeof (void *)); | |
uint idx = 0; | |
while (llen > 0 && rlen > 0) { | |
void *a = left[0]; | |
void *b = right[0]; | |
if (compare(a, b) <= 0) { | |
merged[idx++] = a; | |
left++; | |
llen--; | |
} else { | |
merged[idx++] = b; | |
right++; | |
rlen--; | |
} | |
} | |
if (llen > 0) { | |
memcpy(merged + idx, left, llen * sizeof (void *)); | |
} else if (rlen > 0) { | |
memcpy(merged + idx, right, rlen * sizeof (void *)); | |
} | |
return merged; | |
} | |
void **sort_array(void *array[], uint len, int (*compare)(void *, void *)) { | |
void **sorted; | |
if (len == 1) { | |
sorted = malloc(sizeof (void *)); | |
sorted[0] = array[0]; | |
} else if (len == 2) { | |
sorted = malloc(2 * sizeof (void *)); | |
void *a = array[0]; | |
void *b = array[1]; | |
if (compare(a, b) <= 0) { | |
sorted[0] = a; | |
sorted[1] = b; | |
} else { | |
sorted[0] = b; | |
sorted[1] = a; | |
} | |
} else { | |
uint llen, rlen; | |
void **left, **right; | |
split_array(array, len, &left, &llen, &right, &rlen); | |
void **s_left = sort_array(left, llen, compare); | |
void **s_right = sort_array(right, rlen, compare); | |
free(left); | |
free(right); | |
sorted = merge_arrays(s_left, llen, s_right, rlen, compare); | |
free(s_left); | |
free(s_right); | |
} | |
return sorted; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment