Question
Python 3.6- Create a program generate randome numbers and write them to a txt file called Random.txt (already did this). The user is asked to
Python 3.6-
Create a program generate randome numbers and write them to a txt file called "Random.txt" (already did this). The user is asked to input the range over which the random numbers will be generated, and how many numbers are desired. The Python pseudorandom number generator is then used to create random numbers. Open the "Random.txt" file and sort it by merge sort (need help with this). The sorted output must be written to a new file called "mergeS.txt" Objective: start a timer, sort random numbers using merge sort, stop timer, note elapsed time, merge sort results must be in output file called "mergeS.txt" . Please provide indented source code and screenshot of mergeS.txt to receive full credit. Thanks
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started