subject

Write a bubble sort that counts the number of comparisons and the number of exchanges made while sorting a list and returns a tuple of the two values (comparisons first, exchanges second). Do the same for insertion sort. Name these functions bubble_count and insertion_count. 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 vs. lists that are nearly reversed? You don't need to submit your observations, just the functions. Your functions should only count comparisons between values in the list. The file must be named: sorts_count. pyHere is my code. Still getting an error on the insertion sort function:pass list(range(10, 0, -1)) (0.0/10.0)Test Failed: 54 != 45def bubble_count(a_list): """ Function that bubble sorts the number of comparisons and exchanges and returns a tuple of the two values """ comparisons = 0 exchanges = 0 # loop from 0 to length - 1 for i in range(len(a_list)): for j in range(len(a_list) - i - 1): comparisons += 1 # compare elements if a_list[j] > a_list[j + 1]: # swap elements a_list[j], a_list[j + 1] = a_list[j + 1], a_list[j] exchanges += 1 return comparisons, exchangesdef insertion_count(a_list): """ Function that bubble sorts the number of comparisons and exchanges and returns a tuple of the two values """ comparisons = 0 exchanges = 0 # first element would be sorted, start from 2nd element for i in range(1, len(a_list)): key = a_list[i] # Move elements of arr[0..i-1], that are # greater than key to one place ahead j = i - 1 # 1 comparison would surely happen comparisons += 1 while j >= 0 and key < a_list[j]: # exchange exchanges += 1 a_list[j + 1] = a_list[j] j -= 1 comparisons += 1 # place key at the position left a_list[j + 1] = key return comparisons, exchanges

ansver
Answers: 3

Other questions on the subject: Computers and Technology

image
Computers and Technology, 23.06.2019 07:30, devnnn44
What is the original authority for copyright laws
Answers: 1
image
Computers and Technology, 23.06.2019 10:50, Leffew
The volume v and paper surface area a of a conical paper cup are given by where r is the radius of the base of the cone and h is the height of the cone. a. by eliminating h, obtain the expression for a as a function of r and v. b. create a user-de ned function that accepts r as the only argument and computes a for a given value of v. declare v to be global within the function. c. for v ! 10 in.3 , use the function with the fminbnd function to compute the value of r that minimizes the area a. what is the corresponding value of the height h? investigate the sensitivity of the solution by plotting v versus r. how much can r vary about its optimal value before the area increases 10 percent above its minimum value?
Answers: 1
image
Computers and Technology, 23.06.2019 12:00, trippie4life
3. when you right-click a linked spreadsheet object, what commands do you choose to activate the excel features? a. linked worksheet object > edit b. edit data > edit data c. linked spreadsheet > edit d. object > edit data
Answers: 2
image
Computers and Technology, 23.06.2019 13:30, alannaamarriee
Jace needs to answer a question on square roots to win a quiz. how can he use a spreadsheet to find the square root of 786? a. use the functions round and count b. create a table and chart c. use the function sqrt d. use the function now
Answers: 3
You know the right answer?
Write a bubble sort that counts the number of comparisons and the number of exchanges made while sor...

Questions in other subjects:

Konu
German, 23.07.2020 03:01
Konu
Mathematics, 23.07.2020 03:01