1
1
#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
-
8
2
/**
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 )
14
9
{
15
10
int tmp ;
16
11
17
- tmp = * a ;
18
- * a = * b ;
19
- * b = tmp ;
12
+ tmp = array [ item1 ] ;
13
+ array [ item1 ] = array [ item2 ] ;
14
+ array [ item2 ] = tmp ;
20
15
}
21
-
22
16
/**
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
34
23
*/
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 )
36
25
{
37
- int driver , up , down ;
26
+ int current = first - 1 , finder = last + 1 ;
27
+ int pivot = array [last ];
38
28
39
- driver = array [right ];
40
- for (up = left - 1 , down = right + 1 ; up < down ;)
29
+ while (1 )
41
30
{
31
+
42
32
do {
43
- up ++ ;
44
- } while (array [up ] < driver );
33
+ current ++ ;
34
+ } while (array [current ] < pivot );
45
35
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 );
54
42
}
55
-
56
- return (up );
57
43
}
58
-
59
44
/**
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
67
50
*/
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 )
69
52
{
70
- int belong ;
53
+ ssize_t position = 0 ;
71
54
72
- if (right - left > 0 )
55
+ if (first < last )
73
56
{
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 );
77
60
}
78
61
}
79
-
80
62
/**
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
88
66
*/
89
67
void quick_sort_hoare (int * array , size_t size )
90
68
{
91
- if (array == NULL || size < 2 )
69
+ if (! array || size < 2 )
92
70
return ;
93
-
94
- hoare_sort (array , size , 0 , size - 1 );
71
+ qs (array , 0 , size - 1 , size );
95
72
}
0 commit comments