forked from mastermind222/DSA-and-Algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuik_sortAlgo.cpp
More file actions
48 lines (41 loc) · 1000 Bytes
/
Quik_sortAlgo.cpp
File metadata and controls
48 lines (41 loc) · 1000 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
#include<bits/stdc++.h>
using namespace std;
#define int long long
int partition(int a[],int l,int r){
int pivot=a[r];
int i=l-1;
for(int j=l;j<r;j++){
if(a[j]<pivot){
i++;
swap(a[i],a[j]);
}
}
swap(a[i+1],a[r]);
return i+1;
}
void quicksort(int a[],int l,int r){
if(l<r){
int pi=partition(a,l,r);
quicksort(a,l,pi-1);
quicksort(a,pi+1,r);
}
}
//used for 64 bit integers
//main function should always be a 32 bit one so we use int32_t
int32_t main(){
int n;
cin>>n;
//size of the array
int a[n];
//declaring an array of size n
for(int i=0;i<n;i++){
cin>>a[i];
}
quicksort(a,0,n-1);
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
}
Now,lets discuss about its time complexity
In best case(when the pivot is at mid of the considered sub array) its time complexity would be O(nlog(n))
in worst case(when the pivot is at start or end of the considered sub array) its time complexity would be O(n^2)