bubble sort - emory university · sorting • lots of algorithms for sorting – selection sort...

96
Bubble Sort With thanks to Bill Leahy, GaTech for original slide deck

Upload: vodan

Post on 22-Nov-2018

236 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

Bubble Sort

With thanks to Bill Leahy, GaTech for original slide deck

Page 2: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

Sorting

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

512354277 101

0 1 2 3 4 5

5 12 35 42 77 101

0 1 2 3 4 5

Page 3: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

Sorting

• Lots of algorithms for sorting

– Selection sort (video lecture and textbook)

– Insertion sort (textbook)– Bubble sort– Merge sort – Radix sort

– ...

Page 4: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

"Bubbling Up" the Largest Element

• Traverse a collection of elements (array)

– Move from the front to the end

– “Bubble” the largest value to the end using pair-wise comparisons and swapping

512354277 101

0 1 2 3 4 5

Page 5: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

"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 101Swap42 77

0 1 2 3 4 5

Page 6: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

"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 101Swap35 77

0 1 2 3 4 5

Page 7: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

"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 101Swap12 77

0 1 2 3 4 5

Page 8: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

"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

No need to swap

0 1 2 3 4 5

Page 9: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

"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 101Swap5 101

0 1 2 3 4 5

Page 10: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

"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 101

Largest value correctly placed

0 1 2 3 4 5

Page 11: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The “Bubble Up” Algorithm

Given some array, a

for(int i = 0; i < a.length-1; i++) { if(a[i] > a[i+1]) {

int temp = a[i];

a[i] = a[i+1];

a[i+1] = temp; }}

Page 12: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

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 101

Largest value correctly placed

0 1 2 3 4 5

Page 13: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

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 14: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

“Bubbling” All the Elements

77123542 5 101

5421235 770 1 2 3 4 5

101

42 5 3512 770 1 2 3 4 5

101

42 35 512 770 1 2 3 4 5

101

42 35 12 5 770 1 2 3 4 5

101

N -

1

0 1 2 3 4 5

Page 15: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

Reducing the Number of Comparisons

12 35 42 77 1010 1 2 3 4 5

5

77123542 50 1 2 3 4 5

101

5421235 770 1 2 3 4 5

101

42 5 3512 770 1 2 3 4 5

101

42 35 512 770 1 2 3 4 5

101

Page 16: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

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 (total number of elements in array)

– Thus we have 2 comparisons to do

42 5 3512 770 1 2 3 4 5

101

Page 17: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

Putting It All Together

Page 18: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

public static void Bubblesort(int[] a){ int to_do = a.length-1;

while (to_do != 0) { for(int i = 0; i < to_do; i++) { if(a[i] > a[i+1]) { int temp = a[i];

a[i] = a[i+1];

a[i+1] = temp; } } to_do = to_do – 1; } // end while loop} //end method

Inn

er l

oo

p

Ou

ter

loo

p

Page 19: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

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 770 1 2 3 4 5

101

Page 20: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

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 21: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

public static void Bubblesort(int[] a){ int to_do = a.length-1; boolean did_swap = true;

while (to_do != 0 && did_swap) { did_swap = false; for(int i = 0; i < to_do; i++) { if(a[i] > a[i+1]) { did_swap = true; int temp = a[i];

a[i] = a[i+1];

a[i+1] = temp; } } to_do = to_do – 1; } // end while} //end method

Page 22: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

An Animated Example

674523 14 6 3398 42

to_do

i

7

0

did_swap false

0 1 2 3 4 5 6 7

Page 23: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

An Animated Example

674523 14 6 3398 42

to_do

i

7

0

Swap

did_swap false

0 1 2 3 4 5 6 7

Page 24: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

An Animated Example

674598 14 6 3323 42

to_do

i

7

1

Swap

did_swap true

0 1 2 3 4 5 6 7

Page 25: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

An Animated Example

674598 14 6 3323 42

to_do

i

7

1

did_swap true

0 1 2 3 4 5 6 7

Page 26: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

An Animated Example

674598 14 6 3323 42

to_do

i

7

1

Swap

did_swap true

0 1 2 3 4 5 6 7

Page 27: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

An Animated Example

679845 14 6 3323 42

to_do

i

7

1

Swap

did_swap true

0 1 2 3 4 5 6 7

Page 28: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

An Animated Example

679845 14 6 3323 42

to_do

i

7

2

did_swap true

0 1 2 3 4 5 6 7

Page 29: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

An Animated Example

679845 14 6 3323 42

to_do

i

7

2

Swap

did_swap true

0 1 2 3 4 5 6 7

Page 30: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

An Animated Example

671445 98 6 3323 42

to_do

i

7

2

Swap

did_swap true

0 1 2 3 4 5 6 7

Page 31: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

An Animated Example

671445 98 6 3323 42

to_do

i

7

3

did_swap true

0 1 2 3 4 5 6 7

Page 32: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

An Animated Example

671445 98 6 3323 42

to_do

i

7

3

Swap

did_swap true

0 1 2 3 4 5 6 7

Page 33: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

An Animated Example

671445 6 98 3323 42

to_do

i

7

3

Swap

did_swap true

0 1 2 3 4 5 6 7

Page 34: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

An Animated Example

671445 6 98 3323 42

to_do

i

7

4

did_swap true

0 1 2 3 4 5 6 7

Page 35: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

An Animated Example

671445 6 98 3323 42

to_do

i

7

4

Swap

did_swap true

0 1 2 3 4 5 6 7

Page 36: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

An Animated Example

981445 6 67 3323 42

to_do

i

7

4

Swap

did_swap true

0 1 2 3 4 5 6 7

Page 37: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

An Animated Example

981445 6 67 3323 42

to_do

i

7

5

did_swap true

0 1 2 3 4 5 6 7

Page 38: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

An Animated Example

981445 6 67 3323 42

to_do

i

7

5

Swap

did_swap true

0 1 2 3 4 5 6 7

Page 39: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

An Animated Example

331445 6 67 9823 42

to_do

i

7

5

Swap

did_swap true

0 1 2 3 4 5 6 7

Page 40: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

An Animated Example

331445 6 67 9823 42

to_do

i

7

6

did_swap true

0 1 2 3 4 5 6 7

Page 41: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

An Animated Example

331445 6 67 9823 42

to_do

i

7

6

Swap

did_swap true

0 1 2 3 4 5 6 7

Page 42: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

An Animated Example

331445 6 67 4223 98

to_do

i

7

6

Swap

did_swap true

0 1 2 3 4 5 6 7

Page 43: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

After First Pass of Outer Loop

331445 6 67 4223 98

to_do

i

7

7 Finished first “Bubble Up”

did_swap true

0 1 2 3 4 5 6 7

Page 44: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Second “Bubble Up”

331445 6 67 4223 98

to_do

i

6

0

did_swap false

0 1 2 3 4 5 6 7

Page 45: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Second “Bubble Up”

331445 6 67 4223 98

to_do

i

6

0

did_swap false

No Swap

0 1 2 3 4 5 6 7

Page 46: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Second “Bubble Up”

331445 6 67 4223 98

to_do

i

6

1

did_swap false

0 1 2 3 4 5 6 7

Page 47: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Second “Bubble Up”

331445 6 67 4223 98

to_do

i

6

1

did_swap false

Swap

0 1 2 3 4 5 6 7

Page 48: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Second “Bubble Up”

334514 6 67 4223 98

to_do

i

6

1

did_swap true

Swap

0 1 2 3 4 5 6 7

Page 49: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Second “Bubble Up”

334514 6 67 4223 98

to_do

i

6

2

did_swap true

0 1 2 3 4 5 6 7

Page 50: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Second “Bubble Up”

334514 6 67 4223 98

to_do

i

6

2

did_swap true

Swap

0 1 2 3 4 5 6 7

Page 51: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Second “Bubble Up”

33614 45 67 4223 98

0 1 2 3 4 5 6 7

to_do

i

6

2

did_swap true

Swap

Page 52: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Second “Bubble Up”

33614 45 67 4223 98

to_do

i

6

3

did_swap true

0 1 2 3 4 5 6 7

Page 53: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Second “Bubble Up”

33614 45 67 4223 98

to_do

i

6

3

did_swap true

No Swap

0 1 2 3 4 5 6 7

Page 54: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Second “Bubble Up”

33614 45 67 4223 98

to_do

i

6

4

did_swap true

0 1 2 3 4 5 6 7

Page 55: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Second “Bubble Up”

33614 45 67 4223 98

to_do

i

6

4

did_swap true

Swap

0 1 2 3 4 5 6 7

Page 56: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Second “Bubble Up”

67614 45 33 4223 98

to_do

i

6

4

did_swap true

Swap

0 1 2 3 4 5 6 7

Page 57: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Second “Bubble Up”

67614 45 33 4223 98

to_do

i

6

5

did_swap true

0 1 2 3 4 5 6 7

Page 58: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Second “Bubble Up”

67614 45 33 4223 98

to_do

i

6

5

did_swap true

Swap

0 1 2 3 4 5 6 7

Page 59: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Second “Bubble Up”

42614 45 33 6723 98

to_do

i

6

5

did_swap true

Swap

0 1 2 3 4 5 6 7

Page 60: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

After Second Pass of Outer Loop

42614 45 33 6723 98

to_do

i

6

6

did_swap true

Finished second “Bubble Up”

0 1 2 3 4 5 6 7

Page 61: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Third “Bubble Up”

42614 45 33 6723 98

to_do

i

5

0

did_swap false

0 1 2 3 4 5 6 7

Page 62: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Third “Bubble Up”

42614 45 33 6723 98

to_do

i

5

0

did_swap false

Swap

0 1 2 3 4 5 6 7

Page 63: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Third “Bubble Up”

42623 45 33 6714 98

to_do

i

5

0

did_swap true

Swap

0 1 2 3 4 5 6 7

Page 64: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Third “Bubble Up”

42623 45 33 6714 98

to_do

i

5

1

did_swap true

0 1 2 3 4 5 6 7

Page 65: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Third “Bubble Up”

42623 45 33 6714 98

to_do

i

5

1

did_swap true

Swap

0 1 2 3 4 5 6 7

Page 66: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Third “Bubble Up”

42236 45 33 6714 98

to_do

i

5

1

did_swap true

Swap

0 1 2 3 4 5 6 7

Page 67: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Third “Bubble Up”

42236 45 33 6714 98

to_do

i

5

2

did_swap true

0 1 2 3 4 5 6 7

Page 68: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Third “Bubble Up”

42236 45 33 6714 98

to_do

i

5

2

did_swap true

No Swap

0 1 2 3 4 5 6 7

Page 69: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Third “Bubble Up”

42236 45 33 6714 98

to_do

i

5

3

did_swap true

0 1 2 3 4 5 6 7

Page 70: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Third “Bubble Up”

42236 45 33 6714 98

to_do

i

5

3

did_swap true

Swap

0 1 2 3 4 5 6 7

Page 71: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Third “Bubble Up”

42236 33 45 6714 98

to_do

i

5

3

did_swap true

Swap

0 1 2 3 4 5 6 7

Page 72: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Third “Bubble Up”

42236 33 45 6714 98

to_do

i

5

4

did_swap true

0 1 2 3 4 5 6 7

Page 73: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Third “Bubble Up”

42236 33 45 6714 98

to_do

i

5

4

did_swap true

Swap

0 1 2 3 4 5 6 7

Page 74: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Third “Bubble Up”

45236 33 42 6714 98

to_do

i

5

4

did_swap true

Swap

0 1 2 3 4 5 6 7

Page 75: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

After Third Pass of Outer Loop

45236 33 42 6714 98

to_do

i

5

5

did_swap true

Finished third “Bubble Up”

0 1 2 3 4 5 6 7

Page 76: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Fourth “Bubble Up”

45236 33 42 6714 98

to_do

i

4

0

did_swap false

0 1 2 3 4 5 6 7

Page 77: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Fourth “Bubble Up”

45236 33 42 6714 98

to_do

i

4

0

did_swap false

Swap

0 1 2 3 4 5 6 7

Page 78: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Fourth “Bubble Up”

452314 33 42 676 98

to_do

i

4

0

did_swap true

Swap

0 1 2 3 4 5 6 7

Page 79: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Fourth “Bubble Up”

452314 33 42 676 98

to_do

i

4

1

did_swap true

0 1 2 3 4 5 6 7

Page 80: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Fourth “Bubble Up”

452314 33 42 676 98

to_do

i

4

1

did_swap true

No Swap

0 1 2 3 4 5 6 7

Page 81: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Fourth “Bubble Up”

452314 33 42 676 98

to_do

i

4

2

did_swap true

0 1 2 3 4 5 6 7

Page 82: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Fourth “Bubble Up”

452314 33 42 676 98

to_do

i

4

2

did_swap true

No Swap

0 1 2 3 4 5 6 7

Page 83: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Fourth “Bubble Up”

452314 33 42 676 98

to_do

i

4

3

did_swap true

0 1 2 3 4 5 6 7

Page 84: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Fourth “Bubble Up”

452314 33 42 676 98

to_do

i

4

3

did_swap true

No Swap

0 1 2 3 4 5 6 7

Page 85: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

After Fourth Pass of Outer Loop

452314 33 42 676 98

to_do

i

4

4

did_swap true

Finished fourth “Bubble Up”

0 1 2 3 4 5 6 7

Page 86: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Fifth “Bubble Up”

452314 33 42 676 98

to_do

i

3

0

did_swap false

0 1 2 3 4 5 6 7

Page 87: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Fifth “Bubble Up”

452314 33 42 676 98

to_do

i

3

0

did_swap false

No Swap

0 1 2 3 4 5 6 7

Page 88: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Fifth “Bubble Up”

452314 33 42 676 98

to_do

i

3

1

did_swap false

0 1 2 3 4 5 6 7

Page 89: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Fifth “Bubble Up”

452314 33 42 676 98

to_do

i

3

1

did_swap false

No Swap

0 1 2 3 4 5 6 7

Page 90: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Fifth “Bubble Up”

452314 33 42 676 98

to_do

i

3

2

did_swap false

0 1 2 3 4 5 6 7

Page 91: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

The Fifth “Bubble Up”

452314 33 42 676 98

to_do

i

3

2

did_swap false

No Swap

0 1 2 3 4 5 6 7

Page 92: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

After Fifth Pass of Outer Loop

452314 33 42 676 98

to_do

i

3

3

did_swap false

Finished fifth “Bubble Up”

0 1 2 3 4 5 6 7

Page 93: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

Finished “Early”

452314 33 42 676 98

to_do

i

3

3

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

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

0 1 2 3 4 5 6 7

Page 94: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

Summary

• “Bubble Up” algorithm will move largest value to its correct location (to the right/end of array)

• 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

Page 95: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

Truth in CS Act

• NOBODY EVER USES BUBBLE SORT

• NOBODY

• NOT EVER

• BECAUSE IT IS EXTREMELY INEFFICIENT

Page 96: Bubble Sort - Emory University · Sorting • Lots of algorithms for sorting – Selection sort (video lecture and textbook) – Insertion sort (textbook) – Bubble sort – Merge

Questions?