From 8b30856a5d0a23f015442b54124c73f6d51475f7 Mon Sep 17 00:00:00 2001 From: Saurabh Kumar <47217012+Saurabhsaha027@users.noreply.github.com> Date: Sat, 1 Aug 2020 14:46:08 +0530 Subject: [PATCH] Create Merge_Sort.py --- Sorting/Merge Sort/Merge_Sort.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Sorting/Merge Sort/Merge_Sort.py diff --git a/Sorting/Merge Sort/Merge_Sort.py b/Sorting/Merge Sort/Merge_Sort.py new file mode 100644 index 00000000..6dbd5649 --- /dev/null +++ b/Sorting/Merge Sort/Merge_Sort.py @@ -0,0 +1,31 @@ +def mergeSort(nlist): +if len(nlist)>1: + mid = len(nlist)//2 + lefthalf = nlist[:mid] + righthalf = nlist[mid:] + + mergeSort(lefthalf) + mergeSort(righthalf) + i=j=k=0 + while i < len(lefthalf) and j < len(righthalf): + if lefthalf[i] < righthalf[j]: + nlist[k]=lefthalf[i] + i=i+1 + else: + nlist[k]=righthalf[j] + j=j+1 + k=k+1 + + while i < len(lefthalf): + nlist[k]=lefthalf[i] + i=i+1 + k=k+1 + + while j < len(righthalf): + nlist[k]=righthalf[j] + j=j+1 + k=k+1 + +nlist = [3,1,4,1,5,9,2,6,5,4] +mergeSort(nlist) +print(nlist)