-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathquickSort.cpp
More file actions
108 lines (89 loc) · 2.29 KB
/
quickSort.cpp
File metadata and controls
108 lines (89 loc) · 2.29 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//TUSHAR PANJETA
//Quicksort with two pivots
#include<iostream>
#define size 100
using namespace std;
class QuickSort
{
public :
int array[size] , n , leftpivot , rightpivot;
void createArray();
void DualPivotQuickSort(int low, int high);
void partition(int low, int high);
};
void QuickSort::createArray()
{
cout<<"\nEnter no. of elements in array : ";
cin>>n;
while(n<=0)
{
cout<<"\n[ERROR] Invalid input , TRY AGAIN";
cout<<"\nEnter no. of elements in array : ";
cin>>n;
}
cout<<"\nEnter the "<<n<<" elements : ";
for(int i=0 ; i<n ;i++)
{
cin>>array[i];
}
}
void QuickSort::DualPivotQuickSort(int low, int high)
{
if (low < high)
{
partition(low, high);//provide left and right pivots
DualPivotQuickSort(low, leftpivot - 1);
DualPivotQuickSort(leftpivot + 1, rightpivot - 1);
DualPivotQuickSort(rightpivot + 1, high);
}
}
void QuickSort::partition(int low, int high)
{
if(array[low] > array[high])//checking that left pivot is greater than right pivot if yes than swap them
{
swap(array[low], array[high]);
}
int j = low + 1, g = high - 1, k = low + 1, leftpiv = array[low], rightpiv = array[high];
while (k <= g)
{
if (array[k] < leftpiv) // if elements are less than the left pivot
{
swap(array[k], array[j]);
j++;
}
else if (array[k] >= rightpiv) // if elements are greater than or equal to right pivot
{
while (array[g] > rightpiv && k < g)
{
g--;
}
swap(array[k], array[g]);
g--;
if (array[k] < leftpiv) //checking after swaping that if element is less than left pivot
{
swap(array[k], array[j]);
j++;
}
}
k++;
}
j--;
g++;
// bring pivots to their appropriate positions.
swap(array[low], array[j]);
swap(array[high], array[g]);
leftpivot = j;
rightpivot = g;
}
int main()
{
QuickSort q1;
q1.createArray();
q1.DualPivotQuickSort(0,q1.n-1);
cout<<"\nSorted array : ";
for(int i=0 ; i<q1.n ; i++)
{
cout<<q1.array[i]<<" ";
}
return 0;
}