-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path3-quick_sort.c
More file actions
82 lines (74 loc) · 1.7 KB
/
3-quick_sort.c
File metadata and controls
82 lines (74 loc) · 1.7 KB
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
#include "sort.h"
void swapp(int *array, size_t size, int i, int j);
/**
* find_pivot - a function that is used to find the pivot
* @array: arr of ints
* @size: Size of array
* @start: first index in the array or sub-array
* @last: last index in the array or sub-array
* Return: the pivot's index
*/
int find_pivot(int *array, size_t size, int start, int last)
{
int i = start - 1, j = start;
for (; j < last; j++)
{
if (array[j] < array[last])
{
i++;
swapp(array, size, i, j);
}
}
swapp(array, size, i + 1, last);
return (i + 1);
}
/**
* make_partitions_with_pivot - a function that is
* used to make partitions with the pivot [low, piv, high]
* @array: arr of ints
* @size: Size of array
* @start: first index in the array or sub-array
* @last: last index in the array or sub-array
* Return: void
*/
void make_partitions_with_pivot(int *array, size_t size, int start, int last)
{
int pivot;
if (start > last)
return;
pivot = find_pivot(array, size, start, last);
make_partitions_with_pivot(array, size, start, pivot - 1);
make_partitions_with_pivot(array, size, pivot + 1, last);
}
/**
* swapp - swaps two eles
*
* @array: array of ints
* @size: size of th arr
* @i: first idx
* @j: sec idx
* Return: void
*/
void swapp(int *array, size_t size, int i, int j)
{
int temp;
temp = array[j];
if (array[i] != array[j])
{
array[j] = array[i];
array[i] = temp;
print_array(array, size);
}
}
/**
* quick_sort - function that sorts an array of integers in
* ascending order using the Quick sort algorithm
*
* @array: The Array
* @size: The size of array
*/
void quick_sort(int *array, size_t size)
{
if (array)
make_partitions_with_pivot(array, size, 0, size - 1);
}