hw2-solutions

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

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

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 ﬁrst 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

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

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

## This note was uploaded on 03/28/2012 for the course ECE 368 taught by Professor Staff during the Fall '08 term at Purdue.

### 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
Ask a homework question - tutors are online