-
Notifications
You must be signed in to change notification settings - Fork 0
/
3-quick_sort.c
50 lines (43 loc) · 931 Bytes
/
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
#include <stdio.h>
#include "sort.h"
/**
* quick_sort - wrapper to call quickSort to sort an array
* @array: array to be sorted
* @size: size of array
*/
void quick_sort(int *array, size_t size)
{
if (array == NULL || size <= 1)
return;
recursive(array, size, 0, size - 1);
}
/**
* recursive - recursively sorts an array
* @array: array to sort
* @size: size of the array
* @start: starting point
* @end: ending point
*/
void recursive(int *array, size_t size, ssize_t start, ssize_t end)
{
int index, tmp, pivot, first = start - 1;
if (start >= end)
return;
pivot = array[end];
for (index = start; index <= end; index++)
{
if (array[index] <= pivot)
{
first++;
tmp = array[first];
array[first] = array[index];
array[index] = tmp;
if (index != first)
{
print_array(array, size);
}
}
}
recursive(array, size, start, first - 1);
recursive(array, size, first + 1, end);
}