|
| 1 | +/* |
| 2 | + * File: 106-bitonic_sort.c |
| 3 | + * Auth: Brennan D Baraban |
| 4 | + */ |
| 5 | + |
| 6 | +#include "sort.h" |
| 7 | + |
| 8 | +void swap_ints(int *a, int *b); |
| 9 | +void bitonic_merge(int *array, size_t size, size_t start, size_t seq, |
| 10 | + char flow); |
| 11 | +void bitonic_seq(int *array, size_t size, size_t start, size_t seq, char flow); |
| 12 | +void bitonic_sort(int *array, size_t size); |
| 13 | + |
| 14 | +/** |
| 15 | + * swap_ints - Swap two integers in an array. |
| 16 | + * @a: The first integer to swap. |
| 17 | + * @b: The second integer to swap. |
| 18 | + */ |
| 19 | +void swap_ints(int *a, int *b) |
| 20 | +{ |
| 21 | + int tmp; |
| 22 | + |
| 23 | + tmp = *a; |
| 24 | + *a = *b; |
| 25 | + *b = tmp; |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * bitonic_merge - Sort a bitonic sequence inside an array of integers. |
| 30 | + * @array: An array of integers. |
| 31 | + * @size: The size of the array. |
| 32 | + * @start: The starting index of the sequence in array to sort. |
| 33 | + * @seq: The size of the sequence to sort. |
| 34 | + * @flow: The direction to sort in. |
| 35 | + */ |
| 36 | +void bitonic_merge(int *array, size_t size, size_t start, size_t seq, |
| 37 | + char flow) |
| 38 | +{ |
| 39 | + size_t i, jump = seq / 2; |
| 40 | + |
| 41 | + if (seq > 1) |
| 42 | + { |
| 43 | + for (i = start; i < start + jump; i++) |
| 44 | + { |
| 45 | + if ((flow == UP && array[i] > array[i + jump]) || |
| 46 | + (flow == DOWN && array[i] < array[i + jump])) |
| 47 | + swap_ints(array + i, array + i + jump); |
| 48 | + } |
| 49 | + bitonic_merge(array, size, start, jump, flow); |
| 50 | + bitonic_merge(array, size, start + jump, jump, flow); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * bitonic_seq - Convert an array of integers into a bitonic sequence. |
| 56 | + * @array: An array of integers. |
| 57 | + * @size: The size of the array. |
| 58 | + * @start: The starting index of a block of the building bitonic sequence. |
| 59 | + * @seq: The size of a block of the building bitonic sequence. |
| 60 | + * @flow: The direction to sort the bitonic sequence block in. |
| 61 | + */ |
| 62 | +void bitonic_seq(int *array, size_t size, size_t start, size_t seq, char flow) |
| 63 | +{ |
| 64 | + size_t cut = seq / 2; |
| 65 | + char *str = (flow == UP) ? "UP" : "DOWN"; |
| 66 | + |
| 67 | + if (seq > 1) |
| 68 | + { |
| 69 | + printf("Merging [%lu/%lu] (%s):\n", seq, size, str); |
| 70 | + print_array(array + start, seq); |
| 71 | + |
| 72 | + bitonic_seq(array, size, start, cut, UP); |
| 73 | + bitonic_seq(array, size, start + cut, cut, DOWN); |
| 74 | + bitonic_merge(array, size, start, seq, flow); |
| 75 | + |
| 76 | + printf("Result [%lu/%lu] (%s):\n", seq, size, str); |
| 77 | + print_array(array + start, seq); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * bitonic_sort - Sort an array of integers in ascending |
| 83 | + * order using the bitonic sort algorithm. |
| 84 | + * @array: An array of integers. |
| 85 | + * @size: The size of the array. |
| 86 | + * |
| 87 | + * Description: Prints the array after each swap. Only works for |
| 88 | + * size = 2^k where k >= 0 (ie. size equal to powers of 2). |
| 89 | + */ |
| 90 | +void bitonic_sort(int *array, size_t size) |
| 91 | +{ |
| 92 | + if (array == NULL || size < 2) |
| 93 | + return; |
| 94 | + |
| 95 | + bitonic_seq(array, size, 0, size, UP); |
| 96 | +} |
0 commit comments