-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-quick_sort.c
70 lines (64 loc) · 1.44 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
#include "sort.h"
/**
* partition - Partitions an array around a pivot element
* @array: Pointer to the array
* @s: Index of the first element of the partition
* @end: Index of the last element of the partition
* @size: Size of the array
* Return: Final position of the pivot element
*/
int partition(int *array, int s, int end, size_t size)
{
int i = s - 1, aux, k;
for (k = s; k <= end - 1; k++)
{
if (array[k] < array[end])
{
i++;
if (i < k)
{
aux = array[i];
array[i] = array[k];
array[k] = aux;
print_array(array, size);
}
}
}
if (array[i + 1] > array[end])
{
aux = array[i + 1];
array[i + 1] = array[end];
array[end] = aux;
print_array(array, size);
}
return (i + 1);
}
/**
* quicksort - Sorts an array of integers using the quicksort algorithm
* @array: Pointer to the array
* @s: Index of the first element of the array/subarray
* @end: Index of the last element of the array/subarray
* @size: Size of the array
* Return: is void
*/
void quicksort(int *array, int s, int end, size_t size)
{
int pivot;
if (s < end)
{
pivot = partition(array, s, end, size);
quicksort(array, s, pivot - 1, size);
quicksort(array, pivot + 1, end, size);
}
}
/**
* quick_sort - Sorts an array of integers using the quicksort algorithm
* @array: Pointer to the array
* @size: Size of the array
*
* Return: is void
*/
void quick_sort(int *array, size_t size)
{
quicksort(array, 0, size - 1, size);
}