Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an...

96
Bubble Sort Merge Sort

Transcript of Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an...

Page 1: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

Bubble SortMerge Sort

Page 2: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

Bubble Sort

Page 3: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

Sorting

• Sorting takes an unordered collection and makes it an ordered one.

512354277 101

1 2 3 4 5 6

5 12 35 42 77 101

1 2 3 4 5 6

Page 4: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

"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

512354277 101

1 2 3 4 5 6

Page 5: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

"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

512354277 101

1 2 3 4 5 6

Swap42 77

Page 6: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

"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

512357742 101

1 2 3 4 5 6

Swap35 77

Page 7: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

"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

512773542 101

1 2 3 4 5 6

Swap12 77

Page 8: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

"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

577123542 101

1 2 3 4 5 6

No need to swap

Page 9: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

"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

577123542 101

1 2 3 4 5 6

Swap5 101

Page 10: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

"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

77123542 5

1 2 3 4 5 6

101

Largest value correctly placed

Page 11: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The “Bubble Up” Algorithm

index <- 1last_compare_at <- n – 1

loop exitif(index > last_compare_at) if(A[index] > A[index + 1]) then Swap(A[index], A[index + 1]) endif index <- index + 1endloop

Page 12: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

No, Swap isn’t built in.

Procedure Swap(a, b isoftype in/out Num)

t isoftype Num

t <- a

a <- b

b <- t

endprocedure // Swap

LB

Page 13: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

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

77123542 5

1 2 3 4 5 6

101

Largest value correctly placed

Page 14: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

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.

Page 15: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

“Bubbling” All the Elements

77123542 51 2 3 4 5 6

101

5421235 771 2 3 4 5 6

101

42 5 3512 771 2 3 4 5 6

101

42 35 512 771 2 3 4 5 6

101

42 35 12 5 771 2 3 4 5 6

101

N -

1

Page 16: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

Reducing the Number of Comparisons

12 35 42 77 1011 2 3 4 5 6

5

77123542 51 2 3 4 5 6

101

5421235 771 2 3 4 5 6

101

42 5 3512 771 2 3 4 5 6

101

42 35 512 771 2 3 4 5 6

101

Page 17: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

Reducing the Number of Comparisons

• On the Nth “bubble up”, we only need to do MAX-N comparisons.

• For example:– This is the 4th “bubble up”– MAX is 6– Thus we have 2 comparisons to do

42 5 3512 771 2 3 4 5 6

101

Page 18: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

Putting It All Together

Page 19: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

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

Page 20: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

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 endloopendprocedure // Bubblesort

Inn

er l

oo

p

Ou

ter

loo

p

Page 21: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

Already Sorted Collections?

• What if the collection was already sorted?• What if only a few elements were out of place and

after a couple of “bubble ups,” the collection was sorted?

• We want to be able to detect this and “stop early”!

42 35 12 5 771 2 3 4 5 6

101

Page 22: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

Using a Boolean “Flag”

• We can use a boolean variable to determine if any swapping occurred during the “bubble up.”

• If no swapping occurred, then we know that the collection is already sorted!

• This boolean “flag” needs to be reset after each “bubble up.”

Page 23: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

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

Page 24: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

An Animated Example

674523 14 6 3398 42

1 2 3 4 5 6 7 8

to_do

index

7

N 8 did_swap true

Page 25: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

An Animated Example

674523 14 6 3398 42

1 2 3 4 5 6 7 8

to_do

index

7

1

N 8 did_swap false

Page 26: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

An Animated Example

674523 14 6 3398 42

1 2 3 4 5 6 7 8

to_do

index

7

1

N 8

Swap

did_swap false

Page 27: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

An Animated Example

674598 14 6 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

1

N 8

Swap

did_swap true

Page 28: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

An Animated Example

674598 14 6 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

2

N 8 did_swap true

Page 29: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

An Animated Example

674598 14 6 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

2

N 8

Swap

did_swap true

Page 30: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

An Animated Example

679845 14 6 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

2

N 8

Swap

did_swap true

Page 31: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

An Animated Example

679845 14 6 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

3

N 8 did_swap true

Page 32: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

An Animated Example

679845 14 6 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

3

N 8

Swap

did_swap true

Page 33: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

An Animated Example

671445 98 6 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

3

N 8

Swap

did_swap true

Page 34: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

An Animated Example

671445 98 6 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

4

N 8 did_swap true

Page 35: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

An Animated Example

671445 98 6 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

4

N 8

Swap

did_swap true

Page 36: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

An Animated Example

671445 6 98 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

4

N 8

Swap

did_swap true

Page 37: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

An Animated Example

671445 6 98 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

5

N 8 did_swap true

Page 38: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

An Animated Example

671445 6 98 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

5

N 8

Swap

did_swap true

Page 39: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

An Animated Example

981445 6 67 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

5

N 8

Swap

did_swap true

Page 40: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

An Animated Example

981445 6 67 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

6

N 8 did_swap true

Page 41: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

An Animated Example

981445 6 67 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

6

N 8

Swap

did_swap true

Page 42: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

An Animated Example

331445 6 67 9823 42

1 2 3 4 5 6 7 8

to_do

index

7

6

N 8

Swap

did_swap true

Page 43: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

An Animated Example

331445 6 67 9823 42

1 2 3 4 5 6 7 8

to_do

index

7

7

N 8 did_swap true

Page 44: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

An Animated Example

331445 6 67 9823 42

1 2 3 4 5 6 7 8

to_do

index

7

7

N 8

Swap

did_swap true

Page 45: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

An Animated Example

331445 6 67 4223 98

1 2 3 4 5 6 7 8

to_do

index

7

7

N 8

Swap

did_swap true

Page 46: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

After First Pass of Outer Loop

331445 6 67 4223 98

1 2 3 4 5 6 7 8

to_do

index

7

8

N 8

Finished first “Bubble Up”

did_swap true

Page 47: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Second “Bubble Up”

331445 6 67 4223 98

1 2 3 4 5 6 7 8

to_do

index

6

1

N 8 did_swap false

Page 48: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Second “Bubble Up”

331445 6 67 4223 98

1 2 3 4 5 6 7 8

to_do

index

6

1

N 8 did_swap false

No Swap

Page 49: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Second “Bubble Up”

331445 6 67 4223 98

1 2 3 4 5 6 7 8

to_do

index

6

2

N 8 did_swap false

Page 50: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Second “Bubble Up”

331445 6 67 4223 98

1 2 3 4 5 6 7 8

to_do

index

6

2

N 8 did_swap false

Swap

Page 51: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Second “Bubble Up”

334514 6 67 4223 98

1 2 3 4 5 6 7 8

to_do

index

6

2

N 8 did_swap true

Swap

Page 52: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Second “Bubble Up”

334514 6 67 4223 98

1 2 3 4 5 6 7 8

to_do

index

6

3

N 8 did_swap true

Page 53: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Second “Bubble Up”

334514 6 67 4223 98

1 2 3 4 5 6 7 8

to_do

index

6

3

N 8 did_swap true

Swap

Page 54: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Second “Bubble Up”

33614 45 67 4223 98

1 2 3 4 5 6 7 8

to_do

index

6

3

N 8 did_swap true

Swap

Page 55: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Second “Bubble Up”

33614 45 67 4223 98

1 2 3 4 5 6 7 8

to_do

index

6

4

N 8 did_swap true

Page 56: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Second “Bubble Up”

33614 45 67 4223 98

1 2 3 4 5 6 7 8

to_do

index

6

4

N 8 did_swap true

No Swap

Page 57: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Second “Bubble Up”

33614 45 67 4223 98

1 2 3 4 5 6 7 8

to_do

index

6

5

N 8 did_swap true

Page 58: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Second “Bubble Up”

33614 45 67 4223 98

1 2 3 4 5 6 7 8

to_do

index

6

5

N 8 did_swap true

Swap

Page 59: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Second “Bubble Up”

67614 45 33 4223 98

1 2 3 4 5 6 7 8

to_do

index

6

5

N 8 did_swap true

Swap

Page 60: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Second “Bubble Up”

67614 45 33 4223 98

1 2 3 4 5 6 7 8

to_do

index

6

6

N 8 did_swap true

Page 61: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Second “Bubble Up”

67614 45 33 4223 98

1 2 3 4 5 6 7 8

to_do

index

6

6

N 8 did_swap true

Swap

Page 62: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Second “Bubble Up”

42614 45 33 6723 98

1 2 3 4 5 6 7 8

to_do

index

6

6

N 8 did_swap true

Swap

Page 63: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

After Second Pass of Outer Loop

42614 45 33 6723 98

1 2 3 4 5 6 7 8

to_do

index

6

7

N 8 did_swap true

Finished second “Bubble Up”

Page 64: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Third “Bubble Up”

42614 45 33 6723 98

1 2 3 4 5 6 7 8

to_do

index

5

1

N 8 did_swap false

Page 65: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Third “Bubble Up”

42614 45 33 6723 98

1 2 3 4 5 6 7 8

to_do

index

5

1

N 8 did_swap false

Swap

Page 66: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Third “Bubble Up”

42623 45 33 6714 98

1 2 3 4 5 6 7 8

to_do

index

5

1

N 8 did_swap true

Swap

Page 67: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Third “Bubble Up”

42623 45 33 6714 98

1 2 3 4 5 6 7 8

to_do

index

5

2

N 8 did_swap true

Page 68: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Third “Bubble Up”

42623 45 33 6714 98

1 2 3 4 5 6 7 8

to_do

index

5

2

N 8 did_swap true

Swap

Page 69: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Third “Bubble Up”

42236 45 33 6714 98

1 2 3 4 5 6 7 8

to_do

index

5

2

N 8 did_swap true

Swap

Page 70: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Third “Bubble Up”

42236 45 33 6714 98

1 2 3 4 5 6 7 8

to_do

index

5

3

N 8 did_swap true

Page 71: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Third “Bubble Up”

42236 45 33 6714 98

1 2 3 4 5 6 7 8

to_do

index

5

3

N 8 did_swap true

No Swap

Page 72: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Third “Bubble Up”

42236 45 33 6714 98

1 2 3 4 5 6 7 8

to_do

index

5

4

N 8 did_swap true

Page 73: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Third “Bubble Up”

42236 45 33 6714 98

1 2 3 4 5 6 7 8

to_do

index

5

4

N 8 did_swap true

Swap

Page 74: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Third “Bubble Up”

42236 33 45 6714 98

1 2 3 4 5 6 7 8

to_do

index

5

4

N 8 did_swap true

Swap

Page 75: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Third “Bubble Up”

42236 33 45 6714 98

1 2 3 4 5 6 7 8

to_do

index

5

5

N 8 did_swap true

Page 76: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Third “Bubble Up”

42236 33 45 6714 98

1 2 3 4 5 6 7 8

to_do

index

5

5

N 8 did_swap true

Swap

Page 77: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Third “Bubble Up”

45236 33 42 6714 98

1 2 3 4 5 6 7 8

to_do

index

5

5

N 8 did_swap true

Swap

Page 78: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

After Third Pass of Outer Loop

45236 33 42 6714 98

1 2 3 4 5 6 7 8

to_do

index

5

6

N 8 did_swap true

Finished third “Bubble Up”

Page 79: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Fourth “Bubble Up”

45236 33 42 6714 98

1 2 3 4 5 6 7 8

to_do

index

4

1

N 8 did_swap false

Page 80: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Fourth “Bubble Up”

45236 33 42 6714 98

1 2 3 4 5 6 7 8

to_do

index

4

1

N 8 did_swap false

Swap

Page 81: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Fourth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

4

1

N 8 did_swap true

Swap

Page 82: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Fourth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

4

2

N 8 did_swap true

Page 83: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Fourth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

4

2

N 8 did_swap true

No Swap

Page 84: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Fourth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

4

3

N 8 did_swap true

Page 85: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Fourth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

4

3

N 8 did_swap true

No Swap

Page 86: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Fourth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

4

4

N 8 did_swap true

Page 87: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Fourth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

4

4

N 8 did_swap true

No Swap

Page 88: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

After Fourth Pass of Outer Loop

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

4

5

N 8 did_swap true

Finished fourth “Bubble Up”

Page 89: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Fifth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

3

1

N 8 did_swap false

Page 90: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Fifth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

3

1

N 8 did_swap false

No Swap

Page 91: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Fifth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

3

2

N 8 did_swap false

Page 92: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Fifth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

3

2

N 8 did_swap false

No Swap

Page 93: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Fifth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

3

3

N 8 did_swap false

Page 94: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

The Fifth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

3

3

N 8 did_swap false

No Swap

Page 95: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

After Fifth Pass of Outer Loop

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

3

4

N 8 did_swap false

Finished fifth “Bubble Up”

Page 96: Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one. 5 12 3542 77 101 1 2 3 4 5 6 5 12 35 42.

Finished “Early”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

3

4

N 8 did_swap false

We didn’t do any swapping,so all of the other elementsmust be correctly placed.

We can “skip” the last twopasses of the outer loop.