-
Notifications
You must be signed in to change notification settings - Fork 0
/
100-shell_sort.c
94 lines (84 loc) · 1.82 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "sort.h"
void swap(int *x, int *y);
/**
* shell_sort - shell sort implementation using the knuth sequence
* Also known as increments sequence
* @a: array to be sorted
* @size: array size
*
* Return: void
*/
void shell_sort(int a[], size_t size)
{
int gap, i, j;
if (a == NULL || size < 2)
return;
/**
* Initial calculation of gap using Knuth sequence
* Also known as increments sequence
*/
for (gap = 1; gap <= (int)size / 3; gap = 3 * gap + 1)
;
/* For each pass of the first loop, gap reduces by half */
for (; gap >= 1; gap /= 3)
{
/* j steps through n using gap to reduce iter count */
for (j = gap; j < (int)size; j++)
/* i makes use of gap to swap values */
for (i = j - gap; i >= 0; i -= gap)
{
if (a[i + gap] >= a[i])
break; /* >= makes it a stable sort */
swap(&a[i + gap], &a[i]);
}
print_array(a, size);
}
}
/**
* shell_sort_gap_rdctn - shell sort with the gap reduction algorithm
* @A: the array to sort
* @n: array size
*
* Return: void
*/
void shell_sort_gap_rdctn(int A[], size_t n)
{
int gap, i, j, size;
size = (int)n;
/**
* Initial calculation of gap using the Gap Reduction technique
* There are other techniques as well such as the Knuth sequence
* demonstrated above
*/
gap = size / 2;
/* For each pass of the first loop, gap reduces by half */
for (; gap >= 1; gap /= 2)
{
/* j steps through n using gap to reduce iter count */
for (j = gap; j < size; j++)
{
/* i makes use of gap to swap values */
for (i = j - gap; i >= 0; i -= gap)
{
if (A[i + gap] > A[i])
break;
swap(&A[i + gap], &A[i]);
print_array(A, size);
}
}
}
}
/**
* swap - swaps two integer pointers
* @x: first int variable
* @y: second int variable
*
* Return: void
*/
void swap(int *x, int *y)
{
int tm;
tm = *x;
*x = *y;
*y = tm;
}