Question: def bubble_sort( seq ): n = len( seq ) for i in range( n - 1 ): # for j in range( n
def bubble_sort( seq ): """ """ n = len( seq )
for i in range( n - 1 ): # for j in range( n - 1 - i ): # if seq[j] > seq[j + 1]: # seq[j], seq[j + 1] = seq[j + 1], seq[j] return seq
Question 130 points10 mins
INTRODUCTION
You have been provided the Python code for Bubble Sort above (bubble_sort()).
Before answering questions about bubble sorting, run the following cell to define the sequence, S, which you will need to consider as input into the Bubble Sort algorithm implementation above.
RUN THE FOLLOWING CELL TO GENERATE THE SEQUENCE, S
import random
try: random.seed( int( student_id )**7 ) S = set() N = 10 while len( S ) < N: # P = random.randint( 10, 100 ) R = random.randint( 0, P ) S.add( R ) S = list( S ) except Exception as e: print( "*** DO NOT CONTINUE THE EXAM!!!" ) print( " Make sure your student ID is defined above!" ) print( f"{e}") S
Question 1 / Part 115 points5 mins
Provide the state of the sequence S after the third pass completes.
Store your answer in the variable named student_answer already provided to you below.
Your answer must be a Python list reflecting the state of the input sequence for the particular pass you are being asked about.
Do not write any code that manipulates/modifies S!!!
Example:
student_answer = [5, 28, 1, 29, 44, 94, 33, 7, 82]
YOU MUST WRITE YOUR ANSWER IN THE CODE CELL BELOW
In [ ]:
student_answer = None
# YOUR CODE HERE
pass
In [ ]:
Question 1 / Part 215 points5 mins
Provide the state of the sequence S after the seventh pass completes.
Store your answer in the variable named student_answer already provided to you below.
Your answer must be a Python list reflecting the state of the input sequence for the particular pass you are being asked about.
Do not write any code that manipulates/modifies S!!!
Example:
student_answer = [5, 28, 1, 29, 44, 94, 33, 7, 82]
YOU MUST WRITE YOUR ANSWER IN THE CODE CELL BELOW
In [ ]:
student_answer = None
# YOUR CODE HERE
pass
In [ ]:
Question 240 points10 mins
Starting with the bubble_sort() function implementation above, define a new version of the same function that supports a reverse keyword argument to control sort ordering. When reverse=False, bubble_sort() should sort in ascending order. When reverse=True, bubble_sort() should sort in descending order.
A copy of the above Bubble Sort implementation has been provided to you with the updated API below with the name bubble_sort_student().
So modify the given implementation below to provide this functionality.
Examples:
>>> S = [10, 51, 2, 18, 4, 31, 13, 5, 23, 64, 29] >>> bubble_sort_student( S, reverse=False ) [2, 4, 5, 10, 13, 18, 23, 29, 31, 51, 64] >>> S = [10, 51, 2, 18, 4, 31, 13, 5, 23, 64, 29] >>> bubble_sort_student( S, reverse=True ) [64, 51, 31, 29, 23, 18, 13, 10, 5, 4, 2]
YOU MUST WRITE YOUR ANSWER IN THE CODE CELL BELOW
In [ ]:
def bubble_sort_student( seq, reverse=False ):
"""
"""
n = len( seq )
for i in range( n - 1 ):
#
for j in range( n - 1 - i ):
#
if seq[j] > seq[j + 1]:
#
seq[j], seq[j + 1] = seq[j + 1], seq[j]
return seq
# YOUR CODE HERE
pass
In [ ]:
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
