-
Notifications
You must be signed in to change notification settings - Fork 0
/
100-shell_sort.c
49 lines (42 loc) · 973 Bytes
/
100-shell_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
#include "sort.h"
/**
* faviswap_ints - Swaps integers in an array
* @a: first integer
* @b: second integer
*/
void faviswap_ints(int *a, int *b)
{
int temp_value;
temp_value = *a;
*a = *b;
*b = temp_value;
}
/**
* shell_sort - Sorry arrays of integers from the
* smallest to the biggest using the shell
* sort alogrithm
* @array: Array of integers
* @size: size of the arra
*/
void shell_sort(int *array, size_t size)
{
size_t interval, currentIndex, innerIndex;
if (array == NULL || size < 2)
return;
for (interval = 1; interval < (size / 3);)
interval = interval * 3 + 1;
for (; interval >= 1; interval /= 3)
{
for (currentIndex = interval; currentIndex < size; currentIndex++)
{
innerIndex = currentIndex;
while (innerIndex >= interval && array[innerIndex - interval] >
array[innerIndex])
{
faviswap_ints(array + innerIndex, array + (innerIndex - interval));
innerIndex -= interval;
}
}
print_array(array, size);
}
}