forked from imnurav/Hactoberfest2021-Cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSort.cpp
75 lines (70 loc) · 1.47 KB
/
QuickSort.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//QuickSort is a Divide and Conquer algorithm.
//Comments in the code: if pivot was taken as last element(the changes that need to be done, in the code.)
//This code is for pivot,taken as starting element.
/*
Time Complexity(Best Case): O(n*logn)
Time Complexity(Worst Case): O(n^2)
Space Complexity: O(log n)
*/
#include <bits/stdc++.h>
using namespace std;
void swap(int &x,int &y)
{
int t=x;
x=y;
y=t;
}
void Display(int a[],int n)
{
for (int i = 0; i < n; i++)
{
cout << "a[" << i << "]:" << a[i] << endl;
}
cout << endl;
}
//Partition of Arrays Logic
int Partition(int a[],int l,int h)
{
int pivot=a[l]; //int pivot=a[h];
int i=l,j=h;
while(i<j)
{
while(a[i]<=pivot) //while(a[i]<pivot)
i++;
while(a[j]>pivot) //(a[j]>=pivot)
j--;
if(i<j)
swap(a[i],a[j]);
}
swap(a[l],a[j]); //swap(a[h],a[i]);
return j; //return i;
}
// Recursion Logic
void QuickSort(int a[],int l,int h)
{
if(l<h)
{
int pos=Partition(a,l,h);
QuickSort(a,l,pos-1);
QuickSort(a,pos+1,h);
}
}
int main()
{
int n;
cout<<"Enter no. of elements: ";
cin>>n;
cout<<"Enter elements"<<endl;
int a[n];
for(int i=0;i<n;i++)
{
cout<<"a["<<i<<"]:";
cin>>a[i];
}
cout <<endl<<"Original Array" << endl;
Display(a,n);
cout<<endl<<"Sorted Array"<<endl;
QuickSort(a,0,n-1);
Display(a,n);
return 0;
}