-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergesort.py
53 lines (35 loc) · 1.09 KB
/
mergesort.py
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
class mergesort:
def __init__(self,list):
self.list = list
self.sort()
def sort(self):
print("before sort",self.list)
self.merge_sort(0,len(self.list)-1)
self.display()
def merge_sort(self,lower,upper):
if lower < upper:
mid = ((lower+upper)//2)
self.merge_sort(lower,mid)
self.merge_sort(mid+1,upper)
self.merge(lower,mid,upper)
def merge(self,lower,mid,upper):
i=lower
j=mid+1
templist=list()
while i<=mid and j<=upper:
if(self.list[i]<self.list[j]):
templist.append(self.list[i])
i+=1
else:
templist.append(self.list[j])
j+=1
while j<=upper:
templist.append(self.list[j])
j+=1
while i<=mid:
templist.append(self.list[i])
i+=1
for i in range(len(templist)):
self.list[lower+i] = templist[i]
def display(self):
print("aftersort",self.list)