Sorting
77 42 3512 101 5
1 2 3 4 5 6
5 12 35 42 77 101
• Sorting takes an unordered collection and
makes it an ordered one.
1 2 3 4 5 6
4.
Exchange/Bubble Sort:
It usessimple algorithm. It sorts by comparing each pair
of adjacent items and swapping them in the order. This
will be repeated until no swaps are needed. The
algorithm got its name from the way smaller elements
"bubble" to the top of the list.
It is not that much efficient, when a list is having more
than a few elements. Among simple sorting algorithms,
algorithms like insertion sort are usually considered
as more efficient.
Bubble sort is little slower compared to other sorting
techniques but it is easy because it deals with only two
elements at a time.
5.
"Bubbling Up" theLargest Element
•
77 42 35 12 101 5
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
6.
"Bubbling Up" theLargest Element
•
5
12
35
7 101
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
74
2Swa
p4727
7.
"Bubbling Up" theLargest Element
•
5
12
42 101
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
8.
"Bubbling Up" theLargest Element
•
5
35
42 101
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
9.
"Bubbling Up" theLargest Element
•
42 35 12 77 101 5
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
No need to swap
10.
"Bubbling Up" theLargest Element
•
77
12
35
42
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
11.
"Bubbling Up" theLargest 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
42 35 12 77 5 101
Largest value correctly placed
12.
Bubble Sort
1. Ineach pass, we compare adjacent elements and swap them if they are
out of order until the end of the list. By doing so, the 1st pass ends
up “bubbling up” the largest element to the last position on the list
2. The 2nd pass bubbles up the 2nd largest, and so on until, after N-1
passes, the list is sorted.
Example:
Pass 1 89 | 45 68
90
29 34 17 Pass 2 45 | 68 89 29 34 17
45 89 | 68 90 29 34 17 45 68 | 89
29
34 17
68 89
| 90
29 34 17 68
89 | 29
34 17
89 90 | 29 34 17 29 89 | 34 17
29 90 | 34 17
34 90 | 17
34 89 |
17
17 89
17 90
45 68 89 29 34 17 90 45 68 29 34 17 89
largest 2nd
largest
13.
The “Bubble Up”Algorithm
index <- 1
last_compare_at <- n – 1
> last_compare_at)
loop
exitif(index
if(A[index]> A[index + 1]) then
Swap(A[index], A[index + 1])
endif
index <- index + 1
endloop
14.
No, Swap isn’tbuilt in.
Procedure Swap(a, b isoftype in/out Num)
t isoftype Num
t <- a
a <- b
b <-
t
endproce
dure //
Swap
LB
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
1 2 3 4 5
6
42 35 12 77 5 101
Largest value correctly placed
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 correct location…
• Then we repeat the “bubble up”
process N – 1 times.
• This guarantees we’ll correctly
place all N elements.
Reducing the Numberof Comparisons
• On the Nt
h “bubble up”, we only need to
do MAX-N comparisons.
• For example:
– This is the 4t
h “bubble up”
– MAX is 6
– Thus we have 2 comparisons to do
1 2 3 4
5 6
12 35 5 42 77 101
N is …// Size of Array
Arr_Type definesa Array[1..N] of Num
Procedure Swap(n1, n2 isoftype in/out Num)
temp isoftype Num
temp <- n1
n1 <- n2
n2 <- temp
endprocedure // Swap
22.
procedure Bubblesort(A isoftypein/out Arr_Type)
to_do, index isoftype Num
to_do <- N – 1
loop
exitif(to_do = 0)
index <- 1
loop
exitif(index > to_do) if(A[index]
> A[index + 1]) then
Swap(A[index], A[index + 1])
endif
index <- index + 1
endloop
to_do <- to_do - 1
endloop
endprocedure //
Bubblesort
Inner
loop
Outer
loop
24.
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
• We reduce the number of elements we
compare each time one is correctly placed