-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-quick_sort.c
79 lines (74 loc) · 1.51 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
79
#include "sort.h"
/**
* swap - swaps 2 ints
* @a: the first int
* @b: the second int
* Return: void
*/
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
/**
* partition - splits the array to 2 parts, where pivot is in the middle
* @array: the array to sort
* @size: the size of the array
* @low: the lowest index in the sub array to sort
* @high: the highes index in the sub array to sort
* Return: the index of the pivot
*/
int partition(int *array, size_t size, int low, int high)
{
int pivot, i = low, j;
pivot = array[high];
for (j = low; j < high; j++)
{
if (array[j] <= pivot)
{
if (j != i)
{
swap(&array[j], &array[i]);
print_array(array, size);
}
i++;
}
}
if (i != high)
{
swap(&array[high], &array[i]);
print_array(array, size);
}
return (i);
}
/**
* quick_sorting - the quick sort algorithm
* @array: the array to sort
* @size: the size of the array
* @low: the lowest index in the sub array to sort
* @high: the highes value in the sub array to sort
* Return: void
*/
void quick_sorting(int *array, size_t size, int low, int high)
{
int part;
if (low >= high)
{
return;
}
part = partition(array, size, low, high);
quick_sorting(array, size, low, part - 1);
quick_sorting(array, size, part + 1, high);
}
/**
* quick_sort - a wrapper function for the quick sort algorithm
* @array: the array to sort
* @size: the size of the array
* Return: void
*/
void quick_sort(int *array, size_t size)
{
quick_sorting(array, size, 0, size - 1);
}