Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

project-4d White a bubble sort that counts the number of comparisons and the number of exchanges mede while sorting a list and returns a tuple

image text in transcribed
project-4d White a bubble sort that counts the number of comparisons and the number of exchanges mede while sorting a list and returns a tuple of the two values (comparibons first, exchanges second) Do the same for insertion sort. Name these functions bubble_count and insertion_count. Your functions should only count comparisons between the values that are being sonted Specificaly, this line in the insertion sort while pos > o e and a 11 st [pos]> value: contains two comparisons - the first one compares a list index to zero, but it doesnt compare values from the list so it shouldn't be counted. The second one does compare values from the list so it should be counted, but remember that if the first comparison is False, the second comparison won't be made due to the short-oircuit bechavior of the and operator (Python knows that if the first part is False, then the whole thing will be False, so there's no need to evaluate the second parth. Try sorting various lists with both functions What do you notice about the number of comparisons and exchanges made for lists of different sizes? What do you notice for lists that are nearly sorted v. lists that are nearly revensed? You don't need to submit your obsenvations, just the functions, Hint 1: For insertion sort, every time the value (that is currently getting "inserted" to its correct ploce) shifs one position to the left counts as an exchange. Watching the visualsation for insertion sort moy help make that more intuitive. Hint 2: If we assume that the insertion sort has already finished sorting all but the last value, then finding the right place for the 3 in [2,4,6,3] and finding the right place for the 1 in [2,4,6,1] would both take three comparisons, because in both cases the number being placed is finst compared to 6 , then 4 , then 2 The file must be named sorts_count.py

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

Transactions On Large Scale Data And Knowledge Centered Systems Vi Special Issue On Database And Expert Systems Applications Lncs 7600

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Stephen W. Liddle ,Klaus-Dieter Schewe ,Xiaofang Zhou

2012th Edition

3642341780, 978-3642341786

More Books

Students also viewed these Databases questions

Question

Define span of management or define span of control ?

Answered: 1 week ago

Question

What is meant by formal organisation ?

Answered: 1 week ago

Question

What is meant by staff authority ?

Answered: 1 week ago

Question

Discuss the various types of policies ?

Answered: 1 week ago