Skip to content

Commit beccef7

Browse files
committed
Task 107
1 parent ae9167f commit beccef7

File tree

1 file changed

+44
-67
lines changed

1 file changed

+44
-67
lines changed

107-quick_sort_hoare.c

+44-67
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,72 @@
11
#include "sort.h"
2-
3-
void swap_ints(int *a, int *b);
4-
int hoare_partition(int *array, size_t size, int left, int right);
5-
void hoare_sort(int *array, size_t size, int left, int right);
6-
void quick_sort_hoare(int *array, size_t size);
7-
82
/**
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)
3+
*swap - the positions of two elements into an array
4+
*@array: array
5+
*@item1: array element
6+
*@item2: array element
7+
*/
8+
void swap(int *array, ssize_t item1, ssize_t item2)
149
{
1510
int tmp;
1611

17-
tmp = *a;
18-
*a = *b;
19-
*b = tmp;
12+
tmp = array[item1];
13+
array[item1] = array[item2];
14+
array[item2] = tmp;
2015
}
21-
2216
/**
23-
* hoare_partition - Order a subset of an array of integers
24-
* according to the hoare partition scheme.
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-
* Description: Uses the last element of the partition as the driver.
33-
* Prints the array after each swap of two elements.
17+
*hoare_partition - hoare partition sorting scheme implementation
18+
*@array: array
19+
*@first: first array element
20+
*@last: last array element
21+
*@size: size array
22+
*Return: return the position of the last element sorted
3423
*/
35-
int hoare_partition(int *array, size_t size, int left, int right)
24+
int hoare_partition(int *array, int first, int last, int size)
3625
{
37-
int driver, up, down;
26+
int current = first - 1, finder = last + 1;
27+
int pivot = array[last];
3828

39-
driver = array[right];
40-
for (up = left - 1, down = right + 1; up < down;)
29+
while (1)
4130
{
31+
4232
do {
43-
up++;
44-
} while (array[up] < driver);
33+
current++;
34+
} while (array[current] < pivot);
4535
do {
46-
down--;
47-
} while (array[down] > driver);
48-
49-
if (up < down)
50-
{
51-
swap_ints(array + up, array + down);
52-
print_array(array, size);
53-
}
36+
finder--;
37+
} while (array[finder] > pivot);
38+
if (current >= finder)
39+
return (current);
40+
swap(array, current, finder);
41+
print_array(array, size);
5442
}
55-
56-
return (up);
5743
}
58-
5944
/**
60-
* hoare_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 Hoare partition scheme.
45+
*qs - qucksort algorithm implementation
46+
*@array: array
47+
*@first: first array element
48+
*@last: last array element
49+
*@size: array size
6750
*/
68-
void hoare_sort(int *array, size_t size, int left, int right)
51+
void qs(int *array, ssize_t first, ssize_t last, int size)
6952
{
70-
int belong;
53+
ssize_t position = 0;
7154

72-
if (right - left > 0)
55+
if (first < last)
7356
{
74-
belong = hoare_partition(array, size, left, right);
75-
hoare_sort(array, size, left, belong - 1);
76-
hoare_sort(array, size, belong, right);
57+
position = hoare_partition(array, first, last, size);
58+
qs(array, first, position - 1, size);
59+
qs(array, position, last, size);
7760
}
7861
}
79-
8062
/**
81-
* quick_sort_hoare - 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 Hoare partition scheme. Prints
87-
* the array after each swap of two elements.
63+
*quick_sort_hoare - prepare the terrain to quicksort algorithm
64+
*@array: array
65+
*@size: array size
8866
*/
8967
void quick_sort_hoare(int *array, size_t size)
9068
{
91-
if (array == NULL || size < 2)
69+
if (!array || size < 2)
9270
return;
93-
94-
hoare_sort(array, size, 0, size - 1);
71+
qs(array, 0, size - 1, size);
9572
}

0 commit comments

Comments
 (0)