Skip to content

Commit 69640d9

Browse files
committed
Create Merge Sort
1 parent 50d5575 commit 69640d9

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

Merge Sort

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Sort:
2+
def MergeSort(self, alist):
3+
print("Splitting", alist)
4+
if len(alist) >1:
5+
mid = len(alist)//2
6+
lefthand = alist[:mid]
7+
righthand = alist[mid:]
8+
self.MergeSort(lefthand)
9+
self.MergeSort(righthand)
10+
11+
i = 0
12+
j = 0
13+
k = 0
14+
while i<len(lefthand) and j<len(righthand):
15+
if lefthand[i] < righthand[j]:
16+
alist[k] = lefthand[i]
17+
i = i +1
18+
else:
19+
alist[k] = righthand[j]
20+
j = j +1
21+
k += 1
22+
23+
while i <len(lefthand):
24+
alist[k] = lefthand[i]
25+
i +=1
26+
k +=1
27+
28+
29+
while j<len(righthand):
30+
alist[k] = righthand[j]
31+
j +=1
32+
k +=1
33+
print("Merging ",alist)
34+
35+
36+
Obj = Sort()
37+
alist = [8,1,3,4,11,3,2,5,88,6]
38+
result = Obj.MergeSort(alist)

0 commit comments

Comments
 (0)