-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ8Lomuto.cpp
50 lines (45 loc) · 942 Bytes
/
Q8Lomuto.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
#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, 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;
}