Dale - Computer Science Illuminated 317

# Dale - Computer Science Illuminated 317 - when it is...

This preview shows page 1. Sign up to view the full content.

290 Chapter 9 Abstract Data Types and Algorithms Figure 9.10 Example of a bubble sort Bubble Sort The bubble sort is a selection sort that uses a different scheme for finding the minimum value. Starting with the last list element, we compare succes- sive pairs of elements, swapping whenever the bottom element of the pair is smaller than the one above it (Figure 9.10a). In this way the smallest element “bubbles up” to the top of the list. Each iteration puts the smallest unsorted item into its correct place using the same technique, but it also makes changes in the locations of the other elements in the array (Figure 9.10b). Before we write this algorithm, we must make an observation: Bubble sort is the slowest of all the sorting algorithms. Why then do we bother to mention it? Because it is the only sorting algorithm that can recognize
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: when it is already sorted. Lets apply the algorithm to an already sorted list. See Figure 9.11. We compare Phil with John and do not swap. We compare John with Jim and do not swap. We compare Jim with Bob and do not swap. We a) First iteration (Sorted elements are shaded.) b) Remaining iterations (Sorted elements are shaded.) RFz R z R9z R z Rhz Phil items John Bob Jim Al RFz R z R9z R z Rhz Phil items John Bob Al Jim RFz R z R9z R z Rhz Phil items John Al Bob Jim RFz R z R9z R z Rhz Phil items Al John Bob Jim RFz R z R9z R z Rhz Al items Phil John Bob Jim RFz R z R9z R z Rhz Al items Phil John Bob Jim RFz R z R9z R z Rhz Al items Bob Phil John Jim RFz R z R9z R z Rhz Al items Bob Jim Phil John RFz R z R9z R z Rhz Al items Bob Jim John Phil...
View Full Document

## This note was uploaded on 01/13/2011 for the course CSE 1550 taught by Professor Marianakant during the Fall '10 term at York University.

Ask a homework question - tutors are online