Professional Documents
Culture Documents
© 1999 Raymond Lister. The material herein was in part developed in my own
time, and in whole exceeds the requirements of lecture preparation. My
permission is required before other parties may exploit these notes -- RL.
04/20/99
CS1 lecture 7 © 1999 Raymond Lister 1
Bubble Sort: the basic idea
AFTER 3 7 9 0 2 5
04/20/99
CS1 lecture 7 © 1999 Raymond Lister 3
Bubble Sort: The movie (2)
0 1 2 3 4 5
BEFORE 3 7 9 0 2 5
AFTER 3 7 9 0 2 5
04/20/99
CS1 lecture 7 © 1999 Raymond Lister 4
Bubble Sort: The movie (3)
0 1 2 3 4 5
BEFORE 3 7 9 0 2 5
5 Right
AFTER 3 7 0 9 2 5
04/20/99
CS1 lecture 7 © 1999 Raymond Lister 5
Bubble Sort: The movie (4)
0 1 2 3 4 5
BEFORE 3 7 0 9 2 5
5 Right
AFTER 3 7 0 2 9 5
04/20/99
CS1 lecture 7 © 1999 Raymond Lister 6
Bubble Sort: The movie (5)
0 1 2 3 4 5
BEFORE 3 7 0 2 9 5
5 Right
After 3 7 0 2 5 9
AFTER 3 7 0 2 5 9
04/20/99
CS1 lecture 7 © 1999 Raymond Lister 9
Bubble Sort: The movie (7)
0 1 2 3 4 5
BEFORE 3 7 0 2 5 9
4 Right
AFTER 3 0 7 2 5 9
04/20/99
CS1 lecture 7 © 1999 Raymond Lister 10
Bubble Sort: The movie (8)
0 1 2 3 4 5
BEFORE 3 0 7 2 5 9
4 Right
AFTER 3 0 2 7 5 9
04/20/99
CS1 lecture 7 © 1999 Raymond Lister 11
Bubble Sort: The movie (9)
0 1 2 3 4 5
BEFORE 3 0 2 7 5 9
4 Right
AFTER 3 0 2 5 7 9
3 0 2 5 7 9
3 0 2 5 7 9
0 2 3 5 7 9
0 1 2 3 4 5
0 2 3 5 7 9
04/20/99
CS1 lecture 7 © 1999 Raymond Lister 16
A Single Pass: Slow Motion Replay (1)
i 0 right 4
BEFORE 3 0 2 7 5 9
The for loop
control variable “i”
is initialized to zero.
AFTER 0 3 2 7 5 9
04/20/99
CS1 lecture 7 © 1999 Raymond Lister 17
A Single Pass: Slow Motion Replay (2)
i 1 right 4
BEFORE 0 3 2 7 5 9
With each iteration
around the for loop,
“i” is incremented.
AFTER 0 2 3 7 5 9
04/20/99
CS1 lecture 7 © 1999 Raymond Lister 18
A Single Pass: Slow Motion Replay (3)
i 2 right 4
BEFORE 0 2 3 7 5 9
With each iteration
around the for loop,
“i” is incremented.
AFTER 0 2 3 7 5 9
04/20/99
CS1 lecture 7 © 1999 Raymond Lister 19
A Single Pass: Slow Motion Replay (3)
i 3 right 4
BEFORE 0 2 3 7 5 9
for loop
continues while
i < right
END 0 2 3 7 5 9
OF
PASS
04/20/99
CS1 lecture 7 © 1999 Raymond Lister 20
Code for doing a single pass
for (int i=0; i<right; i++) Array “a”
if ( a[i] > a[i+1] )
swap(a, i, i+1); ;
“right”
04/20/99
CS1 lecture 7 © 1999 Raymond Lister 23
Bubble Sort: the code (Version 1)
public static void BubbleSort1(int[] a) {
04/20/99
CS1 lecture 7 © 1999 Raymond Lister 24
The for loop as a while loop ...
The outer for loop from the previous slide ...
for (int right = a.length - 1 ; right>0 ; --right)
… could be rewritten as a while loop ...
int right = a.length - 1;
while ( right > 0 ) {
The for loop that makes one left-to-right pass.
--right;
} // while
This is clumsy, but we will soon need a loop like this.
04/20/99
CS1 lecture 7 © 1999 Raymond Lister 25
Improving the basic bubble sort (1)
Problem: If the array is nearly sorted before we start,
Version 1 can do a lot of redundant work. For
example, if we started with the following array ...
9 0 2 3 5 7
After only the first pass, we have a sorted array, but
Bubblesort will continue making passes ...
0 2 3 5 7 9
04/20/99
CS1 lecture 7 © 1999 Raymond Lister 26
Improving the basic bubble sort (2)
Solution: On each pass we keep a record of whether
any elements have been swapped.
0 2 3 5 7 9
} // while
04/20/99
CS1 lecture 7 © 1999 Raymond Lister 30
Improving on version 2 (part 1)
Problem: If the only numbers swapped occur early in
the unsorted region, Version 2 may do a lot of
redundant work in the next pass. For example ...
2 3 0 5 7 9
Only 3 and the 0 will swap, but version 2 will not
realize that this implies that 3, 5 and 7 are now also sorted ...
2 0 3 5 7 9
04/20/99
CS1 lecture 7 © 1999 Raymond Lister 31
Improving on version 2 (part 2)
Solution: Don’t just remember that a swap was made,
remember where it was made. In the next pass, you
don’t have to sort any part of the array that is on the
right hand side of the rightmost swap.
04/20/99
CS1 lecture 7 © 1999 Raymond Lister 32
Version 3: The Trailer
0 1 2 3 4 5
BEFORE 2 3 0 5 7 9
FIRST
PASS 5 Right
Rightmost swap
AFTER 2 0 3 5 7 9
FIRST
PASS 1 Right
04/20/99
CS1 lecture 7 © 1999 Raymond Lister 33
Bubble Sort: the code (Version 3)
public static void BubbleSort3(int[] a) {
int rightmostSwap = a.length - 1;
while ( rightmostSwap > 0 ) {
… see next slide for the guts of the while loop ...
} // while
04/20/99
CS1 lecture 7 © 1999 Raymond Lister 34
Bubble Sort Version 3 (continued)
while ( rightmostSwap > 0 ) {
int right = rightmostSwap;
rightmostSwap = 0;
This box is
for ( int i=0 ; i<right ; ++i ) the code
if ( a[i] > a[i+1] ) { for 1 pass,
just like
swap(a, i, i+1); before,
rightmostSwap = i ;
with the one
} extra line.
} // while
04/20/99
CS1 lecture 7 © 1999 Raymond Lister 35