-
Notifications
You must be signed in to change notification settings - Fork 0
/
quicksort.cpp
78 lines (72 loc) · 1.84 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
76
77
78
/* QUICK SORT */
#include <iostream>
#include <vector>
using namespace std;
int quick(vector<int> &a, int n, int beg, int end)
{
int left, right, loc = beg;
cout<<"left is :" << left << ",right is: " << right << ", loc is : " << loc << endl;
while(left != right){
left = beg, right = end;
cout << "we are in the first while loop" << endl;
cout<<"left is :" << left << ",right is: " << right << ", loc is : " << loc << endl;
while(a[loc]<a[right] && loc!=right)
right--;
cout << " we are in nested while loop " << endl;
cout<<"left is :" << left << ",right is: " << right << ", loc is : " << loc << endl;
if(a[loc]>a[right]){
cout << " a[loc] > a [right]" << endl;
int temp = a[right];
a[right] = a[loc];
a[loc] = temp;
cout << "elements swapped" << endl << "right is : " << right << " loc is " << loc<<endl;
}
loc = right;
cout << "shifted loc to right" << endl << "loc is " << loc << endl;
while(a[loc] > a[left] && loc!=left)
left++;
cout << "left is: "<<left<<endl;
if(a[loc] < a[left]){
int temp = a[loc];
a[loc] = a[left];
a[left] = temp;
}
loc = left;
cout<< "shifted loc to left"<<endl<<"loc is"<<loc<<endl;
}
return loc;
}
main()
{
vector<int> val(20), lower(20), upper(20);
int n, top = 0, beg, end;
cout <<"enter number of elements ";
cin >> n;
cout<<"enter values"<<endl;
for(int i =1; i<=n; i++){
cout << i <<": ";
cin >> val[i];
}
if(n>1){
top++;
lower[1] = 1;
upper[1] = n;
}
while(top!=0){
beg = lower[top]; end = upper[top];
top--;
cout<<"top is: "<<top<<"beg is "<<beg<<"end is: "<<end<<endl;
int loc = quick(val, n, beg, end);
// cout << "loc = "<<loc<<endl;
if(beg < loc-1){
top++; lower[top] = beg;
upper[top] = loc-1;
}
if(loc+1 < end){
top++; lower[top] = loc+1;
upper[top] = end;
}
}
for(int j=1; j<=n; j++)
cout<<" "<<val[j];
}