diff options
author | Julian T <julian@jtle.dk> | 2020-10-12 16:26:42 +0200 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2020-10-12 16:54:48 +0200 |
commit | 4e05a55e373bd315e721d534a1711fec4c0054c5 (patch) | |
tree | 4fc0141760c2730ed79aaa4c7aa60ea45cc66cc0 /sem3/algo/lek1 | |
parent | b7f9cf43c8a9ab3400cbb30d5e1cadb0c6c2cf23 (diff) |
Moved to linux kernel inspired clang-format
Diffstat (limited to 'sem3/algo/lek1')
-rw-r--r-- | sem3/algo/lek1/merge.c | 151 |
1 files changed, 74 insertions, 77 deletions
diff --git a/sem3/algo/lek1/merge.c b/sem3/algo/lek1/merge.c index 945b86a..f2d908a 100644 --- a/sem3/algo/lek1/merge.c +++ b/sem3/algo/lek1/merge.c @@ -8,114 +8,111 @@ void printArr(int *arr, size_t len) { - printf("{"); - for (int i = 0; i < len; i++) { - printf(" %d%s", arr[i], i+1 == len ? " " : ","); - } - printf("}\n"); + printf("{"); + for (int i = 0; i < len; i++) { + printf(" %d%s", arr[i], i + 1 == len ? " " : ","); + } + printf("}\n"); } int *genArr(size_t len, int div) { - /* Make array */ - int *arr = (int *) malloc(len * sizeof(int)); + /* Make array */ + int *arr = (int *)malloc(len * sizeof(int)); - if (arr == NULL) { - return NULL; - } + if (arr == NULL) { + return NULL; + } - /* Fill it with random */ - while (len--) { - arr[len] = div - (rand() % (div*2)); - } - - return arr; + /* Fill it with random */ + while (len--) { + arr[len] = div - (rand() % (div * 2)); + } + return arr; } int mergeSort(int *arr, size_t len) { - /* Check if len is one(then it's sorted */ - if (len <= 1) { - return 0; - } - /* Calculate array sizes */ - size_t q = len/2; - size_t rl = len-q; - - /* Make arrays */ - int *left = (int *) malloc(q * sizeof(int)); - if (left == NULL) { - return 1; - } - int *right = (int *) malloc(rl * sizeof(int)); - if (right == NULL) { - return 1; - } - - /* Copy data */ - memcpy(left, arr, q * sizeof(int)); - memcpy(right, arr + q, rl * sizeof(int)); - - /* Sort each */ - mergeSort(left, q); - mergeSort(right, rl); - - /* Merge them into arr */ - for (int i=0, j=0, k = 0; k < len; k++) { - if ( i < q && ( j == rl || left[i] <= right[j] ) ) { - arr[k] = left[ i++ ]; - } else { - arr[k] = right[ j++ ]; - } - } - - /* Free the pointers */ - free(left); - free(right); - - return 0; + /* Check if len is one(then it's sorted */ + if (len <= 1) { + return 0; + } + /* Calculate array sizes */ + size_t q = len / 2; + size_t rl = len - q; + + /* Make arrays */ + int *left = (int *)malloc(q * sizeof(int)); + if (left == NULL) { + return 1; + } + int *right = (int *)malloc(rl * sizeof(int)); + if (right == NULL) { + return 1; + } + + /* Copy data */ + memcpy(left, arr, q * sizeof(int)); + memcpy(right, arr + q, rl * sizeof(int)); + + /* Sort each */ + mergeSort(left, q); + mergeSort(right, rl); + + /* Merge them into arr */ + for (int i = 0, j = 0, k = 0; k < len; k++) { + if (i < q && (j == rl || left[i] <= right[j])) { + arr[k] = left[i++]; + } else { + arr[k] = right[j++]; + } + } + + /* Free the pointers */ + free(left); + free(right); + + return 0; } -void test(size_t len) +void test(size_t len) { - - int *a = genArr(len, len); + int *a = genArr(len, len); #if DO_PRINT == 1 - printArr(a, len); - printf("\n\n"); + printArr(a, len); + printf("\n\n"); #endif - clock_t begin = clock(); - mergeSort(a, len); - clock_t end = clock(); + clock_t begin = clock(); + mergeSort(a, len); + clock_t end = clock(); #if DO_PRINT == 1 - printArr(a, len); + printArr(a, len); #endif - clock_t diff = end - begin; - printf("Sorting %d long array took %d : %f s\n", len, diff, (double)diff/CLOCKS_PER_SEC); + clock_t diff = end - begin; + printf("Sorting %d long array took %d : %f s\n", len, diff, + (double)diff / CLOCKS_PER_SEC); - free(a); + free(a); } void bench() { + size_t len = SIZE_MAX; - size_t len = SIZE_MAX; - - srand((unsigned) time(NULL)); - for (int i = 1; i < len; i *= 2) { - test(i); - } + srand((unsigned)time(NULL)); + for (int i = 1; i < len; i *= 2) { + test(i); + } } int main(void) { + bench(); - bench(); - - return 0; + return 0; } |