-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path29th day
50 lines (50 loc) · 918 Bytes
/
29th day
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 <bits/stdc++.h>
using namespace std;
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
int partition(int arr[], int l, int h)
{
int pivot = arr[l], i = l, j = h;
do
{
do
{
i++;
} while (arr[i] <= pivot);
do
{
j--;
} while (arr[j] > pivot);
if (i < j)
{
swap(&arr[i], &arr[j]);
}
} while (i < j);
swap(&arr[l], &arr[j]);
return j;
}
void quicksort(int arr[], int l, int h)
{
int j;
if (l < h)
{
j = partition(arr, l, h);
quicksort(arr, l, j);
quicksort(arr, j + 1, h);
}
}
int main()
{
int arr[] = {7, 74, 26, 23, 45, 27, 35, 77, 54, 33, 12, 45, 43, INT_MAX}, n = 14;
quicksort(arr, 0, n - 1);
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
return 0;
}