From fd43c3832041769e11d42d6bac6300f549ed4d33 Mon Sep 17 00:00:00 2001 From: Sumanth Palla <64864074+sumanthpalla@users.noreply.github.com> Date: Mon, 7 Jun 2021 18:27:16 +0530 Subject: [PATCH] chore(Python): added Merge Sort algorithm (#329) Co-authored-by: Sumanth Co-authored-by: Arsenic <54987647+Arsenic-ATG@users.noreply.github.com> --- algorithms/Python/README.md | 1 + algorithms/Python/sorting/merge_sort.py | 34 +++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 algorithms/Python/sorting/merge_sort.py diff --git a/algorithms/Python/README.md b/algorithms/Python/README.md index f9c50fa4..d715cdf0 100644 --- a/algorithms/Python/README.md +++ b/algorithms/Python/README.md @@ -38,6 +38,7 @@ 6. [Heap Sort](sorting/heap-sort.py) 7. [Radix Sort](sorting/radix-sort.py) 8. [Shell Sort](sorting/shell-sort.py) +9. [Merge sort](sorting/merge_sort.py) ## Strings diff --git a/algorithms/Python/sorting/merge_sort.py b/algorithms/Python/sorting/merge_sort.py new file mode 100644 index 00000000..cb885ca3 --- /dev/null +++ b/algorithms/Python/sorting/merge_sort.py @@ -0,0 +1,34 @@ +## Implementation of merge sort +## Merge Sort is a recursive algorithm which sorts the list by divide and conquer approach +def merge_sort(list): + # if list size <= 1, the list is anyway sorted! :) + if len(list)==1: return list + #splitting the list into half + mid = len(list)//2 # integer division + l = list[:mid] + r = list[mid:] + merge_sort(l) # recursive calling of left part of list + merge_sort(r) # recursive calling of right part of list + #merging the left and right parts + i=j=k=0 + while(i