Python 3 Write 3 programs to do the 3 variations on the Bubble Sort. Comment the
ID: 3858735 • Letter: P
Question
Python 3
Write 3 programs to do the 3 variations on the Bubble Sort.
Comment the first variation completely.
Another big deal in programming is performance. For large files it makes a difference if a program uses a few extra processor cycles to do a job. I want you to see how to do performance testing (benchmarking), at least on a simple level. To do that I want you to compare 3 ways of writing a bubble sort in Python. Those ways are:
1. Do the sorting directly in the main body of the program.
2. Do the sorting entirely in a separate function called from main.
3. Do just the element switching needed as a function called from main.
1to9.txt file data follows
6
8
1
4
9
5
3
2
7
higher.digit.file.txt - This file contains several hundred 4 digit numbers similar to the following.
import time
text_file = open("C:\Users\doole\OneDrive\JCTC\CIT 144\Files\1to9.txt", "r")
lines = text_file.readlines()
# print (lines)
# print (len(lines))
start = time.time()
for passnum in range(len(lines)-1,0,-1):
print (passnum)
for i in range(passnum):
# print(i)
if lines[i]>lines[i+1]:
temp = lines[i]
lines[i] = lines[i+1]
lines[i+1] = temp
print(lines)
end = time.time()
print ("Elapsed time = ", end-start)
#for line in lines:
# print (line)
text_file.close()
Explanation / Answer
NOTE: Please check the codes in link given below and let me know if you face any issues. I will revert back within 24 hours.
1. Do the sorting directly in the main body of the program.
Code in link:- http://pasted.co/8eb9089e
2. Do the sorting entirely in a separate function called from main.
Code in link:- http://pasted.co/4b4c3c3c
3. Do just the element switching needed as a function called from main.
Code in link:- http://pasted.co/be0fc38e
Code execution and output:
https://pasteboard.co/GBOIXrD.png
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.