-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path106-bitonic_sort.c
More file actions
93 lines (89 loc) · 1.95 KB
/
106-bitonic_sort.c
File metadata and controls
93 lines (89 loc) · 1.95 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "sort.h"
#include <stdio.h>
/**
* swap - change two values in ascending or descending order
* @arr: array
* @item1: item one
* @item2: item two
* @order: 1: ascending order, 0 descending order
*/
void swap(int arr[], int item1, int item2, int order)
{
int temp;
if (order == (arr[item1] > arr[item2]))
{
temp = arr[item1];
arr[item1] = arr[item2];
arr[item2] = temp;
}
}
/**
* merge - sort bitonic sequences recursively in both orders
* @arr: array
* @low: first element
* @nelemnt: elements number
* @order: 1: ascending order, 0 descending order
*/
void merge(int arr[], int low, int nelemnt, int order)
{
int mid, i;
if (nelemnt > 1)
{
mid = nelemnt / 2;
for (i = low; i < low + mid; i++)
swap(arr, i, i + mid, order);
merge(arr, low, mid, order);
merge(arr, low + mid, mid, order);
}
}
/**
* bitonicsort - bitonic sort algorithm implementation
* @arr: array
* @low: first element
* @nelemnt: number of elements
* @order: 1: ascending order, 0 descending order
* @size: array lenght
*/
void bitonicsort(int arr[], int low, int nelemnt, int order, int size)
{
int mid;
if (nelemnt > 1)
{
if (order >= 1)
{
printf("Merging [%i/%i] (UP):\n", nelemnt, size);
print_array(&arr[low], nelemnt);
}
else
{
printf("Merging [%i/%i] (DOWN):\n", nelemnt, size);
print_array(&arr[low], nelemnt);
}
mid = nelemnt / 2;
bitonicsort(arr, low, mid, 1, size);
bitonicsort(arr, low + mid, mid, 0, size);
merge(arr, low, nelemnt, order);
if (order <= 0)
{
printf("Result [%i/%i] (DOWN):\n", nelemnt, size);
print_array(&arr[low], nelemnt);
}
if (order >= 1)
{
printf("Result [%i/%i] (UP):\n", nelemnt, size);
print_array(&arr[low], nelemnt);
}
}
}
/**
* bitonic_sort - prepare the terrain to bitonic sort algorithm
* @array: array
* @size: array lenght
*/
void bitonic_sort(int *array, size_t size)
{
int order = 1;
if (!array || size < 2)
return;
bitonicsort(array, 0, size, order, size);
}