-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick_lumoto.cpp
48 lines (44 loc) · 883 Bytes
/
quick_lumoto.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
#include<iostream>
int partition(int arr[], int low, int high)
{
srand(time(NULL));
int index = rand() % (high - low + 1) + low;
int i = low;
int temp = arr[high];
arr[high] = arr[index];
arr[index] = temp;
int pivot = arr[high];
for(int j = low; j<high; j++)
{
if(arr[j]<=pivot)
{
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
i++;
}
}
temp = arr[high];
arr[high] = arr[i];
arr[i] = temp;
return i;
}
void quicksort(int arr[], int low, int high)
{
if(low < high)
{
int p = partition(arr, low, high);
quicksort(arr, low, p-1);
quicksort(arr, p+1, high);
}
}
int main()
{
int arr[] = {5,7,2,9,1,6};
quicksort(arr, 0, 5);
for(int i=0; i<6; i++)
{
printf("%d\t", arr[i]);
}
return 0;
}