-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquick_sort.cpp
More file actions
80 lines (51 loc) · 1.32 KB
/
quick_sort.cpp
File metadata and controls
80 lines (51 loc) · 1.32 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Data-Structures-And-Algorithms
Here I will post my regular DSA problems
#include<iostream>
using namespace std;
// Quick Sort Algorithm
void swap ( int *a ,int *b ){
int c;
c=*a;
*a=*b;
*b=c;
}
int partition (int a[], int low, int high)
{
int pivot = a[high]; // Pivot element
int i= (low -1);
// Partitioning takes place here
for( int j = low ; j<=high -1; j++){
if(a[j]<pivot){
i++;
swap(&a[i],&a[j]);
}
}
swap(&a[i+1],&a[high]);
return (i+1);
}
int quick_sort( int a[] , int low ,int high ){
int pi;
if(low<high){
pi= partition (a,low,high);
quick_sort(a, low ,pi -1 );
quick_sort(a, pi + 1 , high);
}
}
int main (){
int n;
cout<<"Enter 'n' value : ";
cin>>n;
cout<<endl;
int a[n];
cout<<"Enter the array elements : "<<endl<<endl;
for(int i=0;i<n;i++){
cin>>a[i];
}
quick_sort ( a,0,n-1 );
cout<<endl<<"The sorted array :\n\n";
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
cout<<endl;
return 0;
}