Question: Python 3.6- PROVIDE INDENTED SOURCE CODE AND MERGE SORT OUTPUT FILE FOR FULL CREDIT. I generated random numbers using import random. I need you to
Python 3.6- PROVIDE INDENTED SOURCE CODE AND MERGE SORT OUTPUT FILE FOR FULL CREDIT. I generated random numbers using import random. I need you to sort the random numbers by merge sort, start and stop the timer, and create an output file with merge sort results. Here is an algorithm that you might use for merge sort (but you can use any algorithm as long as it works with my source code) :
def merge_sort(A): """ Sort list A into order, and return result. """ n = len(A) if n==1: return A mid = n//2 # floor division L = merge_sort(A[:mid]) R = merge_sort(A[mid:]) return merge(L,R) def merge(L,R): """ Given two sorted sequences L and R, return their merge. """ i = 0 j = 0 answer = [] while iHere is my source code:
import time import random randfile = open("Random.txt", "w") start = int(input('Enter lower limit of random numbers: ')) end = int(input('Enter upper limit of random numbers: ')) for i in range(int(input('How many to generate?: '))): line = str(random.randint(start, end)) randfile.write(line + ' ') print(line) randfile.close() # example of selection sort algorithm : needs modification def swap(a, i, j): (a[i], a[j]) = (a[j], a[i]) def selectionSort(a): n = len(a) for startIndex in range(n): minIndex = startIndex for ind in range(startIndex+1, n): if a[ind] < a[minIndex]: minIndex = ind swap(a, startIndex, minIndex) lst = [] with open("Random.txt", "r") as f: for line in f: lst.append(int(line.strip())) start_time = time.time() selectionSort(lst) end_time = time.time() print('Sorted list: ', lst) print('Elapsed time: {:.20f} seconds'.format(end_time-start_time))
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
