Python program - looking for help coding a bucket sort with some specific criter
ID: 3707791 • Letter: P
Question
Python program - looking for help coding a bucket sort with some specific criteria
I need to implement a bucket sort and keep track of execution times of varying inputs, (in nanosec, so if using time() function *1e+9)
the inputs should be read in from an input file, "input.txt" with a very large number of integers(~100000), output is a little funky, I need to first take the first 1000 numbers, perform a bucket sort then return the exceution time, then call back to the original(unsorted) list take 2000 integers then sort them, then get the execution time, then 3000 and so on. then print the execution times, in a format I can easily paste into excel to create a graph.
I've been using
https://www.random.org/integers/?num=10000&min=1&max=10000000&col=5&base=10&format=html&rnd=new
to get a seed of random integers for it to work with.
Explanation / Answer
PROGRAM:
# first we need to read the numbers in a list
numbers = []
with open('input.txt') as f:
for line in f:
# ignore blank whitespace lines
if not line.strip():
numbers.append(int(line))
# all numbers have been read now
# take start size of 1000:
size = 1000
start = 0
# till the time there is some numbers to be processed
while (start + size) <= len(numbers):
# fetch the list of size, which starts from start pointer
sublist = numbers[start:start+size+1]
# sort the sublist using mergesort
# and then calculate the time
# i hope you know how to do that.
start = time.time()
# call merge sort function here
end = time.time()
print(end - start, ' secs time elapsed')
# update the start pointer now to process next set of data
start = start + size
size = size + 1000
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.