-
Notifications
You must be signed in to change notification settings - Fork 0
/
quick_sort.cpp
57 lines (48 loc) · 1.11 KB
/
quick_sort.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
/*
Best-case time complexity O(nlogn)
Average-case time complexity O(nlogn)
worst-case time complexity O(n^2)
space complexity O(logn) ~ O(1)
it is stable
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
void swap(int *i,int *j){
int temp=*i; *i=*j; *j=temp;
}
int partition(int *arr,int start,int end){
int pivot=arr[end];
int pindex=start;
for(int i=start;i<end;++i){
if(arr[i]<=pivot){
swap(&arr[i],&arr[pindex]);
pindex++;
}
}
swap(&arr[pindex],&arr[end]);
return pindex;
}
void quicksort(int *arr,int start, int end){
if(start>=end) return;
int pivot=partition(arr,start,end);
quicksort(arr,start,pivot-1);
quicksort(arr,pivot+1,end);
}
int main(){
cout<<"enter the size of array?"<<endl;
int n,i,j,k;
cin>>n;
cout<<"enter the elements of the array......."<<endl;
int arr[n];
for(i=0;i<n;++i){
cin>>arr[i];
}
quicksort(arr,0,n-1);
cout<<"sorted array: ";
for(i=0;i<n;++i){
cout<<arr[i]<<" ";
}cout<<endl;
return 0;
}