Simple Python Program #4 of 5 I have 4 other programs posted as well, they are a
ID: 3582233 • Letter: S
Question
Simple Python Program #4 of 5
I have 4 other programs posted as well, they are all simple, but I don't have time to do them. Due in a few hours.
Playing Computer with Bubble
Sort Bubble sort is a simple algorithm for sorting a list – the first sorting algorithm that most people learn.
There are lots of other algorithms that are faster most of the time, but this one is easy to analyze.
The algorithm proceeds by passing through the list multiple time and comparing adjacent elements. If the two items are in the wrong order, the order is switched.
For the three-argument range function, see “the Most General Range Function,” in the beginning.
Fill out and submit Problem-3-Playing-Computer-Bubble-Sort.doc. Bubble Sort 1 def bubbleSort(alist): 2 for pass in range(len(alist)-1, 0, -1): # see “most general range” fn 3 for i in range(pass): 4 if alist[i] > alist[i+1]: 5 temp = alist[i] 6 alist[i] = alist[i+1] 7 alist[i+1] = temp 8 print(alist) 9 bubbleSort([17, 31, 77, 44, 55, 20]) 10 . . .
Explanation / Answer
solution
def bubbleSort(alist):
for pass in range(len(alist)-1,0,-1):
for i in range(pass):
if alist[i]>alist[i+1]:
temp = alist[i]
alist[i] = alist[i+1]
alist[i+1] = temp
alist = [17,31,77,44,55,20]
bubbleSort(alist)
print(alist)
output
[17, 20, 31, 44, 55, 77]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.