-
Notifications
You must be signed in to change notification settings - Fork 0
/
3-quick_sort.c
88 lines (81 loc) · 1.68 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"
/**
* swap - functions that swaps two integers in an array
* @x: First integer to swap
* @y: Second integer to swap
*
* Return: nothing
*/
void swap(int *x, int *y)
{
int tmp = *x;
*x = *y;
*y = tmp;
}
/**
* lomuto_partition - function that orders subarray and places pivot
* by using lomuto partitioning scheme
*
* @array: Array of integers to sort
* @size: Size of array
* @left: Starting index of subarray
* @right: Ending index of subarray
*
* Return: partition index
*/
int lomuto_partition(int *array, size_t size, int left, int right)
{
int l = left - 1, m;
for (m = left; m <= right - 1; m++)
{
if (array[m] < array[right])
{
l++;
if (l != m)
{
swap(&array[l], &array[m]);
print_array(array, size);
}
}
}
if (l + 1 != right)
{
swap(&array[l + 1], &array[right]);
print_array(array, size);
}
return (l + 1);
}
/**
* lomuto_sort - fucntion that implement the quicksort algorithm
* through recursion.
* @array: array of integers to sort.
* @size: The size of the array.
* @left: starting index of the array
* @right: ending index of the array
*
* Return: nothing
*/
void lomuto_sort(int *array, size_t size, int left, int right)
{
int ind;
if (right - left > 0)
{
ind = lomuto_partition(array, size, left, right);
lomuto_sort(array, size, left, ind - 1);
lomuto_sort(array, size, ind + 1, right);
}
}
/**
* quick_sort - function that sorts an array of integers
* in ascending order using quicksort algorithm
* @array: Array of integers to sort
* @size: Size of array
*
* Return: void
*/
void quick_sort(int *array, size_t size)
{
if (!array || size < 2)
return;
lomuto_sort(array, size, 0, size - 1);
}