10000 Added Merge Sort · AllAlgorithms/python@8f7135a · GitHub
[go: up one dir, main page]

Skip to content

Commit 8f7135a

Browse files
committed
Added Merge Sort
1 parent f1e1ce3 commit 8f7135a

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def merge(left_subarray,right_subarray):
2+
i,j = 0,0
3+
result = []
4+
5+
while i<len(left_subarray) and j<len(right_subarray):
6+
if left_subarray[i] <= right_subarray[j]:
7+
result.append(left_subarray[i])
8+
i += 1
9+
else:
10+
result.append(right_subarray[j])
11+
j += 1
12+
result += left_subarray[i:]
13+
result += right_subarray[j:]
14+
return result
15+
16+
def merge_sort(input_list):
17+
if len(input_list) <= 1:
18+
return input_list
19+
else:
20+
midpoint = int(len(input_list)/2)
21+
left_subarray = merge_sort(input_list[:midpoint])
22+
right_subarray = merge_sort(input_list[midpoint:])
23+
return merge(left_subarray,right_subarray)
24+
25+
number_list = [8, 3, 1, 10, 111, 56, 999, 2]
26+
print merge_sort(number_list)

0 commit comments

Comments
 (0)
0