Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Coderunner About If the bubble sort completes a pass where it doesn't perform any swaps, then we know the entire list must already be ordered,

image text in transcribed

Coderunner About If the bubble sort completes a pass where it doesn't perform any swaps, then we know the entire list must already be ordered, so we can immediately return from the function. First, modify the bubble_sort() function to be more efficient by immediately stopping once the list is sorted (i.e. if there is an entire pass of the list in which no swaps are performed). Also, rename the function from "bubble sort" to bubble_sort_fast" to reflect this increase in efficiency Your function should sort the list given into ascending order, but also return the number of elements, the number of comparisons and number of swaps that occurred during the sorting. The function must return these 3 values as a tuple. Submit the bubble_sort_fast() function in the answer box below. You may not use Python's built in sort() or sorted functions. For example: Test Result Fast Bubble Sort: Length: 5 Comparisons: 4 Swaps: 0 #Normal Bubble Sort: Length: 5 Comparisons: 10 Swaps: 0 d2 = [12, 15, 19, 37, 39] result2 = bubble_sort_fast(d2) print('Fast Bubble Sort: Length: {} Comparisons: {} Swaps: Un format (result2[@], result2[1], result2[2])) Fast Bubble Sort: Length: 5 Comparisons: 10 Swaps: 10 #Normal Bubble Sort: Length: 5 Comparisons: 10 Swaps : 10 d2 = [84, 62, 38, 36, 24] result2 = bubble_sort_fast (2) print('Fast Bubble Sort: Length: { Comparisons: {} Swaps format (result2[@], result2[1], result2[2]>> Answer: (penalty regime: 0.0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 %) Reset answer 1 def bubble_sort_fast (data): 2 Length-1 3 Comparisons=0 4 Swaps=0 5 for num_steps in range(len (data) - 1, 0, -1): 6 Length+=1 for i in range(num_steps) Comparisons=1 if data[i]>data[i+1]: 10 a=data[i] 11 c=data[i+1] 12 del data[i] 13 data, insert(in) 14 del data[i+1] 15 data.insert(i+1, a) 16 Swaps +=1 17 18 return Length, Comparisons, Swaps

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeff Hoffer, Ramesh Venkataraman, Heikki Topi

13th Edition Global Edition

1292263350, 978-1292263359

More Books

Students also viewed these Databases questions

Question

What is the difference between Needs and GAP Analyses?

Answered: 1 week ago

Question

What are ERP suites? Are HCMSs part of ERPs?

Answered: 1 week ago