-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergeSort.py
51 lines (37 loc) · 933 Bytes
/
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
# -*- coding: utf-8 -*-
"""
Created on Sat Jan 4 13:57:00 2020
@author: Akshita
Note: Not working properly at the moment! Figure out later.
"""
def merge(nums,L,R):
i=j=k=0
while i < len(L) and j < len(R):
if L[i]<R[j]:
nums[k] = L[i]
i = i+1
else:
nums[k] = R[j]
j = j+1
k = k+1
if i < len(L):
while i!=len(L):
nums[k] = L[i]
i = i+1
k = k+1
elif j < len(R):
while j!=len(R):
nums[k] = R[j]
j = j+1
k = k+1
def mergeSort(nums):
if len(nums) > 1:
mid = len(nums)//2
mergeSort(nums[:mid])
mergeSort(nums[mid:])
L = nums[:mid]
R = nums[mid:]
return merge(nums,L,R)
nums = [34, 2, 98, 74, 63, 102]
mergeSort(nums)
print(nums)