I have some code further below which is doing a merge sort. It was implemented using a python Array of ints (list of ints) as my initial test case. This went well. However, once this was working, I generated a random set of numbers to test further. This list is a numpy array. The merge sort does not work with the numpy array, the results look like repeated figures and like the reconstruction isn't working as intended.
My assumption is that the way the subdivided array is passed to mergesort and then recombined in memory is different between the two, and that is what is causing the issue. However, I am not certain.
Any thoughts?
example arrays: working with:
Array = [48,44,19,59,72,80,42,65,82,8,95,68]
not working with
unsorted = np.random.randint(1, 1000, 150)
The merge sort code:
#function for merging two sub-arrays
def merge(left, right, Array):
i = 0
j = 0
k = 0
while (i < len(left) and j < len(right)):
if (left[i] < right[j]):
Array[k] = left[i]
i = i+1
else:
Array[k] = right[j]
j = j+1
k = k+1
while (i < len(left)):
Array[k] = left[i]
i = i+1
k = k+1
while (j < len(right)):
Array[k] = right[j]
j = j+1
k = k+1
#function for dividing and calling merge function
def mergesort(Array):
n = len(Array)
if (n < 2):
return
mid = n / 2
left = Array[0 : int(np.round(mid))]
right = Array[int(np.round(mid)) : n]
print("array size = {}, leftsize= {}, rightsize= {}".format(n, len(left), len(right)))
mergesort(left)
mergesort(right)
merge(left, right, Array)
return Array
Then just need to call the following to compare the output:
mergesort(Array)
mergesort(unsorted)
mid = n // 2i/oint(np.round(mid))?unsortedis anarraywhileArrayis alist.listis not just an name for anarray. The only things they have in common is that they use the[ ]syntax and that they are an ordered collection of items.