Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Sort.h
- // Sort
- //
- // Created by me on 1/14/13.
- // Copyright (c) 2013 me. All rights reserved.
- //
- #ifndef Sort_Sort_h
- #define Sort_Sort_h
- #define CREATE_SORT_FUNC_WITH_TYPE(__TYPE__, __FUNC_DIRECTION__) \
- static void sort_##__TYPE___##__FUNC_DIRECTION__(__TYPE__ *array, u_int32_t count) { \
- u_int32_t i = 0; \
- u_int32_t j = 0; \
- __TYPE__ temp = 0; \
- for (i = 0; i < count; i++) { \
- for (j = 0; j < i; j++) { \
- if (_compare_##__FUNC_TYPE___##__FUNC_DIRECTION__(array[i], array[j])) { \
- temp = array[i]; \
- array[i] = array[j]; \
- array[j] = temp; \
- } \
- } \
- } \
- }
- #define DEFINE_COMPARE_FUNC(__FUNC_TYPE__, __FUNC_DIRECTION__, a, b) static inline bool _compare_##__FUNC_TYPE___##__FUNC_DIRECTION__(__FUNC_TYPE__ a, __FUNC_TYPE__ b)
- #define SORT(__TYPE__, __FUNC_DIRECTION__, array, count) sort_##__TYPE___##__FUNC_DIRECTION__(array, count)
- #endif
- //
- // main.c
- // Sort
- //
- // Created by me on 1/14/13.
- // Copyright (c) 2013 me. All rights reserved.
- //
- #include <stdlib.h>
- #include <stdio.h>
- #include <stdbool.h>
- #include <time.h>
- #include "Sort.h"
- DEFINE_COMPARE_FUNC(int, ascending, a, b) {
- return (a < b);
- }
- CREATE_SORT_FUNC_WITH_TYPE(int, ascending)
- int main(int argc, const char * argv[]) {
- u_int32_t count = 10;
- int *array = malloc(count * sizeof(*array));
- srand((unsigned int)time(NULL));
- for (u_int32_t i = 0; i < count; i++) {
- array[i] = rand();
- }
- SORT(int, ascending, array, count);
- for (u_int32_t i = 0; i < count; i++) {
- printf("%d\n", array[i]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement