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

Another question on Computers and Technology

question
Computers and Technology, 21.06.2019 18:20
Write the pseudocode for the following: a function called fahrenheittocelsius that accepts a real fahrenheit temperature, performs the conversion, and returns the real celsius temperature. a function called celsiustofahrenheit that accepts a real celsius temperature, performs the conversion, and returns the real fahrenheit temperature. a main module that asks user to enter a fahrenheit temperature, calls the fahrenheittocelsius function, and displays the celsius temperature with a user-friendly message. it then asks the user to enter a celsius temperature, calls the celsiustofahrenheit function, and displays the fahrenheit temperature with a user-friendly message.
Answers: 1
question
Computers and Technology, 22.06.2019 03:10
Write a program that begins by reading in a series of positive integers on a single line of input and then computes and prints the product of those integers. integers are accepted and multiplied until the user enters an integer less than 1. this final number is not part of the product. then, the program prints the product. if the first entered number is negative or 0, the program must print “bad input.” and terminate immediately. next, the program determines and prints the prime factorization of the product, listing the factors in increasing order. if a prime number is not a factor of the product, then it must not appear in the factorization. sample runs are given below. note that if the power of a prime is 1, then that 1 must appear in t
Answers: 3
question
Computers and Technology, 22.06.2019 11:00
When building customer relationships through email what should you not do? question 2 options: utilize proper grammar, spelling, and punctuation type in all capital letters use hyperlinks rather than attachments respond to all emails within 24 hours
Answers: 1
question
Computers and Technology, 23.06.2019 21:20
In microsoft word, when you highlight existing text you want to replace, you're in              a.  advanced mode.    b.  automatic mode.    c.  basic mode.    d.  typeover mode
Answers: 1
You know the right answer?
Write a bubble sort that counts the number of comparisons and the number of exchanges made while sor...
Questions
question
English, 24.08.2019 11:00
question
English, 24.08.2019 11:00
question
Mathematics, 24.08.2019 11:00
question
Advanced Placement (AP), 24.08.2019 11:00
Questions on the website: 13722367