forked from pandeydivesh15/Data-Structures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 3
/
merge_and_quick_sort.cpp
135 lines (114 loc) · 2.39 KB
/
merge_and_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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <iostream>
#include <vector>
#include <iterator>
#include <stack>
using namespace std;
void merge(vector<int> &v, int left_start, int right_start, int right_end){
//cout<<"$"<<left_start<<right_start<<right_end<<"\n";
vector<int>::iterator itr = v.begin();
vector<int> v1(itr + left_start, itr + right_start);
vector<int> v2(itr + right_start, itr + right_end + 1);
int i = 0, j = 0 , k = left_start;
int l1 = right_start - left_start;
int l2 = right_end - right_start + 1;
while(i < l1 && j < l2){
if (v1[i] <= v2[j])
{
v[k] = v1[i];
i++;
}
else
{
v[k] = v2[j];
j++;
}
k++;
}
while (i < l1){
v[k] = v1[i];
i++;
k++;
}
while (j < l2){
v[k] = v2[j];
j++;
k++;
}
}
void merge_sort(vector<int> &v){
int size = v.size();
int left_start, right_start, right_end;
for(int i = 1; i < size ; i = 2*i) {
for(left_start = 0; left_start < size - 1; left_start += 2*i) {
//cout<<left_start<<"\t"<<i<<"\n";
right_start = left_start + i;
right_end = left_start + 2*i - 1;
if(right_end > size - 1) {
right_end = size - 1;
}
//cout<<"$"<<left_start<<right_start<<right_end<<'\n';
merge(v,left_start, right_start, right_end);
}
}
}
int create_partition(vector<int> &v, int left, int right){
int x = v[right];
int i = left - 1;
for(int j = left; j < right; ++j) {
if(v[j] < x) {
i++;
int temp = v[j];
v[j] = v[i];
v[i] = temp;
}
}
int temp = v[i+1];
v[i+1] = v[right];
v[right] = temp;
return i+1;
}
void quick_sort(vector<int> &v){
stack<int> S;
int start = 0;
int end = v.size() - 1;
S.push(start);
S.push(end);
int part_index;
while(!S.empty()){
end = S.top();
S.pop();
start = S.top();
S.pop();
part_index = create_partition(v, start, end);
if(part_index - 1 > start) {
S.push(start);
S.push(part_index - 1);
}
if(part_index + 1 < end) {
S.push(part_index + 1);
S.push(end);
}
}
}
int main()
{
vector<int> v;
cout<<"Enter size, choice for sorting method and finally your array.\n";
int n;
int ch;
cin>>n>>ch;
int input_count=0;
int temp_input;
while(input_count<n){
cin>>temp_input;
v.push_back(temp_input);
input_count++;
}
if (ch)
merge_sort(v);
else
quick_sort(v);
copy(v.begin(), v.end(),
ostream_iterator<int>(cout," "));
return 0;
}