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, 22.06.2019 15:00
Which of the following statements tests if students have a grade of 70 or above, as well as fewer than five absences? a: if(grade > = 70 and daysabsent < = 5): b: if(grade > = 70 or daysabsent < = 5): c: if(grade > 70 and daysabsent < = 5): d: if(grade > 70 or daysabsent < = 5): i took the test the answer is a
Answers: 1
question
Computers and Technology, 22.06.2019 17:00
Your computer running windows 10 is doing some very strange things with the operating system. you are fairly certain it is not a hardware issue. you need to try to get further insight into what is going on within the operating system. which tool would be best suited for this?
Answers: 2
question
Computers and Technology, 23.06.2019 17:20
What is the best assassins creed game?
Answers: 2
question
Computers and Technology, 24.06.2019 17:50
Which of the following best describe how the depth-limited search works. a normal depth-first search is performed but the number of ply/depths is limited. a normal breadth-first search is performed but the number of ply/depths is limited. a normal breadth-first search is performed but values above a specific value will be ignored. a normal depth-first search is performed but values above a specific value will be ignored.
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
Mathematics, 12.10.2020 07:01
question
Health, 12.10.2020 07:01
question
Mathematics, 12.10.2020 07:01
question
English, 12.10.2020 07:01
question
Mathematics, 12.10.2020 07:01