I\'m just starting to use Python and I would like to get some short comments exp
ID: 3554179 • Letter: I
Question
I'm just starting to use Python and I would like to get some short comments explaining this code I copied online. I have an assignment that requires me to write a bubble sort algorithm that reads from a file that contains one number per line. Thanks.
def bubblesort(lst):
"Sorts lst in place and returns it."
for passesLeft in range(len(lst)-1, 0, -1):
for index in range(passesLeft):
if lst[index] > lst[index + 1]:
lst[index], lst[index + 1] = lst[index + 1], lst[index]
return lst
Explanation / Answer
def bubblesort( A ):
for i in range( len( A ) ):
for k in range( len( A ) - 1, i, -1 ):
if ( A[k] < A[k - 1] ):
swap( A, k, k - 1 )
def swap( A, x, y ):
tmp = A[x]
A[x] = A[y]
A[y] = tmp
This is your bubble sort code..
you just need to output A array now
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.