-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort.cpp
More file actions
57 lines (57 loc) · 2.02 KB
/
quickSort.cpp
File metadata and controls
57 lines (57 loc) · 2.02 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
#include<iostream>
using namespace std;
void swap(int &a, int &b) {
int temp;
temp=a;
a=b;
b=temp;
}
void display(int *a, int n) {
int i;
for(i=0;i<n;i++){
cout<<a[i]<<" ";
}
}
/*func partition() takes an array,select pivot element ,compare pivot using start ands end indices and perform swaps
function return the final location of pivot element after swapping */
int partition(int *a,int l,int u){
int start=l,end=u,pivot=a[l];//start pointing lowerIndex of Array,end pointing upperIndex of Array,1st element is chosen as pivot element
while(start<end)
{
while(a[start]<=pivot)//start pointer will stop at element > pivot element
start++;
while(a[end]>pivot) //end pointer will stop at element < pivot element
end--;
if(start<end) //swap element at start and end only if start and end didnt crossed each other.
swap(a[start],a[end]) ;
}
swap(a[l], a[end]) ; //pivot element will be swaped withh element pointed by end
return end; //return location of pivot element
}
/*func. quickSort() calls partition function and takes the location of pivot element after swaping,
then partition array as leftside of pivot and rightside of pivot and recursively calls partion function untill array is partitioned into single elements */
void quickSort(int *a, int l, int u){
int loc; //variable storing location of pivot element
if(l<u)
{
loc=partition(a, l, u); //takes location of pivot element
quickSort(a, l, loc-1); //calls for leftside array from pivot element
quickSort(a, loc+1, u) ; //calls for rightside array from pivot element
}
}
int main(){
int n, i;
cout<<"\nEnter Number of Elements ";
cin>>n;
int a[n];
cout<<"\nEnter "<<n<<" Elements ";
for(i=0;i<n;i++){
cin>>a[i];
}
cout<<"\nBefore Sorting : ";
display(a, n) ;
quickSort(a, 0,n-1) ;
cout<<"\nAfter Sorting : ";
display(a, n);
return 0;
}