-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmerge_sort.py
67 lines (51 loc) · 2.16 KB
/
merge_sort.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class Car:
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
def __str__(self):
return str.format("Make: {}, Model: {}, Year: {}", self.make, self.model, self.year)
def merge(list1, l, r, m, comp_fun):
left_copy = list1[l:m + 1]
r_sublist = list1[m+1:r+1]
left_copy_index = 0
r_sublist_index = 0
sorted_index = l
while left_copy_index < len(left_copy) and r_sublist_index < len(r_sublist):
# We use the comp_fun instead of a simple comparison operator
if comp_fun(left_copy[left_copy_index], r_sublist[r_sublist_index]):
list1[sorted_index] = left_copy[left_copy_index]
left_copy_index = left_copy_index + 1
else:
list1[sorted_index] = r_sublist[r_sublist_index]
r_sublist_index = r_sublist_index + 1
sorted_index = sorted_index + 1
while left_copy_index < len(left_copy):
list1[sorted_index] = left_copy[left_copy_index]
left_copy_index = left_copy_index + 1
sorted_index = sorted_index + 1
while r_sublist_index < len(r_sublist):
list1[sorted_index] = r_sublist[r_sublist_index]
r_sublist_index = r_sublist_index + 1
sorted_index = sorted_index + 1
def merge_sort(list1, l, r, comp_fun):
if l >= r:
return
m = (l + r)//2
merge_sort(list1, l, m, comp_fun)
merge_sort(list1, m + 1, r, comp_fun)
merge(list1, l, r, m, comp_fun)
car1 = Car("Renault", "33 Duster", 2001)
car2 = Car("Maruti", "Maruti Suzuki Dzire", 2015)
car3 = Car("Tata motor", "Jaguar", 2004)
car4 = Car("Cadillac", "Seville Sedan", 1995)
list1 = [car1, car2, car3, car4]
merge_sort(list1, 0, len(list1) -1, lambda carA, carB: carA.year < carB.year)
print("Cars sorted by year:")
for car in list1:
print(car)
print()
merge_sort(list1, 0, len(list1) -1, lambda carA, carB: carA.make < carB.make)
print("Cars sorted by make:")
for car in list1:
print(car)