forked from Biyuktul/sorting_algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path100-shell_sort.c
More file actions
40 lines (34 loc) · 764 Bytes
/
Copy path100-shell_sort.c
File metadata and controls
40 lines (34 loc) · 764 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
#include "sort.h"
#include <stdio.h>
/**
* shell_sort - Sorts an array of integers using the Shell Sort algorithm.
* @array: Pointer to the first element of the array.
* @size: Number of elements in the array.
* Returns: void
*/
void shell_sort(int *array, size_t size)
{
int tmp;
size_t j, i, increment;
if (!array || size == 1)
return;
increment = 1;
while (increment <= size / 3)
{
increment = increment * 3 + 1;
}
while (increment > 0)
{
for (i = increment; i < size; i++)
{
tmp = array[i];
for (j = i; j >= increment && tmp < array[j - increment]; j -= increment)
{
array[j] = array[j - increment];
}
array[j] = tmp;
}
print_array(array, size);
increment = (increment == 1) ? 0 : (increment - 1) / 3;
}
}