2. Sorting
• Sorting takes an unordered collection and
makes it an ordered one.
1 2 3 4 5 6 7 8
674523 14 6 3398 42
1 2 3 4 5 6 7 8
452314 33 42 676 98
3. "Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping
1 2 3 4 5 6 7 8
674523 14 6 3398 42
4. "Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping
Swap
1 2 3 4 5 6 7 8
674598 14 6 3323 42
Value swapped
5. "Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping
Swap
1 2 3 4 5 6 7 8
679845 14 6 3323 42
Value swapped
6. "Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping
Swap
1 2 3 4 5 6 7 8
671445 98 6 3323 42
Value swapped
7. "Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping
Value swapped
Swap
1 2 3 4 5 6 7 8
671445 6 98 3323 42
8. "Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping
Swap
1 2 3 4 5 6 7 8
981445 6 67 3323 42
Value swapped
9. "Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping
Swap
1 2 3 4 5 6 7 8
331445 6 67 9823 42
Value swapped
10. "Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping
Swap
1 2 3 4 5 6 7 8
331445 6 67 4223 98
Value swapped
11. "Bubbling Up" the Largest Element
• Traverse a collection of elements
– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping
Largest value correctly placed
1 2 3 4 5 6 7 8
331445 6 67 4223 98
12. The “Bubble Up” Algorithm
index <- 1
last_compare_at <- n – 1
loopouter
exitif(index > last_compare_at)
Loopinner
to_do<-0
exitif(to_do > index)
if(A[to_do] > A[to_do + 1]) then
Swap(A[to_do], A[to_do + 1])
endif
to_do<- to_do + 1
endloopinner
index <- index - 1
endloopouter
13. No, Swap isn’t built in.
Procedure Swap(a, b isoftype in/out Num)
temp isoftype Num
temp <- a
a <- b
b <- temp
endprocedure // Swap
15. Items of Interest
• Notice that only the largest value is
correctly placed
• All other values are still out of order
• So we need to repeat this process
Largest value correctly placed
1 2 3 4 5 6 7 8
331445 6 67 4223 98
16. Repeat “Bubble Up” How Many Times?
• If we have N elements…
• And if each time we bubble an element, we
place it in its largest correct location…
• Then we repeat InnerLoop the “bubble up”
process OuterLoop – 1 times.
• This guarantees we’ll correctly
place all N elements.
92. Summary
• “Bubble Up” algorithm will move largest
value to its correct location (to the right)
• Repeat “Bubble Up” until all elements are
correctly placed:
– Maximum of N-1 times
– Can finish early if no swapping occurs
• We reduce the number of elements we
compare each time one is correctly placed