You are on page 1of 35

Bubble Sort

© 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

Always compares adjacent elements in the array.


If the second of the two elements is bigger than the
first element, then the two elements are swapped.
After one left-to-right pass through the array, the
largest element will have bubbled to the top.
With each successive pass, another element will
have bubbled up to its correct position.
After N passes, the array is sorted.
04/20/99
CS1 lecture 7 © 1999 Raymond Lister 2
Bubble Sort: The movie (1)
The start of the first left-to-right pass.
0 1 2 3 4 5
BEFORE 7 3 9 0 2 5
5 Right

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

(No swap) 5 Right

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

The first left-to-right pass is completed.


04/20/99
CS1 lecture 7 © 1999 Raymond Lister 7
Intermission
We have completed the first left-to-right pass through
the array.

As a result of that pass, the largest element has


“bubbled” its way to the “top”.

We repeat the process. This time, however, the left-to-


right pass doesn’t have to look at the last element
in the array.
… next slide ...
04/20/99
CS1 lecture 7 © 1999 Raymond Lister 8
Bubble Sort: The movie (6)
Start of the second left-to-right pass.
0 1 2 3 4 5
BEFORE 3 7 0 2 5 9

(No swap) 4 Right

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

The second left-to-right pass is completed.


04/20/99
CS1 lecture 7 © 1999 Raymond Lister 12
End of second left-to-right pass
As a result of this pass, the second largest element has
“bubbled” its way to it’s correct position, so the sorted
region has grown by one element.
0 1 2 3 4 5

3 0 2 5 7 9

We now repeat this process a third time. This time,


however, the left-to-right pass doesn’t have to look at
the last two elements in the array.
… next slide ...
04/20/99
CS1 lecture 7 © 1999 Raymond Lister 13
End of third left-to-right pass
As a result of this pass, the third largest element has
“bubbled” its way to it’s correct position, so the sorted
region has grown by one element.
0 1 2 3 4 5

3 0 2 5 7 9

We now repeat this process a fourth time. This time,


however, the left-to-right pass doesn’t have to look at
the last three elements in the array.
… next slide ...
04/20/99
CS1 lecture 7 © 1999 Raymond Lister 14
End of fourth left-to-right pass
As a result of this pass, the fourth largest element has
“bubbled” its way to it’s correct position, so the sorted
region has grown by one element.
0 1 2 3 4 5

0 2 3 5 7 9

We can see that the array is now completely sorted,


but the program doesn’t know that, so it will keep
going, and do one more pass …
… next slide ...
04/20/99
CS1 lecture 7 © 1999 Raymond Lister 15
End of fifth left-to-right pass

0 1 2 3 4 5

0 2 3 5 7 9

And we are finished.

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”

(Assuming the array is called “a”, and that we should stop


the pass at position “right”.)
The swap method is straight out of selection sort.
This is an example of code reuse … we wrote “swap”
properly once, so now we can use it for other purposes.
04/20/99
CS1 lecture 7 © 1999 Raymond Lister 21
The Key to the OOP Universe
First satisfy yourself that you understand the code
inside the box on the previous slide.
This code (and the box around it) will be repeated on
several future slides. However, now that you
understand that code, don’t read it again and again …
concentrate on the new code, the code outside the box.
This is how professional programmers read and
understand code. Reading code not like reading a
novel. It’s more like doing a jig saw, or a crossword.
04/20/99
CS1 lecture 7 © 1999 Raymond Lister 22
By the way ...
for (int i=0 ; i<right ; i++)

In a for loop, it doesn’t matter if the ++ comes before


or after the variable being incremented.

04/20/99
CS1 lecture 7 © 1999 Raymond Lister 23
Bubble Sort: the code (Version 1)
public static void BubbleSort1(int[] a) {

for (int right = a.length -1 ; right>0 ; --right)


for (int i=0; i<right; i++) This box is from the earlier
slide. Don’t read it again!
if ( a[i] > a[i+1] ) The code in the box makes
one left-to-right pass
swap(a, i, i+1); ;
through the array.
Concentrate on the code
} // method BubbleSort1 outside the box.

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

If none swap, then the program can stop immediately.

This approach requires the use of a Boolean variable.

04/20/99 … code on next slide ...


CS1 lecture 7 © 1999 Raymond Lister 27
Bubble Sort (Version 2)
public static void BubbleSort2(int[] a) {
boolean elementsSwitched = true;
int right = a.length - 1;
while ( elementsSwitched & right>0 ) {

The “&” symbol means “and”.


For the above while condition to be true, both parts
of the condition must be true.
… more code on next slide ...
04/20/99
CS1 lecture 7 © 1999 Raymond Lister 28
Bubble Sort, Version 2 (continued)
public static void BubbleSort2(int[] a) {
boolean elementsSwitched = true;
int right = a.length - 1;
while ( elementsSwitched & right>0 ) {
elementsSwitched = false;
Using a for loop, make 1 left-to-right pass.
During that pass, if any elements swap, set …
elementsSwitched = true;
} // while … rest of code on next slide ...
04/20/99
CS1 lecture 7 © 1999 Raymond Lister 29
Bubble Sort Version 2 (continued)
while ( elementsSwitched & right>0 ) {
elementsSwitched = false;
for ( int i=0 ; i<right ; ++i )
This box is
if ( a[i] > a[i+1] ) { the code
swap(a, i, i+1); for 1 pass,
elementsSwitched = true; just like
} // if and for before,
with the one
--right; extra line.

} // 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.

… See picture on next slide ...

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

You might also like