-
Notifications
You must be signed in to change notification settings - Fork 0
/
3-quick_sort.c
46 lines (40 loc) · 1.03 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
#include "sort.h"
void print_quick_sort(int *array, size_t size, int *o_array, size_t o_size);
/**
* print_quick_sort - This function sorts an array of integers
* in ascending order and print for each swap
* @array: List of numbers
* @size: Size of array
* @o_array: Original list of numbers
* @o_size: Original size of array
*/
void print_quick_sort(int *array, size_t size, int *o_array, size_t o_size)
{
unsigned int i = 0, j = 0;
int p, n;
if (!array || size <= 1)
return;
p = array[size - 1];
for (j = 0; j < size; j++)
if (array[j] <= p || j == size - 1)
{
n = array[i];
array[i] = array[j];
array[j] = n;
if (array[i] != array[j])
print_array(o_array, o_size);
i++;
}
print_quick_sort(array, i - 1, o_array, o_size);
print_quick_sort(array + i, size - i, o_array, o_size);
}
/**
* quick_sort - This function sorts an array of integers
* in ascending order
* @array: List of numbers
* @size: Size of array
*/
void quick_sort(int *array, size_t size)
{
print_quick_sort(array, size, array, size);
}