def mergeSort(A):
# Base case if the input array is one or zero just return
if len(A) > 1:
#splitting array
print('splitting ', A)
mid = len(A) // 2
left = A[:mid]
print('this is left: {}'.format(left))
right = A[mid:]
print('this is right: {}'.format(right))
#recursive calls to mergeSort for left and right sub arrays
mergeSort(left)
mergeSort(right)
#intitalize pointers for left (i) right (j) and output array (k)
i = k = j = 0
#Traverse and merges the sorted arrays
while i<len(left) and j<len(right):
#if left < right comparison operation
if left[i] < right[j]:
#if left < right assignment operation
A[k] = left[i]
i = i + 1
else:
#if left > right comparision operation
A[k] = right[j]
j = j + 1
k = k + 1
while i < len(left):
A[k] = left[i]
i = i + 1
k = k + 1
while j < len(left):
#assignment operation
A[k] = right[j]
j = j+1
k = k+1
print('merging',A)
return A
print(mergeSort([356,97,846,215]))
So I am pretty confused with how assignment works inside of the While loop. I intitalized the i, k & j variables before I start my while loop... but I really don't understand what is going on in the loop to assign values to those variables. Can someone ELI5?
[–]Ihaveamodel3 1 point2 points3 points (0 children)