-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathQuick.cpp
More file actions
62 lines (49 loc) · 1.11 KB
/
Quick.cpp
File metadata and controls
62 lines (49 loc) · 1.11 KB
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
#include <bits/stdc++.h>
using namespace std;
void swap(int* ele1, int* ele2)
{
int t = *ele1;
*ele1 = *ele2;
*ele2 = t;
}
int partition (int a[], int beg, int end)
{
int pivot = a[end];
int i = (beg - 1);
for (int j = beg; j <= end - 1; j++)
{
if (a[j] < pivot)
{
i++;
swap(&a[i], &a[j]);
}
}
swap(&a[i + 1], &a[end]);
return (i + 1);
}
void quickSort(int a[], int beg, int end)
{
if (beg < end)
{
int pIndex = partition(a, beg, end);
quickSort(a, beg, pIndex - 1);
quickSort(a, pIndex + 1, end);
}
}
void display(int a[], int n)
{
for (int i = 0; i < n; i++)
cout << a[i] << " ";
cout << endl;
}
int main()
{
int a[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(a) / sizeof(a[0]);
cout << "Elements of array before sorting: \n";
display(a, n);
quickSort(a, 0, n - 1);
cout << "Elements of array after sorting: \n";
display(a, n);
return 0;
}