Skip to content

Add Merge Sort algo chapter and Python example #163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
3 changes: 2 additions & 1 deletion CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ Hitesh C
Maxime Dherbécourt
Jess 3Jane
Pen Pal
Chinmaya Mahesh
Mukundan
Chinmaya Mahesh
40 changes: 40 additions & 0 deletions chapters/sorting_searching/merge/code/python/merge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import random


def merge(l1, l2):
i = j = 0
l = []

while (i < len(l1)) and (j < len(l2)):
if l1[i] < l2[j]:
l.append(l1[i])
i += 1
else:
l.append(l2[j])
j += 1

l.extend(l1[i:len(l1)])
l.extend(l2[j:len(l2)])

return l


def merge_sort(l):
if len(l) == 1:
return l

l1 = merge_sort(l[0:len(l)//2])
l2 = merge_sort(l[len(l)//2:len(l)])

return merge(l1, l2)


def main():
number = [random.randint(0, 10000) for _ in range(10)]
print("Before Sorting {}".format(number))
number = merge_sort(number)
print("After Sorting {}".format(number))


if __name__ == "__main__":
main()
29 changes: 29 additions & 0 deletions chapters/sorting_searching/merge/merge_sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# This Chapter is comming soon
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"coming" is spelled with only one "m".


{% method %}
{% sample lang="py" %}
[import:1-40, lang:"python"](code/python/merge.py)
{% endmethod %}

<script>
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
</script>
$$
\newcommand{\d}{\mathrm{d}}
\newcommand{\bff}{\boldsymbol{f}}
\newcommand{\bfg}{\boldsymbol{g}}
\newcommand{\bfp}{\boldsymbol{p}}
\newcommand{\bfq}{\boldsymbol{q}}
\newcommand{\bfx}{\boldsymbol{x}}
\newcommand{\bfu}{\boldsymbol{u}}
\newcommand{\bfv}{\boldsymbol{v}}
\newcommand{\bfA}{\boldsymbol{A}}
\newcommand{\bfB}{\boldsymbol{B}}
\newcommand{\bfC}{\boldsymbol{C}}
\newcommand{\bfM}{\boldsymbol{M}}
\newcommand{\bfJ}{\boldsymbol{J}}
\newcommand{\bfR}{\boldsymbol{R}}
\newcommand{\bfT}{\boldsymbol{T}}
\newcommand{\bfomega}{\boldsymbol{\omega}}
\newcommand{\bftau}{\boldsymbol{\tau}}
$$