-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ8Hoare.cpp
58 lines (53 loc) · 1.01 KB
/
Q8Hoare.cpp
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
#include <iostream>
int partition(int arr[], int low, int high)
{
srand(time(NULL));
int index = rand() % (high - low + 1) + low;
int temp = arr[index];
arr[index] = arr[low];
arr[low] = temp;
int pivot = arr[low];
int i = low - 1;
int j = high + 1;
while (true)
{
do
{
i++;
} while (arr[i] < pivot);
do
{
j--;
} while (arr[j] > pivot);
if (i >= j)
{
return j;
}
else
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
void quicksort(int arr[], int low, int high)
{
if (low < high)
{
int p = partition(arr, low, high);
quicksort(arr, low, p);
quicksort(arr, p + 1, high);
}
}
int main()
{
int arr[] = {5, 7, 1, 9, 8, 10};
quicksort(arr, 0, 5);
printf("Soreted Array: \n");
for (int i = 0; i < 6; i++)
{
printf("%d\t", arr[i]);
}
return 0;
}