-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_quick_sort.c
54 lines (48 loc) · 1.48 KB
/
ft_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_quick_sort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yochered <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/10/30 15:01:25 by yochered #+# #+# */
/* Updated: 2018/10/30 15:01:26 by yochered ### ########.fr */
/* */
/* ************************************************************************** */
static void ft_swap(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
static int partition(int *tab, int low, int high)
{
int pivot;
int i;
int j;
pivot = tab[high];
j = low;
i = low - 1;
while (j <= high - 1)
{
if (tab[j] <= pivot)
{
i++;
ft_swap(&tab[i], &tab[j]);
}
j++;
}
ft_swap(&tab[i + 1], &tab[high]);
return (i + 1);
}
void ft_quick_sort(int **tab, int low, int high)
{
int pivot;
if (low < high)
{
pivot = partition(*tab, low, high);
ft_quick_sort(tab, low, pivot - 1);
ft_quick_sort(tab, pivot + 1, high);
}
}