File tree Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Original file line number Diff line number Diff line change 1+ #Merge Sort python code
2+
3+ def mergeSort(arr):
4+ if len(arr) > 1:
5+ mid = len(arr)//2
6+ left = arr[:mid]
7+ right = arr[mid:]
8+ mergeSort(left)
9+ mergeSort(right)
10+
11+ i = j = k = 0
12+
13+ # Copy data to temporary arrays left and right
14+ while i < len(left) and j < len(right):
15+ if left[i] <= right[j]:
16+ arr[k] = left[i]
17+ i += 1
18+ else:
19+ arr[k] = right[j]
20+ j += 1
21+ k += 1
22+
23+ # Checking if any element was left in array
24+ while i < len(left):
25+ arr[k] = left[i]
26+ i += 1
27+ k += 1
28+
29+ while j < len(right):
30+ arr[k] = right[j]
31+ j += 1
32+ k += 1
33+
34+
35+
36+ # Code to print the list
37+ def printList(arr):
38+ for i in range(len(arr)):
39+ print(arr[i], end=" ")
40+ print()
41+
42+
43+ # Driver Code
44+ if __name__ == '__main__':
45+ arr = [int(i) for i in input().split()]
46+ print("Given array is", end="\n")
47+ printList(arr)
48+ mergeSort(arr)
49+ print("Sorted array is: ", end="\n")
50+ printList(arr)
You can’t perform that action at this time.
0 commit comments