-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path100-shell_sort.c
More file actions
49 lines (43 loc) · 799 Bytes
/
100-shell_sort.c
File metadata and controls
49 lines (43 loc) · 799 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
#include "sort.h"
/**
* shell_sort - Function to sort array int w/ shell sort algorithm
* @array: The array
* @size: The array size
*
* Return: None
*/
void shell_sort(int *array, size_t size)
{
size_t gap, i, j;
if (array == NULL || size < 2)
return;
for (gap = 1; gap < (size / 3);)
gap = gap * 3 + 1;
for (; gap >= 1; gap /= 3)
{
for (i = gap; i < size; i++)
{
j = i;
while (j >= gap && array[j - gap] > array[j])
{
swap(array + j, array + (j - gap));
j -= gap;
}
}
print_array(array, size);
}
}
/**
* swap - Function to swap position of 2 elements in list
* @first: Fist element
* @second: Second element
*
* Return: None
*/
void swap(int *first, int *second)
{
int tmp_elem;
tmp_elem = *first;
*first = *second;
*second = tmp_elem;
}