forked from super30admin/PreCourse-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise_4.py
More file actions
53 lines (42 loc) · 1.36 KB
/
Exercise_4.py
File metadata and controls
53 lines (42 loc) · 1.36 KB
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
# Python program for implementation of MergeSort
def mergeSort(arr):
if len(arr)>1:
# 1. Get Left and right sub arrays.
# left sub-array
lsa= arr[:len(arr)//2]
#rsa= arr[len(lsa)-1:]//2
rsa= arr[len(arr)//2:]
mergeSort(lsa)
mergeSort(rsa)
# 2. Comparing left most element in left array with the left most element in the right array.
i=j=k=0
while i<len(lsa) and j<len(rsa):
if lsa[i]<rsa[j]:
arr[k]= lsa[i]
i+=1
else:
arr[k]= rsa[j]
j+=1
k+=1
# 3. Adding all the LEFT OVER elements in the left array to the main array.
while i<len(lsa) :
arr[k]= lsa[i]
i+=1
k+=1
# 4. Adding all the LEFT OVER elements in the right array to the main array.
while j<len(rsa) :
arr[k]= rsa[j]
j+=1
k+=1
return arr
# Code to print the list
def printList(arr):
return [print(i) for i in arr]
# driver code to test the above code
if __name__ == '__main__':
arr = [12, 11, 13, 5, 6, 7]
print ("Given array is", end="\n")
printList(arr)
mergeSort(arr)
print("Sorted array is: ", end="\n")
printList(arr)