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, 22.06.2019 07:30, DivineMemes420
What type of computer network ensures high security ?
Answers: 1
image
Computers and Technology, 23.06.2019 06:00, hilarydodard7099
Which statistical function in a spreadsheet you to see how far each number varies, on average, from the average value of the list?
Answers: 2
image
Computers and Technology, 23.06.2019 07:30, emilyplays474
What key should you press and hold to select and open multiple files at one time? enter alt control esc
Answers: 1
image
Computers and Technology, 23.06.2019 13:30, carolelai08
Stops: using the information learned in this course, explain three things you will not do when driving. a. b. c. explain why you will not do these things when driving. starts: using the information learned in this course, explain three things you will do when driving. a. b. c. explain why you will do these particular things when driving. explain one thing you will stop doing as a passenger. explain one thing you will start doing as a passenger.
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
Mathematics, 04.12.2020 20:20
Konu
Mathematics, 04.12.2020 20:20
Konu
SAT, 04.12.2020 20:20