1+ # ### Merge Sort Exercise
2+
3+ # Modify [merge_sort function](https://github.com/codebasics/data-structures-algorithms-python/blob/master/algorithms/5_MergeSort/merge_sort_final.py) such that it can sort following list of athletes as per the time taken by them in the marathon,
4+ # ```
5+ # elements = [
6+ # { 'name': 'vedanth', 'age': 17, 'time_hours': 1},
7+ # { 'name': 'rajab', 'age': 12, 'time_hours': 3},
8+ # { 'name': 'vignesh', 'age': 21, 'time_hours': 2.5},
9+ # { 'name': 'chinmay', 'age': 24, 'time_hours': 1.5},
10+ # ]
11+ # ```
12+ # merge_sort function should take key from an athlete's marathon log and sort the list as per that key. For example,
13+ # ```
14+ # merge_sort(elements, key='time_hours', descending=True)
15+ # ```
16+ # This will sort elements by time_hours and your sorted list will look like,
17+ # ```
18+ # elements = [
19+ # {'name': 'rajab', 'age': 12, 'time_hours': 3},
20+ # {'name': 'vignesh', 'age': 21, 'time_hours': 2.5},
21+ # {'name': 'chinmay', 'age': 24, 'time_hours': 1.5},
22+ # {'name': 'vedanth', 'age': 17, 'time_hours': 1},
23+ # ]
24+ # ```
25+ # But if you call it like this,
26+ # ```
27+ # merge_sort(elements, key='name')
28+ # ```
29+ # output will be,
30+ # ```
31+ # elements = [
32+ # { 'name': 'chinmay', 'age': 24, 'time_hours': 1.5},
33+ # { 'name': 'rajab', 'age': 12, 'time_hours': 3},
34+ # { 'name': 'vedanth', 'age': 17, 'time_hours': 1},
35+ # { 'name': 'vignesh', 'age': 21, 'time_hours': 2.5},
36+ # ]
37+ # ```
38+
39+ # [Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/algorithms/5_MergeSort/merge_sort_exercise_solution.py)
40+
41+ # def merge_sort(arr):
42+ # if len(arr) <= 1:
43+ # return
44+
45+ # mid = len(arr)//2
46+
47+ # left = arr[:mid]
48+ # right = arr[mid:]
49+
50+ # merge_sort(left)
51+ # merge_sort(right)
52+
53+ # merge_two_sorted_lists(left, right, arr)
54+
55+ # def merge_two_sorted_lists(a,b,arr):
56+ # len_a = len(a)
57+ # len_b = len(b)
58+
59+ # i = j = k = 0
60+
61+ # while i < len_a and j < len_b:
62+ # if a[i] <= b[j]:
63+ # arr[k] = a[i]
64+ # i+=1
65+ # else:
66+ # arr[k] = b[j]
67+ # j+=1
68+ # k+=1
69+
70+ # while i < len_a:
71+ # arr[k] = a[i]
72+ # i+=1
73+ # k+=1
74+
75+ # while j < len_b:
76+ # arr[k] = b[j]
77+ # j+=1
78+ # k+=1
79+
80+ # def merge_two_sorted_by_key(a,b,arr,key):
81+ # len_a = len(a)
82+ # len_b = len(b)
83+
84+ # i = j = k = 0
85+
86+ # while i < len_a and j < len_b:
87+ # if a[i][key] <= b[j][key]:
88+ # arr[k] = a[i]
89+ # i+=1
90+ # else:
91+ # arr[k] = b[j]
92+ # j+=1
93+ # k+=1
94+
95+ # while i < len_a:
96+ # arr[k] = a[i]
97+ # i+=1
98+ # k+=1
99+
100+ # while j < len_b:
101+ # arr[k] = b[j]
102+ # j+=1
103+ # k+=1
104+
105+ def merge_sort_by_key (arr , key ,descending = False ):
106+ if len (arr ) <= 1 :
107+ return arr
108+
109+ mid = len (arr )// 2
110+
111+ left = arr [:mid ]
112+ right = arr [mid :]
113+
114+ left = merge_sort_by_key (left ,key ,descending )
115+ right = merge_sort_by_key (right ,key ,descending )
116+
117+ return merge_two_sorted_lists_by_key (left , right ,key ,descending )
118+
119+ def merge_two_sorted_lists_by_key (a ,b ,key ,descending ):
120+ sorted_list = []
121+
122+ len_a = len (a )
123+ len_b = len (b )
124+
125+ i = j = 0
126+
127+ while i < len_a and j < len_b :
128+ if descending :
129+ condition = a [i ][key ] > b [j ][key ] # Note the change here for descending
130+ else :
131+ condition = a [i ][key ] <= b [j ][key ]
132+
133+ if condition :
134+ sorted_list .append (a [i ])
135+ i += 1
136+ else :
137+ sorted_list .append (b [j ])
138+ j += 1
139+
140+ while i < len_a :
141+ sorted_list .append (a [i ])
142+ i += 1
143+
144+ while j < len_b :
145+ sorted_list .append (b [j ])
146+ j += 1
147+
148+ return sorted_list
149+
150+
151+ elements = [
152+ { 'name' : 'vedanth' , 'age' : 17 , 'time_hours' : 1 },
153+ { 'name' : 'rajab' , 'age' : 12 , 'time_hours' : 3 },
154+ { 'name' : 'vignesh' , 'age' : 21 , 'time_hours' : 2.5 },
155+ { 'name' : 'chinmay' , 'age' : 24 , 'time_hours' : 1.5 },
156+ ]
157+
158+ print (merge_sort_by_key (elements ,key = "age" ,descending = False ))
0 commit comments