-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-quick_sort.c
executable file
·78 lines (66 loc) · 1.19 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
77
78
#include "sort.h"
/**
* quick_sort - ...
* @array: ...
* @size: ...
*
* Return: Nothing!
*/
void quick_sort(int *array, size_t size)
{
if (!array || size < 2)
return;
quick_sort_rec(array, 0, size - 1, size);
}
/**
* quick_sort_rec - ...
* @array: ...
* @lower: ...
* @higher: ...
* @size: ...
*
* Return: Nothing!
*/
void quick_sort_rec(int *array, int lower, int higher, size_t size)
{
int l_p = 0;
if (lower < higher)
{
l_p = lomuto_partition(array, lower, higher, size);
quick_sort_rec(array, lower, l_p - 1, size);
quick_sort_rec(array, l_p + 1, higher, size);
}
}
/**
* lomuto_partition - ...
* @array: ...
* @lower: ...
* @higher: ...
* @size: ...
*
* Return: Nothing!
*/
int lomuto_partition(int *array, int lower, int higher, size_t size)
{
int i = 0, j = 0, pivot = 0, aux = 0;
pivot = array[higher];
i = lower;
for (j = lower; j < higher; ++j)
{
if (array[j] < pivot)
{
aux = array[i];
array[i] = array[j];
array[j] = aux;
if (aux != array[i])
print_array(array, size);
++i;
}
}
aux = array[i];
array[i] = array[higher];
array[higher] = aux;
if (aux != array[i])
print_array(array, size);
return (i);
}