From 5dad3b7df58c0eb74e0c8fa799a9e23f2c2c7c0f Mon Sep 17 00:00:00 2001 From: Sunny Pate Date: Sat, 13 Oct 2018 12:21:44 +1100 Subject: [PATCH] Merge Sort without Slicing Basically in merge sort we use slice function so the above code is similat to quick sort which helps merge sort to use without slicing --- .DS_Store | Bin 0 -> 6148 bytes sorting/MergesortWithoutSlicing.py | 60 +++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 .DS_Store create mode 100644 sorting/MergesortWithoutSlicing.py diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..1944ae096e08d1ea5c636e3be00d6df20475c8cb GIT binary patch literal 6148 zcmeHKO>Wab6n@jD#0~PY`xshY&^cqBr03y!VYWpXC`35wZR(>=Ctz$bt)Ow9x#*L|*us zZRj46WO9s@d>Z2ykCL@&(_s`a3jAvdh zN%v?(2Q;Jytv(0$-FI-#(}cvoXwqzvPZ zX$BNgia4Si+$o*bxL<4BCh9m?1!kYpakW~``F&;1mFi%gPqjjygJ}}yMYsEfZEQBS z&NVHoWwkD}UU(Be_X@w5<$XVUCaxa(#U#-2W6qNwu^&8%qxoU`@&Qi^KTe}elH)K! z$@8ai8uEFc&(biL<9I4zS+->#ws#hby<68i&TjAKvg0iF_q!cuZ}-M>XGR(;5Yg0#yYz)n!}k|HI#(|EnZ(WfU+9{8tLF#=sl& zF(tjXu1!wtwGR9UE=<&w3MB=N-i~D;w&ESQG~^r+09}oxLiE7Qhk%s9G)95Hs=#+I COrW3u literal 0 HcmV?d00001 diff --git a/sorting/MergesortWithoutSlicing.py b/sorting/MergesortWithoutSlicing.py new file mode 100644 index 0000000..7883024 --- /dev/null +++ b/sorting/MergesortWithoutSlicing.py @@ -0,0 +1,60 @@ + +# coding: utf-8 + +# In[1]: + + +#Merge Slot Without Slicing +#start is the index of the first item +#end is the index past the the last item +def indexMergeSort(alist, start = None, end = None): + if start == None: + start = 0 + if end == None: + end = len(alist) + #note that the print operations still use slicing for convenience + #print("Input: ",alist[start:end]) + + length = end - start + if length > 1: + #print("Splitting ",alist[start:end]) + mid = start + length//2 + + indexMergeSort(alist, start, mid) + indexMergeSort(alist, mid, end) + + i=start # index for the left part of the list + j=mid # index for the right part of the list + #we use a temporary list + templist = [None] * (length) + k = 0 #index for the temp list + while i < mid and j < end: + if alist[i] < alist[j]: + templist[k] = alist[i] + i=i+1 + else: + #we swap + templist[k] = alist[j] + j=j+1 + k=k+1 + + while i < mid: + templist[k] = alist[i] + i=i+1 + k=k+1 + + while j < end: + templist[k]=alist[j] + j=j+1 + k=k+1 + + #we copy the results + for k in range(length): + alist[start+k] = templist[k] + + #print("Merging ",alist[start:mid], alist[mid:end]) + +alist = [54,26,93,17,77,31,44,55,20] +indexMergeSort(alist) +print(alist) +