-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquicksort.cpp
More file actions
39 lines (33 loc) · 793 Bytes
/
quicksort.cpp
File metadata and controls
39 lines (33 loc) · 793 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
// Best Case- when partitioning is always done in the middle. O(n log n)
// Worst Case- when partitioning is done at any end. O(n^2)
// Average Case- O(n log n)
#include <bits/stdc++.h>
using namespace std;
int partition(int A[], int l, int h){
int pivot=A[l];
int i=l, j=h;
do{
do{i++;}while(A[i]<=pivot);
do{j--;}while(A[j]>pivot);
if(i<j)swap(A[i], A[j]);
}while(i<j);
swap(A[l], A[j]);
return j;
}
void QuickSort(int A[], int l, int h){
int j;
if(l<h){
j=partition(A, l, h);
QuickSort(A, l, j);
QuickSort(A, j+1, h);
}
}
int main()
{
int A[]={11,13,7,12,16,9,24,5,10,3,INT_MAX},n=11;
QuickSort(A, 0, n-1);
for(int i=0; i<n-1; i++){
cout<<A[i]<<" ";
}
return 0;
}