Title: Overview
1Overview
- Sort placing data in an array in some order
(usually decreasing or increasing order) - Bubble Sort
- More efficient bubble sort
2Bubble Sort
Smaller values bubble their way to the top like
air bubbles rising in water.
1
0
1
0
3
1
9
1
2
3
2
3
8
3
8
3
4
9
4
3
3Bubble Sort
Algorithm Start with the first two elements in
the array (element 0 and 1). Compare them. If
the value at the smaller subscript is a larger
number, swap the values. Continue moving
through the array comparing two values at a time.
1
0
9
1
2
3
8
3
4
3
4Bubble Sort
age
1
0
Compare age0 and age1 age1 is bigger. Do
nothing. Compare age1 and age2. age1 is
bigger. Swap
9
1
2
3
8
3
4
3
5Bubble Sort
age
1
0
Compare age2 and age3 age2 is bigger. Swap
3
1
2
9
8
3
4
3
6Bubble Sort
age
1
0
Compare age3 and age4 age3 is bigger. Swap
3
1
2
8
9
3
4
3
7Bubble Sort
age
This completes pass one Note that the largest
value sank to the bottom of the array. This is
guaranteed to happen on the first pass.
1
0
3
1
2
8
3
3
4
9
Begin second pass. Repeat exactly as for first
pass. Compare age0 and age1, then age1 and
age2, and so on, swapping values whenever the
value at the higher subscript is smaller.
8Bubble Sort
age
This completes pass two
1
0
3
1
Begin third pass. Repeat exactly as for first
pass. Compare age0 and age1, then age1 and
age2, and so on, swapping values whenever the
value at the higher subscript is smaller. Note
that no swaps happened in this pass and none will
happen in the fourth and final pass.
2
3
8
3
4
9
9Swap
Does this code swap the values in a and b? int
a 2 int b 3 a b b a
10Swap
Does this code swap the values in a and b? int
a 2 int b 3 int temp temp a a b
b temp
11Bubble Sort Program
Initialize array with numbers that are out of
order Display contents of array Sort the array
using a bubble sort Display contents of array
12Bubble Sort Program
Initialize array with numbers that are out of
order const int arraySize 10 int
agearraySize 2, 6, 4, 8, 10, 12, 89, 68, 45,
37 Display contents of array for (int i
0 i lt arraySize i ) cout ltlt
setw(4) ltlt agei
13Bubble Sort Program
Sort the array using a bubble sort int temp
for (int pass 1 pass lt arraySize
pass) for (int i 0 i lt
arraySize i)
if (age i gt agei 1)
temp agei agei
agei 1 agei1
temp
14const int arraySize 10, temp int
agearraySize 2, 6, 4, 8, 10, 12, 89, 68, 45,
37 for (int i 0 i lt arraySize i )
cout ltlt setw(4) ltlt agei cout ltlt endl for
(int pass 1 pass lt arraySize pass)
for (int i 0 i lt (arraySize - 1) i)
if (age i gt agei 1)
temp agei
agei agei 1
agei1 temp
for (int i 0 i lt arraySize i )
cout ltlt setw(4) ltlt agei
15Exercise
Lets make the bubble sort more efficient.
After the first pass of the bubble sort the
largest number is guaranteed to be in the
highest-numbered element of the array. So we
dont need to check this value on the next pass.
age
age
1
0
1
0
9
1
3
1
2
3
2
8
8
3
3
3
4
3
4
9
Modify bubble sort so it makes one less
comparison on each pass. In this example, pass 2
should only compare age0 age11, age1
age2, age2 age3 but should not compare
age3 and age4.
16const int arraySize 10, temp int
agearraySize 2, 6, 4, 8, 10, 12, 89, 68, 45,
37 int index arraySize - 1 for (int i 0
i lt arraySize i ) cout ltlt setw(4) ltlt
agei cout ltlt endl for (int pass 1 pass lt
arraySize pass) for (int i 0 i lt
index i) if
(age i gt agei 1)
temp agei agei
agei 1 agei1
temp index-- for
(int i 0 i lt arraySize i ) cout
ltlt setw(4) ltlt agei
17Exercise
Lets make the bubble sort even more efficient.
The data may not need all the passes to get
into the proper order. It may end up in the
proper order earlier than that. The way you can
tell is if any swaps are made in a pass. If no
swaps are made then the data is already in the
proper order. No further sorting is
necessary. Modify the bubble sort to check to
see if any swaps were made on a pass and stop
sorting if no swaps were made.
18int numSwaps int numPasses arraySize for
(int pass 1 pass lt numPasses pass)
numSwaps 0 for (int i 0 i lt index
i) if (age i
gt agei 1) temp ai
agei agei 1
agei1 temp
numSwaps if
(0 numSwaps) numPasses
0 index--
19Happy Valentines Day!!