Skip to content

Commit d1e9352

Browse files
Write a function that sorts an array of integers in ascending order using the Quick sort algorithm. Prototype: void quick_sort(int *array, size_t size); You must implement the Lomuto partition scheme.
1 parent 5c2d14e commit d1e9352

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

3-quick_sort.c

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include "sort.h"
2+
3+
void swap_ints(int *a, int *b);
4+
int lomuto_partition(int *array, size_t size, int left, int right);
5+
void lomuto_sort(int *array, size_t size, int left, int right);
6+
void quick_sort(int *array, size_t size);
7+
8+
/**
9+
* swap_ints - Swap two integers in an array.
10+
* @a: The first integer to swap.
11+
* @b: The second integer to swap.
12+
*/
13+
void swap_ints(int *a, int *b)
14+
{
15+
int tmp;
16+
17+
tmp = *a;
18+
*a = *b;
19+
*b = tmp;
20+
}
21+
22+
/**
23+
* lomuto_partition - Order a subset of an array of integers according to
24+
* the lomuto partition scheme (last element as pivot).
25+
* @array: The array of integers.
26+
* @size: The size of the array.
27+
* @left: The starting index of the subset to order.
28+
* @right: The ending index of the subset to order.
29+
*
30+
* Return: The final partition index.
31+
*/
32+
int lomuto_partition(int *array, size_t size, int left, int right)
33+
{
34+
int *pivot, above, below;
35+
36+
pivot = array + right;
37+
for (above = below = left; below < right; below++)
38+
{
39+
if (array[below] < *pivot)
40+
{
41+
if (above < below)
42+
{
43+
swap_ints(array + below, array + above);
44+
print_array(array, size);
45+
}
46+
above++;
47+
}
48+
}
49+
50+
if (array[above] > *pivot)
51+
{
52+
swap_ints(array + above, pivot);
53+
print_array(array, size);
54+
}
55+
56+
return (above);
57+
}
58+
59+
/**
60+
* lomuto_sort - Implement the quicksort algorithm through recursion.
61+
* @array: An array of integers to sort.
62+
* @size: The size of the array.
63+
* @left: The starting index of the array partition to order.
64+
* @right: The ending index of the array partition to order.
65+
*
66+
* Description: Uses the Lomuto partition scheme.
67+
*/
68+
void lomuto_sort(int *array, size_t size, int left, int right)
69+
{
70+
int part;
71+
72+
if (right - left > 0)
73+
{
74+
part = lomuto_partition(array, size, left, right);
75+
lomuto_sort(array, size, left, part - 1);
76+
lomuto_sort(array, size, part + 1, right);
77+
}
78+
}
79+
80+
/**
81+
* quick_sort - Sort an array of integers in ascending
82+
* order using the quicksort algorithm.
83+
* @array: An array of integers.
84+
* @size: The size of the array.
85+
*
86+
* Description: Uses the Lomuto partition scheme. Prints
87+
* the array after each swap of two elements.
88+
*/
89+
void quick_sort(int *array, size_t size)
90+
{
91+
if (array == NULL || size < 2)
92+
return;
93+
94+
lomuto_sort(array, size, 0, size - 1);
95+
}

0 commit comments

Comments
 (0)