-
Notifications
You must be signed in to change notification settings - Fork 0
/
quicksort.cpp
63 lines (59 loc) · 1.02 KB
/
quicksort.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
59
60
61
62
63
#include <iostream>
using namespace std;
void swap(int arr[], int e1, int e2)
{
int temp;
temp = arr[e1];
arr[e1] = arr[e2];
arr[e2] = temp;
}
int partition(int a[], int l, int h)
{
int piv;
piv = a[l];
int i, j;
i = l;
j = h;
do
{
do
{
i++;
} while (a[i] <= piv);
do
{
j--;
} while (a[j] > piv);
if (i < j)
swap(a, i, j);
} while (i < j);
swap(a, l, j);
return j;
}
void qs(int a[], int l, int h)
{
int j;
if (l < h)
{
j = partition(a, l, h);
qs(a, l, j);
qs(a, j + 1, h);
}
}
int main()
{
int n;
cout << "Enter the number of elements: ";
cin >> n;
int a[n + 1];
a[n] = INT32_MAX; //infinnity
cout << "Enter elements:" << endl;
for (int i = 0; i < n; i++)
cin >> a[i];
qs(a, 0, n);
cout << "Array after Sorting: ";
for (int i = 0; i < n; i++)
cout << a[i] << " ";
cout << endl;
return 0;
}