forked from dbkwce/CA22-23
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quicksorting.cpp
61 lines (56 loc) · 833 Bytes
/
quicksorting.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
#include <iostream>
using namespace std;
void swap(int arr[],int pos1,int pos2)
{
int tmp;
tmp=arr[pos1];
arr[pos1]=arr[pos2];
arr[pos2]=tmp;
}
int partition(int arr[],int low,int high,int pivot)
{
int i=low;
int j= low;
while( i<=high)
{
if(arr[i]>pivot)
{
i++;
}
else
{
swap(arr,i,j);
i++;
j++;
}
}
return j-1;
}
void quickSort(int arr[], int low, int high)
{
if(low<high)
{
int pivot=arr[high];
int pos=partition(arr,low,high,pivot);
quickSort(arr,low,pos-1);
quickSort(arr,pos+1,high);
}
}
int main()
{
int n;
cout <<"Enter the size of array:";
cin>>n;
int arr[n];
cout<<"\nEnter Array elements:";
for(int i=0;i<n;i++)
{
cin>> arr[i];
}
quickSort(arr,0,n-1);
cout<<"The sorted array is: ";
for(int i=0;i<n;i++)
{
cout<<arr[i]<<"\t";
}
}