-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3-quick_sort.c
More file actions
63 lines (48 loc) · 971 Bytes
/
3-quick_sort.c
File metadata and controls
63 lines (48 loc) · 971 Bytes
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"
/**
* quick_sort - Sorts an int array with the quick sort algorithm.
*
* @array: Array of integers.
* @size: Size of int array.
*/
void quick_sort(int *array, size_t size)
{
size_t pivot, l = 0, r = size - 1, count = 0;
int *narray, temp;
if (!array || (size < 2 && size > 0))
return;
narray = array;
while (count++ < size)
{
pivot = partition(narray, l, r);
print_array(array, size);
if (pivot < r)
temp = array[r];
array[r] = array[pivot];
array[pivot] = temp;
print_array(array, size);
r = pivot - 1;
}
}
void sort_array(int *array, size_t left, size_t right)
{
}
size_t partition(int *array, size_t left, size_t right)
{
size_t pivot = right--;
int temp;
while (array[left] <= array[pivot])
left++;
while (array[right] > array[pivot])
right--;
if (left < pivot)
{
if (right > left)
{
temp = array[right];
array[right] = array[left];
array[left] = temp;
return (left);
}
}
}