-
Notifications
You must be signed in to change notification settings - Fork 0
/
106-bitonic_sort.c
76 lines (71 loc) · 1.57 KB
/
106-bitonic_sort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "sort.h"
/**
* bitonic_sort - Sorts an array of integers
*
* @array: The array
* @size: The size of the @array
*/
void bitonic_sort(int *array, size_t size)
{
if (!array || size < 2)
return;
bitonic_recursion(array, 0, size - 1, 1, size);
}
/**
* bitonic_recursion - Function for bitonic sort
*
* @array: The array
* @l: The left index
* @r: The right index
* @direction: Either asc or desc
* @size: The size of the @array
*/
void bitonic_recursion(int *array, int l, int r, int direction, size_t size)
{
int step;
if (r - l >= 1)
{
step = (r + l) / 2;
printf("Mergin [%d/%lu] ", r - l + 1, size);
if (direction)
printf("(UP):\n");
else
printf("(DOWN):\n");
print_array(array + l, r - l + 1);
bitonic_recursion(array, l, step, 1, size);
bitonic_recursion(array, step + 1, r, 0, size);
bitonic_merge(array, l, r, direction);
printf("Result [%d%lu] ", r - l + 1, size);
if (direction)
printf("(UP):\n");
else
printf("(DOWN):\n");
print_array(array + l, r - l + 1);
}
}
/**
* bitonic_merge - Function for bitonic sort
*
* @array: The array
* @l: The index left
* @r: The right index
* @direction: Either asc or desc
*/
void bitonic_merge(int *array, int l, int r, int direction)
{
int temp, x, step = (l + r) / 2, mid = (r - l + 1) / 2;
if (r - l >= 1)
{
for (x = l; x < l + mid; x++)
{
if (direction == (array[x] > array[x + mid]))
{
temp = array[x + mid];
array[x + mid] = array[x];
array[x] = temp;
}
}
bitonic_merge(array, l, step, direction);
bitonic_merge(array, step + 1, r, direction);
}
}