-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path3-quick_sort.c
88 lines (79 loc) · 1.38 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
#include "sort.h"
/**
* myswap - swaps two values
*
* @array: data sort input
* @firts: first
* @second: second
* @size: size
*
* Return: No Return
*/
void myswap(int *array, int firts, int second, int size)
{
int tmp;
if (array[firts] != array[second])
{
tmp = array[firts];
array[firts] = array[second];
array[second] = tmp;
print_array(array, size);
}
}
/**
* part - part to a pivot
*
* @array: data sort input
* @left: left
* @right: right
* @size: size input
*
* Return: New pivote
*/
int part(int *array, int left, int right, size_t size)
{
int i = left, j, pivot = array[right];
for (j = left; j <= right; j++)
{
if (array[j] < pivot)
{
myswap(array, i, j, size);
i++;
}
}
myswap(array, i, right, size);
return (i);
}
/**
* myquicksort - quick sort algorithm
*
* @array: data to sort
* @left: left pivote
* @right: right pivote
* @size: size input
* Return: No Return
*/
void myquicksort(int *array, int left, int right, size_t size)
{
int pivote;
if (left < right)
{
pivote = part(array, left, right, size);
myquicksort(array, left, pivote - 1, size);
myquicksort(array, pivote + 1, right, size);
}
}
/**
* quick_sort - quick sort algorithm
*
* @array: sort data
* @size: size input
*
* Return: No Return
*/
void quick_sort(int *array, size_t size)
{
if (size < 2)
return;
myquicksort(array, 0, size - 1, size);
}