-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path100-shell_sort.c
More file actions
47 lines (41 loc) · 802 Bytes
/
100-shell_sort.c
File metadata and controls
47 lines (41 loc) · 802 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
46
47
#include "sort.h"
/**
* swap_nbrs - Swap two numbers in an array.
* @x: first number to swap.
* @y: second number to swap.
*/
void swap_nbrs(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
/**
* shell_sort - Sort an array of numbers in ascending order
* @array: An array of numbers.
* @size: size of array.
*
* Description: Uses Knuth interval sequence.
*/
void shell_sort(int *array, size_t size)
{
size_t gapp, x, y;
if (array == NULL || size < 2)
return;
for (gapp = 1; gapp < (size / 3);)
gapp = gapp * 3 + 1;
for (; gapp >= 1; gapp /= 3)
{
for (x = gapp; x < size; x++)
{
y = x;
while (y >= gapp && array[y - gapp] > array[y])
{
swap_nbrs(array + y, array + (y - gapp));
y -= gapp;
}
}
print_array(array, size);
}
}