-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path100-shell_sort.c
More file actions
51 lines (48 loc) · 832 Bytes
/
100-shell_sort.c
File metadata and controls
51 lines (48 loc) · 832 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
48
49
50
51
#include "sort.h"
/**
* shell_sort - sort and array
* @array: pointer to the array
* @size: size of the array
* Return: no return
*/
void shell_sort(int *array, size_t size)
{
unsigned int gap = 1, i = 0, j = 0, n = 0;
int temp = 0, x = 0;
if (!array || !size || size == 1)
return;
while (gap < size / 3)
{
gap = gap * 3 + 1;
n = n + 1;
}
for (i = gap; i > 0;)
{
temp = array[i];
for (j = 0; j < size - gap;)
{
if (array[j] > temp)
{
array[gap + j] = array[j];
array[j] = temp;
temp = array[j];
x = j - gap;
while (x >= 0)
{
if (array[j - gap] > array[j])
{
array[j] = array[j - gap];
array[j - gap] = temp;
}
j--;
x = j - gap;
}
}
j++;
temp = array[gap + j];
}
i = gap / 3;
gap = i;
print_array(array, size);
}
}