-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-quick_sort.c
90 lines (80 loc) · 1.4 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
80
81
82
83
84
85
86
87
88
89
90
#include "sort.h"
/**
* swap - function swapping integers in array.
* @a: int.
* @b: int.
*
* Return: void.
*/
void swap(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
/**
* partition_arr - function that divides the array.
* @array: array of integers.
* @size: the size of the array.
* @low: index.
* @high: index.
*
* Return: size_t.
*/
int partition_arr(int *array, size_t size, int low, int high)
{
int x, *pivot, y;
pivot = array + high;
for (x = y = low; y < high; y++)
{
if (array[y] < *pivot)
{
if (x < y)
{
swap(array + y, array + x);
print_array(array, size);
}
x++;
}
}
if (array[x] > *pivot)
{
swap(array + x, pivot);
print_array(array, size);
}
return (x);
}
/**
* sorting - function to sort.
* @array: array of integers.
* @size: the size of the array.
* @low: index.
* @high: index.
*
* Return: size_t.
*/
void sorting(int *array, size_t size, int low, int high)
{
int x;
if (high - low > 0)
{
x = partition_arr(array, size, low, high);
sorting(array, size, low, x - 1);
sorting(array, size, x + 1, high);
}
}
/**
* quick_sort - function that sorts an array of integers
* in ascending order using the Quick sort algorithm.
* @array: array of integers.
* @size: the size of the array.
*
* Return: void.
*/
void quick_sort(int *array, size_t size)
{
if (!array || size < 2)
return;
sorting(array, size, 0, size - 1);
}