Skip to content

Commit a8e7ebb

Browse files
authored
mergeSort
1 parent 3896a45 commit a8e7ebb

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

mergeSort

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
"""
2+
# Programmer : Dhruv Patel
3+
# Problem Name : Merge Sort
4+
# Used In : Python
5+
# Used As : Practice - Divide and Conquer
6+
# Problem =>
7+
# Implementation of Selection - sort
8+
# Thoughts =>
9+
# Merge Sort implemented with divide and conquer approach.
10+
# It first merges the elements into the smaller parts till it gets to size 1.
11+
# Then we merge the result into the new array which will merge all parts.
12+
# The divide step will take log(n) time and merging require each elements to
13+
# visit hence the total time-complexity is (n log(n)) regardless of the input order.
14+
# We also use recursion here where every time we call the function it calls up
15+
# a memory stack frame and foot print as well. Also, the additional space require
16+
# while merging an array hence the space complexity in O(n)
17+
# i.e,
18+
# Split and merge for [4,3,2,1] will look like this,
19+
# _____|_____ ___________________________
20+
# | | |
21+
# ___|___ ___|___ |
22+
# [4,3] [2,1] Divide/Split an array |
23+
# ____|____ ___|____ into two parts till the |
24+
# | || | size is 1 |
25+
# [4] [3][2] [1] ___________________________|
26+
# | | | |
27+
# ------- ------- ---------------------------
28+
# | | |
29+
# [3,4] [1,2] Merge smallers parts into |
30+
# | | sorted result till we get |
31+
# ---------- the final answer |
32+
# [1,2,3,4] --------------------------
33+
#
34+
#
35+
# Time - Complexity =>
36+
# Best - Ω(n log(n))
37+
# Average - Θ(n log(n))
38+
# Worst - O(n log(n))
39+
40+
# Space - Complexity =>
41+
Worst - O(n)
42+
43+
import random
44+
import time
45+
46+
47+
def merge(l, r):
48+
answer = []
49+
i = 0
50+
j = 0
51+
while i < len(l) and j < len(r):
52+
if l[i] > r[j]:
53+
answer.append(r[j])
54+
j = j + 1
55+
else:
56+
answer.append(l[i])
57+
i = i + 1
58+
while i < len(l):
59+
answer.append(l[i])
60+
i = i + 1
61+
while j < len(r):
62+
answer.append(r[j])
63+
j = j + 1
64+
return answer
65+
66+
67+
def mergeSort(x):
68+
if len(x) == 0 or len(x) == 1:
69+
return x
70+
else:
71+
middle = len(x) // 2
72+
left = mergeSort(x[0:middle])
73+
right = mergeSort(x[middle:])
74+
return merge(left, right)
75+
76+
77+
def fillList(x):
78+
array = []
79+
for i in range(x):
80+
array.append(random.randint(-999, 999))
81+
return mergeSort(array)
82+
start = time.time()
83+
fillList(200000)
84+
print("For sorting 200000 elements mergeSort took time",
85+
round(time.time()-start))
86+
start = time.time()
87+
fillList(400000)
88+
print("For sorting 400000 elements mergeSort took time",
89+
round(time.time()-start))
90+
start = time.time()
91+
fillList(800000)
92+
print("For sorting 800000 elements mergeSort took time",
93+
round(time.time()-start))
94+
start = time.time()
95+
fillList(1600000)
96+
print("For sorting 1600000 elements mergeSort took time",
97+
round(time.time()-start))
98+
start = time.time()
99+
fillList(3200000)
100+
print("For sorting 3200000 elements mergeSort took time",
101+
round(time.time()-start))
102+
start = time.time()
103+
fillList(6400000)
104+
print("For sorting 6400000 elements mergeSort took time",
105+
round(time.time()-start))
106+
107+
"""
108+
Output:-
109+
For sorting 200000 elements mergeSort took time 8
110+
For sorting 400000 elements mergeSort took time 16
111+
For sorting 800000 elements mergeSort took time 34
112+
For sorting 1600000 elements mergeSort took time 71
113+
For sorting 3200000 elements mergeSort took time 143
114+
"""

0 commit comments

Comments
 (0)