Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Bubble Sort
Sorting
Sorting takes an unordered collection and makes it an ordered one.
1 77 2 42 3 35 4 12 5 101 6 5
1 5
2 12
3 35
4 42
5 77
6 101
Swap77 42 42 77
35 77 Swap77 35
12 12 77 Swap77
No need to swap
5 101Swap101 5
LB
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 42 2 35 3 12 4 77 5 5 6 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
procedure Bubblesort(A isoftype in/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
did_swap isoftype Boolean did_swap <- true loop exitif ((to_do = 0) OR NOT(did_swap)) index <- 1 did_swap <- false loop exitif(index > to_do) if(A[index] > A[index + 1]) then Swap(A[index], A[index + 1]) did_swap <- true endif index <- index + 1 endloop to_do <- to_do - 1 endloop
An Animated Example
N to_do index 8 7 did_swap true
98 23 45 14 1 2 3 4
6 67 33 42 5 6 7 8
An Animated Example
N to_do index 8 7 1 did_swap false
98 23 45 14 1 2 3 4
6 67 33 42 5 6 7 8
An Animated Example
N to_do index 8 7 1 Swap did_swap false
98 23 45 14 1 2 3 4
6 67 33 42 5 6 7 8
An Animated Example
N to_do index 8 7 1 Swap did_swap true
23 98 45 14 1 2 3 4
6 67 33 42 5 6 7 8
An Animated Example
N to_do index 8 7 2 did_swap true
23 98 45 14 1 2 3 4
6 67 33 42 5 6 7 8
An Animated Example
N to_do index 8 7 2 Swap did_swap true
23 98 45 14 1 2 3 4
6 67 33 42 5 6 7 8
An Animated Example
N to_do index 8 7 2 Swap did_swap true
23 45 98 14 1 2 3 4
6 67 33 42 5 6 7 8
An Animated Example
N to_do index 8 7 3 did_swap true
23 45 98 14 1 2 3 4
6 67 33 42 5 6 7 8
An Animated Example
N to_do index 8 7 3 Swap did_swap true
23 45 98 14 1 2 3 4
6 67 33 42 5 6 7 8
An Animated Example
N to_do index 8 7 3 Swap did_swap true
23 45 14 98 1 2 3 4
6 67 33 42 5 6 7 8
An Animated Example
N to_do index 8 7 4 did_swap true
23 45 14 98 1 2 3 4
6 67 33 42 5 6 7 8
An Animated Example
N to_do index 8 7 4 Swap did_swap true
23 45 14 98 1 2 3 4
6 67 33 42 5 6 7 8
An Animated Example
N to_do index 8 7 4 Swap did_swap true
23 45 14 6 1 2 3 4
98 67 33 42 5 6 7 8
An Animated Example
N to_do index 8 7 5 did_swap true
23 45 14 6 1 2 3 4
98 67 33 42 5 6 7 8
An Animated Example
N to_do index 8 7 5 Swap did_swap true
23 45 14 6 1 2 3 4
98 67 33 42 5 6 7 8
An Animated Example
N to_do index 8 7 5 Swap did_swap true
23 45 14 6 1 2 3 4
67 98 33 42 5 6 7 8
An Animated Example
N to_do index 8 7 6 did_swap true
23 45 14 6 1 2 3 4
67 98 33 42 5 6 7 8
An Animated Example
N to_do index 8 7 6 Swap did_swap true
23 45 14 6 1 2 3 4
67 98 33 42 5 6 7 8
An Animated Example
N to_do index 8 7 6 Swap did_swap true
23 45 14 6 1 2 3 4
67 33 98 42 5 6 7 8
An Animated Example
N to_do index 8 7 7 did_swap true
23 45 14 6 1 2 3 4
67 33 98 42 5 6 7 8
An Animated Example
N to_do index 8 7 7 Swap did_swap true
23 45 14 6 1 2 3 4
67 33 98 42 5 6 7 8
An Animated Example
N to_do index 8 7 7 Swap did_swap true
23 45 14 6 1 2 3 4
67 33 42 98 5 6 7 8
23 45 14 6 1 2 3 4
67 33 42 98 5 6 7 8
23 45 14 6 1 2 3 4
67 33 42 98 5 6 7 8
23 45 14 6 1 2 3 4
67 33 42 98 5 6 7 8
23 14 45 6 1 2 3 4
67 33 42 98 5 6 7 8
23 14 6 1 2 3
45 67 33 42 98 4 5 6 7 8
23 14 6 1 2 3
45 67 33 42 98 4 5 6 7 8
23 14 6 1 2 3
45 33 67 42 98 4 5 6 7 8
23 14 6 1 2 3
45 33 42 67 98 4 5 6 7 8
23 14 6 1 2 3
45 33 42 67 98 4 5 6 7 8
14 23 6 1 2 3
45 33 42 67 98 4 5 6 7 8
14 6 1 2
23 45 33 42 67 98 3 4 5 6 7 8
14 6 1 2
23 45 33 42 67 98 3 4 5 6 7 8
14 6 1 2
23 33 45 42 67 98 3 4 5 6 7 8
14 6 1 2
23 33 42 45 67 98 3 4 5 6 7 8
14 6 1 2
23 33 42 45 67 98 3 4 5 6 7 8
6 1
6 1
14 23 33 42 45 67 98 2 3 4 5 6 7 8
6 1
6 1
14 23 33 42 45 67 98 2 3 4 5 6 7 8
6 1
6 1
14 23 33 42 45 67 98 2 3 4 5 6 7 8
6 1
6 1
14 23 33 42 45 67 98 2 3 4 5 6 7 8
6 1
14 23 33 42 45 67 98 2 3 4 5 6 7 8
6 1
6 1
14 23 33 42 45 67 98 2 3 4 5 6 7 8
6 1
6 1
14 23 33 42 45 67 98 2 3 4 5 6 7 8
6 1
6 1
14 23 33 42 45 67 98 2 3 4 5 6 7 8
Finished Early
N to_do index 8 3 4 did_swap false We didnt do any swapping, so all of the other elements must be correctly placed. We can skip the last two passes of the outer loop. 6 1 14 23 33 42 45 67 98 2 3 4 5 6 7 8