Computers and Technology

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

answer
Answers: 3

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 07:30, akluke6059
Events and conditions that happen within an organization that are somewhat easier to deal with when responding to change are called
Answers: 1
image
Computers and Technology, 22.06.2019 15:30, coollid876
To increase sales, robert sends out a newsletter to his customers each month, letting them know about new products and ways in which to use them. in order to protect his customers' privacy, he uses this field when addressing his e-mail. attach bcc forward to
Answers: 2
image
Computers and Technology, 23.06.2019 03:00, SKYBLUE1015
What are the different parts of computer
Answers: 2
image
Computers and Technology, 23.06.2019 07:30, Braxtonw875
What part of the interface displays the external references contained in a selected cell? the status bar the review tab the scroll bar the formula bar
Answers: 1
Do you know the correct answer?
Write a bubble sort that counts the number of comparisons and the number of exchanges made while sor...

Questions in other subjects:

Konu
History, 30.10.2020 01:40
Konu
English, 30.10.2020 01:40