-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-quick_sort.c
76 lines (66 loc) · 1 KB
/
3-quick_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
#include "sort.h"
/**
* partition - Lomutu partition scheme for quicksort algorithm
* @a: Array to sort
* @l: lowest index of array
* @h: highest index of array
* Return: index of pivot
*/
int partition(int *a, int l, int h)
{
int p, i, j, t;
static int size, k;
if (k == 0)
size = h + 1, k++;
p = a[h];
i = l;
for (j = l; j < h; j++)
{
if (a[j] <= p)
{
if (i != j)
{
t = a[i];
a[i] = a[j];
a[j] = t;
print_array(a, size);
}
i++;
}
}
if (i != h)
{
t = a[i];
a[i] = a[h];
a[h] = t;
print_array(a, size);
}
return (i);
}
/**
* qs - Quicksort recurssive function
* @a: array to sort
* @l: lowest index
* @h: highest index
*/
void qs(int *a, int l, int h)
{
int p;
if (l < h)
{
p = partition(a, l, h);
qs(a, l, p - 1);
qs(a, p + 1, h);
}
}
/**
* quick_sort - sorts array using quicksort algorithm
* @array: Array to sort
* @size: Size of array to sort
*/
void quick_sort(int *array, size_t size)
{
if (array == NULL)
return;
qs(array, 0, size - 1);
}