-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path100-shell_sort.c
More file actions
45 lines (39 loc) · 753 Bytes
/
100-shell_sort.c
File metadata and controls
45 lines (39 loc) · 753 Bytes
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
#include "sort.h"
/**
* _swap - swap between 2 pointers of int
*
* @a: first int pointer
* @b: second int pointer
* Return: void
*/
void _swap(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
/**
* shell_sort - calculate a gap and take 2 index to compare and swap
*
* @array: array of integers
* @size: size of the array
* Return: void
*/
void shell_sort(int *array, size_t size)
{
size_t gap = 0, i, j;
if (size <= 1)
return;
/* calculate the gap */
while ((3 * gap + 1) <= size)
gap = gap * 3 + 1;
while (gap > 0)
{
for (i = gap; i < size; i++)
for (j = i; j >= gap && array[j - gap] > array[j] ; j = j - gap)
_swap(&array[j], &array[j - gap]);
gap = (gap - 1) / 3;
print_array(array, size);
}
}