forked from rathoresrikant/HacktoberFestContribute
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadding-quick-sort-algorithm.cpp
More file actions
55 lines (52 loc) · 895 Bytes
/
adding-quick-sort-algorithm.cpp
File metadata and controls
55 lines (52 loc) · 895 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
#include<iostream>
using namespace std;
int count=0;
int par(int A[ ],int p,int r)
{
int x=A[r];
int i=p-1;
for(int j=p;j<r;j++)
{
count++;
if(A[j]<x)
{
i++;
int temp=A[j];
A[j]=A[i];
A[i]=temp;
}
}
int temp=A[r];
A[r]=A[i+1];
A[i+1]=temp;
return i+1;
}
void quickSort(int A[ ],int p,int r)
{
if(p<r)
{
int q=par(A,p,r);
quickSort(A,p,q-1);
quickSort(A,q+1,r);
}
}
int main()
{
int n;
cout<<"\tQuick-Sort:";
do{
cout<<"\n Enter the number of element : ";
cin>>n;
int A[n];
cout<<"Enter the element : ";
for(int i=0;i<n;i++)
{
cin>>A[i];
}
quickSort(A,0,n-1);
cout<<"Sorted Array : ";
for(int i=0;i<n;i++)
cout<<A[i]<<" ";
cout<<"\nCount : "<<count;
}while(1);
}