Skip to content

Commit 63b706e

Browse files
committed
added iterative code
1 parent 5c0973e commit 63b706e

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

pygorithm/sorting/merge_sort.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,25 @@ def sort(_list):
4848
b = sort(_list[middle:])
4949
return merge(a, b)
5050

51-
def sort_iter(_list):
51+
from itertools import zip_longest
52+
def sorti(_list):
5253
"""
5354
Function to sort an array
5455
using merge sort algorithm, iteratively
5556
5657
:param _list: list of values to sort
5758
:return: sorted
5859
"""
59-
pass
60+
# breakdown every element into its own list
61+
series = [[i] for i in _list]
62+
while len(series) > 1:
63+
# iterator to handle two at a time in the zip_longest below
64+
isl = iter(series)
65+
series = [
66+
merge(a, b) if b else a
67+
for a, b in zip_longest(isl, isl)
68+
]
69+
return series[0]
6070

6171
# TODO: Are these necessary?
6272
def time_complexities():

0 commit comments

Comments
 (0)