{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CSE331_Homework_2

# CSE331_Homework_2 - 1 a What is the total number of...

This preview shows pages 1–3. Sign up to view the full content.

1. a. What is the total number of comparisons and the total number of swaps (comparing out-of-order pairs) of adjacent elements in the following list for both bubble and insertion sort. Bubble Sort Compares = 0 Swaps = 0 L: 8, 5, 7, 2, 3 Compares = 1 Swaps = 1 L: 5, 8, 7, 2, 3 Compares = 2 Swaps = 2 L: 5, 7, 8, 2, 3 Compares = 3 Swaps = 3 L: 5, 7, 2, 8, 3 Compares = 4 Swaps = 4 L: 5, 7, 2, 3, 8 Compares = 5 Swaps = 4 L: 5, 7, 2, 3, 8 Compares = 6 Swaps = 5 L: 5, 2, 7, 3, 8 Compares = 7 Swaps = 6 L: 5, 2, 3, 7, 8 Compares = 8 Swaps = 7 L: 2, 5, 3, 7, 8 Compares = 9 Swaps = 8 L: 2, 3, 5, 7, 8 Compares = 10 Swaps = 8 L: 2, 3, 5, 7, 8 Final Answer: Compares = 10 Swaps = 8 Insertion Sort Compares = 0 Swaps = 0 L: 8, 5, 7, 2, 3 Compares = 1 Swaps = 1 L: 5, 8, 7, 2, 3 Compares = 2 Swaps = 2 L: 5, 7, 8, 2, 3 Compares = 3 Swaps = 2 L: 5, 7, 8, 2, 3 Compares = 4 Swaps = 3 L: 5, 7, 2, 8, 3 Compares = 5 Swaps = 4 L: 5, 2, 7, 8, 3 Compares = 6 Swaps = 5 L: 2, 5, 7, 8, 3 Compares = 7 Swaps = 6 L: 2, 5, 7, 3, 8 Compares = 8 Swaps = 7 L: 2, 5, 3, 7, 8 Compares = 9 Swaps = 8 L: 2, 3, 5, 7, 8 Compares = 10 Swaps = 8 L: 2, 3, 5, 7, 8 Final Answer: Compares = 10 Swaps = 8 b. Why does the number of swaps give a lower bound on computation time for these two sorts? Bubble Sort give a lower bound on computation time compared to the number of swaps because the bigger numbers are at the front and it takes a lot more swaps to move them to the end because the way Bubble Sort swaps elements. Since every element is at most N- 1 away from its final position, the biggest number of operations to move the element to its sorted spot is n - 1 = O(n) operations. For Insertion Sort a lower bound computation is given based on the number of swaps because on average and in worse case it takes O(n^2). So more times than not the number of swaps will be a lower bound computation because contrary to bubble sort which takes less time than the previous pass, insertion may take more time on each pass than the previous. c. What is the number of inversions in list L? Why is the number of swaps of adjacent elements equal to the number of inversions for these sorts? There are 8 inversions in list L. The number of inversions is always the number of swaps because swapping two adjacent elements that are out of place removes one inversion.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
d.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern