Skip to content

Commit b9ff2b8

Browse files
committed
mergesort code in python and cpp added
1 parent 7358cd8 commit b9ff2b8

File tree

5 files changed

+109
-2
lines changed

5 files changed

+109
-2
lines changed

a.out

-6.96 KB
Binary file not shown.

cpp_notes.txt

+22
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,25 @@ table. Coz all keys of the map are initialized to 0. Hence, if a fucntions retur
4343

4444
============================================================================
4545

46+
Ouuu... While doing merge sort I found a lot of usefull things
47+
48+
vectors; merging vectors in c ++
49+
50+
assume you have
51+
std::vector<int> AB, A = {1,2,3,4},B = {5,6,7,8};
52+
53+
then do:
54+
55+
AB.reserve(A.size()+B.size());
56+
AB.insert(AB.end(), A.begin().A.end());
57+
AB.insert(AB.end(), B.begin(), B.end());
58+
59+
this will give us AB = {1,2,3,4,5,6,7,8}
60+
61+
-----
62+
63+
Now, to check if a vector is empty: vector.empty() (boolean value return)
64+
65+
to erase an element (or range of elements) in the vector: vector.erase(vector.begin() + index, (optional) vector.begin()+end)
66+
67+
=============================================================================

ip.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
AGGTAB
2-
GXTXAYB
1+
12 11 13 5 6 7

merge_sort.cpp

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
A standard divide and conquer algorithm for mergesort
3+
4+
input format
5+
============
6+
12 11 13 5 6 7
7+
==============
8+
*/
9+
#include <bits/stdc++.h>
10+
std::vector<int> v, answer;
11+
12+
std::vector<int> merge(std::vector<int> low, std::vector<int> high){
13+
std::vector<int> l, ret;
14+
while(!low.empty() && !high.empty()){
15+
if(low[0] < high[0]){
16+
l.push_back(low[0]);
17+
low.erase(low.begin());
18+
}
19+
else{
20+
l.push_back(high[0]);
21+
high.erase(high.begin());
22+
}
23+
}
24+
ret.reserve(l.size() + low.size() + high.size());
25+
ret.insert(ret.end(), l.begin(), l.end());
26+
ret.insert(ret.end(), low.begin(), low.end());
27+
ret.insert(ret.end(), high.begin(), high.end());
28+
return ret;
29+
}
30+
31+
std::vector<int> mergesort(int l, int h){
32+
std::vector<int> low;
33+
std::vector<int> high;
34+
int mid = (l+h)/2;
35+
if (l < h){
36+
low = mergesort(l, mid);
37+
high = mergesort(mid+1, h);
38+
return merge(low, high);
39+
}
40+
return {v[l]};
41+
}
42+
43+
int main(){
44+
int temp;
45+
for(int i = 0; i<6; i++){
46+
std::cin >> temp;
47+
v.push_back(temp);
48+
}
49+
50+
answer = mergesort(0, 5);
51+
for(int i = 0; i < 6; i++){
52+
std::cout << answer[i] << " ";
53+
}
54+
return 0;
55+
}

merge_sort.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'''
2+
A standard divide and conquer algorithm for mergesort
3+
4+
input format
5+
============
6+
12 11 13 5 6 7
7+
==============
8+
'''
9+
10+
arr = list(map(int, input().split(" ")))
11+
12+
def merge(a,b):
13+
l = []
14+
while a and b:
15+
if a[0] < b[0]:
16+
l.append(a.pop(0))
17+
else:
18+
l.append(b.pop(0))
19+
return l + a + b
20+
21+
22+
def merge_sort(l, h):
23+
mid = int((h+l)/2)
24+
if l < h:
25+
# print(arr[l: h])
26+
low = merge_sort(l, mid)
27+
high = merge_sort(mid+1, h)
28+
return merge(low, high)
29+
# print(arr, l)
30+
return [arr[l]]
31+
print(merge_sort(0, len(arr)-1))

0 commit comments

Comments
 (0)