-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-quick_sort.c
63 lines (60 loc) · 1.36 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
#include "sort.h"
void qs_partition(int *arr, size_t size, size_t left, size_t right);
void swap_elem(int *arr, size_t size, int *a, int *b);
/**
* quick_sort - sorts an array in ascending order
* @array: array to sort
* @size: array size
*/
void quick_sort(int *array, size_t size)
{
if (array && size > 1)
qs_partition(array, size, 0, size - 1);
}
/**
* qs_partition - sorts partition according to pivot
* @arr: original array
* @size: size of original array
* @left: leftmost element of partition
* @right: rightmost element of partition
*/
void qs_partition(int *arr, size_t size, size_t left, size_t right)
{
int pivot = arr[right]; /* pivot is last elem */
size_t i, j;
if (left < right)
{
for (i = j = left; j < right; j++)
{
if (arr[j] <= pivot)
{
swap_elem(arr, size, &arr[j], &arr[i]);
i++;
}
}
swap_elem(arr, size, &arr[j], &arr[i]);
/* lesser partition */
if (i > 0)
qs_partition(arr, size, left, i - 1);
/* greater partition */
qs_partition(arr, size, i + 1, right);
}
}
/**
* swap_elem - swap value of array elements
* @arr: array (for print)
* @size: size of array (for print)
* @a: pointer to array element
* @b: pointer to array element
*/
void swap_elem(int *arr, size_t size, int *a, int *b)
{
int tmp;
if (*a != *b)
{
tmp = *a;
*a = *b;
*b = tmp;
print_array(arr, size); /* print updated array */
}
}