Browse Source

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
pull/4/head
Sunny Pate 6 years ago
parent
commit
5dad3b7df5
2 changed files with 60 additions and 0 deletions
  1. BIN
      .DS_Store
  2. +60
    -0
      sorting/MergesortWithoutSlicing.py

BIN
.DS_Store View File


+ 60
- 0
sorting/MergesortWithoutSlicing.py View File

@ -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)

Loading…
Cancel
Save