hw2-solutions

hw2-solutions - ECE368 Homework #2 Solutions 1. (15 points)...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
ECE368 Homework #2 Solutions 1. (15 points) Does the bidirectional bubble sorting algorithm reduce the number of swaps when compared to the original bubble sort? Does it reduce the total number of comparisons required to sort an array? Justify your answers. Solution: The number of swaps would remain the same, because both bubble sort and bidirectional bubble sort allow only swapping of adjacent elements. The total number of comparisons required to sort an array would be reduced for the following reasons: In bi-directional bubble sort, small elements can be bubbled to the correct position in a few (backward) passes. In contrast, small elements can move only one position at a time in the forward passes of a regular bubble sort. Hence, bidirectional bubble sort cuts down the total number of iterations and hence the total number of comparisons. For a group of contiguous elements that are in the correct order but not in the right positions, forward passes of a bubble sort over these elements result in only comparisons and no swaps. Consider for example an array that has the first n-1 elements in the correct order and the smallest element occupying the last position in the array. In each forward pass, only one swap
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

hw2-solutions - ECE368 Homework #2 Solutions 1. (15 points)...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online