-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathQuickSort.cpp
More file actions
56 lines (48 loc) · 872 Bytes
/
QuickSort.cpp
File metadata and controls
56 lines (48 loc) · 872 Bytes
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
#include<iostream>
// #include<algorithm>
using namespace std;
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
int partition(int arr[], int l, int h)
{
int pivot = arr[h];
int i = l-1;
for(int j=l; j<=h-1; j++)
{
if(arr[j]<pivot)
{
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i+1], &arr[h]);
return (i+1);
}
void qSort(int arr[], int l, int h)
{
if(l<h)
{
int p = partition(arr, l, h);
qSort(arr, l, p-1);
qSort(arr, p+1, h);
}
}
void print(int arr[], int n)
{
for(int i=0; i<n; i++)
cout<<arr[i]<<" ";
cout<<endl;
}
int main()
{
int n;
int arr[] = {10, 7, 8, 9, 1, 5};
n = 6;
qSort(arr, 0, n-1);
print(arr, n);
return 0;
}