-
Notifications
You must be signed in to change notification settings - Fork 0
/
100-shell_sort.c
50 lines (44 loc) · 883 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
50
#include "sort.h"
/**
* swap_ints - Swap two integers in an array.
* @a: The first integer to swap.
* @b: The second integer to swap.
* Rturn: NULL.
*/
void swap_ints(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
/**
* shell_sort - Sort an array of integers in ascending
* order using the shell sort algorithm.
* @array: An array of integers.
* @size: The size of the array.
*
* Description: Uses the Knuth interval sequence.
* Return: Void.
*/
void shell_sort(int *array, size_t size)
{
size_t gap, a, b;
if (array == NULL || size < 2)
return;
for (gap = 1; gap < (size / 3);)
gap = gap * 3 + 1;
for (; gap >= 1; gap /= 3)
{
for (a = gap; a < size; a++)
{
b = a;
while (b >= gap && array[b - gap] > array[b])
{
swap_ints(array + b, array + (b - gap));
b -= gap;
}
}
print_array(array, size);
}
}