93
Bubble Sort Prepared By: Rao Muhammad Salman MCS The Islamia University BahawalPur(Pakistan)

Bubble sort

  • Upload
    rmsz786

  • View
    296

  • Download
    0

Embed Size (px)

DESCRIPTION

Bubble Sort Complete Description with algorithm, coding and step by step dry run.

Citation preview

Page 1: Bubble sort

Bubble Sort

Prepared By:

Rao Muhammad Salman MCS

The Islamia University BahawalPur(Pakistan)

Page 2: Bubble sort

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

Page 3: Bubble sort

"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

Page 4: Bubble sort

"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

Page 5: Bubble sort

"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

Page 6: Bubble sort

"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

Page 7: Bubble sort

"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

Page 8: Bubble sort

"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

Page 9: Bubble sort

"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

Page 10: Bubble sort

"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

Page 11: Bubble sort

"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

Page 12: Bubble sort

The “Bubble Up” Algorithm

index <- 1last_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 + 1endloopinner index <- index - 1endloopouter

Page 13: Bubble sort

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

Page 14: Bubble sort

Bubble Sort Code

void bubble_sort( int data[ ], int size)

{

int outer, inner,temp;

for(outer = size-1; outer =>0; --outer)

{

for(inner = 0; inner < outer;++inner)

if(data[inner] > data[inner+1])

{

temp = data[inner];

data[inner] = data[inner+1];

data[inner+1] = temp;

}

}

}

Page 15: Bubble sort

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

Page 16: Bubble sort

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.

Page 17: Bubble sort

Dry Run

Page 18: Bubble sort

674523 14 6 3398 42

1 2 3 4 5 6 7 8

to_do

index

7

N 8 did_swap true

The First “Bubble Up”

Page 19: Bubble sort

674523 14 6 3398 42

1 2 3 4 5 6 7 8

to_do

index

7

1

N 8 did_swap false

The First “Bubble Up”

Page 20: Bubble sort

The First “Bubble Up”

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 21: Bubble sort

The First “Bubble Up”

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 22: Bubble sort

The First “Bubble Up”

674598 14 6 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

2

N 8 did_swap true

Page 23: Bubble sort

The First “Bubble Up”

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 24: Bubble sort

The First “Bubble Up”

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 25: Bubble sort

The First “Bubble Up”

679845 14 6 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

3

N 8 did_swap true

Page 26: Bubble sort

The First “Bubble Up”

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 27: Bubble sort

The First “Bubble Up”

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 28: Bubble sort

The First “Bubble Up”

671445 98 6 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

4

N 8 did_swap true

Page 29: Bubble sort

The First “Bubble Up”

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 30: Bubble sort

The First “Bubble Up”

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 31: Bubble sort

The First “Bubble Up”

671445 6 98 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

5

N 8 did_swap true

Page 32: Bubble sort

The First “Bubble Up”

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 33: Bubble sort

The First “Bubble Up”

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 34: Bubble sort

The First “Bubble Up”

981445 6 67 3323 42

1 2 3 4 5 6 7 8

to_do

index

7

6

N 8 did_swap true

Page 35: Bubble sort

The First “Bubble Up”

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 36: Bubble sort

The First “Bubble Up”

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 37: Bubble sort

The First “Bubble Up”

331445 6 67 9823 42

1 2 3 4 5 6 7 8

to_do

index

7

7

N 8 did_swap true

Page 38: Bubble sort

The First “Bubble Up”

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 39: Bubble sort

The First “Bubble Up”

331445 6 67 4223 98

1 2 3 4 5 6 7 8

to_do

index

7

7

N 8 did_swap true

Finished 1st “Bubble Up”

No Swap

Page 40: Bubble sort

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 41: Bubble sort

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 42: Bubble sort

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 43: Bubble sort

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 44: Bubble sort

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 45: Bubble sort

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 46: Bubble sort

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 47: Bubble sort

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 48: Bubble sort

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 49: Bubble sort

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 50: Bubble sort

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 51: Bubble sort

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 52: Bubble sort

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 53: Bubble sort

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 54: Bubble sort

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 55: Bubble sort

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

Finished 2nd “Bubble Up”

Page 56: Bubble sort

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 57: Bubble sort

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 58: Bubble sort

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 59: Bubble sort

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 60: Bubble sort

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 61: Bubble sort

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 62: Bubble sort

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 63: Bubble sort

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 64: Bubble sort

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 65: Bubble sort

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 66: Bubble sort

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 67: Bubble sort

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 68: Bubble sort

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 69: Bubble sort

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

Finished 3rd “Bubble Up”

Page 70: Bubble sort

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 71: Bubble sort

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 72: Bubble sort

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 73: Bubble sort

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 74: Bubble sort

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 75: Bubble sort

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 76: Bubble sort

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 77: Bubble sort

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 78: Bubble sort

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

Finished 4th “Bubble Up”

Page 79: Bubble sort

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 80: Bubble sort

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 81: Bubble sort

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 82: Bubble sort

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 83: Bubble sort

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 84: Bubble sort

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

Finished 5th “Bubble Up”

Page 85: Bubble sort

The Sixth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

2

1

N 8 did_swap false

Page 86: Bubble sort

The Sixth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

2

1

N 8 did_swap false

No Swap

Page 87: Bubble sort

The Sixth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

2

2

N 8 did_swap false

Page 88: Bubble sort

The Sixth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

2

2

N 8 did_swap false

No Swap

Finished 6th “Bubble Up”

Page 89: Bubble sort

The Seventh “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

1

1

N 8 did_swap false

Page 90: Bubble sort

The Seventh “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_do

index

1

1

N 8 did_swap false

No Swap

Finished 7th “Bubble Up”

Page 91: Bubble sort

Sorted Elements

452314 33 42 676 98

1 2 3 4 5 6 7 8

Page 92: Bubble sort

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

Page 93: Bubble sort