Question: Implementation in Python3 Need help using a file named input.txt which lists an array ( first value is number of values and not printed so

Implementation in Python3

Need help using a file named input.txt which lists an array ( first value is number of values and not printed so for file using 4 8 1 11 2 only 1 2 8 11 are displayed in output.txt file and the sorted results must go to output.txt. where should i put these two files in the following code? user prompts asking about entering array size are not needed. they were used to check initial code. thank you

#4-way mergeSort

def merge(A, l,m2,m1, m3, h): n1 = m2 - l + 1#calculating length of subarrays n2 = m1- m2 n3 = m3-m1 n4 = h-m3 Ln = m1-l+1 Rn = h-m1 # creating temporary arrays a = [0] * (n1) b = [0] * (n2) c = [0] * (n3) d = [0] * (n4) L = [0] * (Ln) R = [0] * (Rn) # Copy data to temp arrays L[] and R[] for i in range(0 , n1): a[i] = A[l + i] for i in range(0 , n2): b[i] = A[m2+1 + i] for i in range(0 , n3): c[i] = A[m1+1 + i] for i in range(0 , n4): d[i] = A[m3+1 + i] i1=0 i2=0 k=0 while i1 < n1 and i2 < n2 :#merging first two part if a[i1] <= b[i2]: L[k] = a[i1] i1 += 1 else: L[k] = b[i2] i2 += 1 k += 1 while i1 < n1: L[k] = a[i1] i1 += 1 k += 1 while i2 < n2: L[k] = b[i2] i2 += 1 k += 1 i1=0 i2=0 k=0 while i1 < n3 and i2 < n4 : if c[i1] <= d[i2]: R[k] = c[i1] i1 += 1 else: R[k] = d[i2] i2 += 1 k += 1 while i1 < n3: R[k] = c[i1] i1 += 1 k += 1 # Copy the remaining elements of R[], if there # are any while i2 < n4: R[k] = d[i2] i2 += 1 k += 1

i=0 j=0 k=l while i < Ln and j < Rn : if L[i] <= R[j]: A[k] = L[i] i += 1 else: A[k] = R[j] j += 1 k += 1 while i < Ln: A[k] = L[i] i += 1 k += 1 # Copy the remaining elements of R[], if there # are any while j < Rn: A[k] = R[j] j += 1 k += 1 def mergeSort(A,low,high): if low < high: m1 = int((low+(high))/2) m2 = int((low+m1)/2) m3 = int(((m1+1)+high)/2) mergeSort(A, low, m2) mergeSort(A, m2+1, m1) mergeSort(A, m1+1, m3) mergeSort(A, m3+1, high) merge(A, low,m2, m1,m3, high) print ("Enter array size") arr_size = int(input()) print ("Enter array element") A = [int(x) for x in input().split()] mergeSort(A,0,arr_size-1) print (" Your array after Sorting is") for i in range(arr_size): print ("%d" %A[i]),

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!