-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquickSort.cpp
More file actions
50 lines (47 loc) · 776 Bytes
/
quickSort.cpp
File metadata and controls
50 lines (47 loc) · 776 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
#include<iostream>
using namespace std;
int partition(int * arr,int start,int end)
{
int i = start-1;
int pivot = arr[end];
for(int j=start;j<end;j++)
{
if(arr[j]<=pivot)
{
i = i + 1;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
i = i + 1;
int temp = arr[i];
arr[i] = arr[end];
arr[end] = temp;
return i;
}
void quickSort(int * arr, int start,int end)
{
if(start<=end)
{
int p = partition(arr,start,end);
quickSort(arr,start,p-1);
quickSort(arr,p+1,end);
}
}
void display(int * arr,int size)
{
cout<<'\n';
for(int i=0;i<size;i++)
{
cout<<arr[i]<<"\t";
}
}
int main()
{
int arr[6]={77,2,16,99,36,81};
display(arr,6);
quickSort(arr,0,5);
display(arr,6);
return 0;
}