-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path100-shell_sort.c
47 lines (41 loc) · 908 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
#include "sort.h"
/**
* swap_ints - Swap two integers in an array.
* @a: The first integer to swap.
* @b: The second integer to swap.
*/
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.
*/
void shell_sort(int *array, size_t size)
{
size_t interval, i, j;
if (array == NULL || size < 2)
return;
for (interval = 1; interval < (size / 3);)
interval = interval * 3 + 1;
for (; interval >= 1; interval /= 3)
{
for (i = interval; i < size; i++)
{
j = i;
while (j >= interval && array[j - interval] > array[j])
{
swap_ints(array + j, array + (j - interval));
j -= interval;
}
}
print_array(array, size);
}
}