bubble sort

85
Bubble Sort

Upload: ali-asgher

Post on 16-Jan-2017

97 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Bubble sort

Bubble Sort

Page 2: Bubble sort

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

512354277 101

1 2 3 4 5 6

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

512354277 101

1 2 3 4 5 6Swap42 77

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

512357742 101

1 2 3 4 5 6Swap35 77

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

512773542 101

1 2 3 4 5 6Swap12 77

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

577123542 101

1 2 3 4 5 6

No need to swap

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

577123542 101

1 2 3 4 5 6Swap5 101

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

77123542 5

1 2 3 4 5 6

101

Largest value correctly placed

Page 10: Bubble sort

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 11: 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

77123542 5

1 2 3 4 5 6

101

Largest value correctly placed

Page 12: Bubble sort

An Animated Example

674523 14 6 3398 42

1 2 3 4 5 6 7 8

to_doindex

7

N 8 did_swap true

Page 13: Bubble sort

An Animated Example

674523 14 6 3398 42

1 2 3 4 5 6 7 8

to_doindex

71

N 8 did_swap false

Page 14: Bubble sort

An Animated Example

674523 14 6 3398 42

1 2 3 4 5 6 7 8

to_doindex

71

N 8

Swap

did_swap false

Page 15: Bubble sort

An Animated Example

674598 14 6 3323 42

1 2 3 4 5 6 7 8

to_doindex

71

N 8

Swap

did_swap true

Page 16: Bubble sort

An Animated Example

674598 14 6 3323 42

1 2 3 4 5 6 7 8

to_doindex

72

N 8 did_swap true

Page 17: Bubble sort

An Animated Example

674598 14 6 3323 42

1 2 3 4 5 6 7 8

to_doindex

72

N 8

Swap

did_swap true

Page 18: Bubble sort

An Animated Example

679845 14 6 3323 42

1 2 3 4 5 6 7 8

to_doindex

72

N 8

Swap

did_swap true

Page 19: Bubble sort

An Animated Example

679845 14 6 3323 42

1 2 3 4 5 6 7 8

to_doindex

73

N 8 did_swap true

Page 20: Bubble sort

An Animated Example

679845 14 6 3323 42

1 2 3 4 5 6 7 8

to_doindex

73

N 8

Swap

did_swap true

Page 21: Bubble sort

An Animated Example

671445 98 6 3323 42

1 2 3 4 5 6 7 8

to_doindex

73

N 8

Swap

did_swap true

Page 22: Bubble sort

An Animated Example

671445 98 6 3323 42

1 2 3 4 5 6 7 8

to_doindex

74

N 8 did_swap true

Page 23: Bubble sort

An Animated Example

671445 98 6 3323 42

1 2 3 4 5 6 7 8

to_doindex

74

N 8

Swap

did_swap true

Page 24: Bubble sort

An Animated Example

671445 6 98 3323 42

1 2 3 4 5 6 7 8

to_doindex

74

N 8

Swap

did_swap true

Page 25: Bubble sort

An Animated Example

671445 6 98 3323 42

1 2 3 4 5 6 7 8

to_doindex

75

N 8 did_swap true

Page 26: Bubble sort

An Animated Example

671445 6 98 3323 42

1 2 3 4 5 6 7 8

to_doindex

75

N 8

Swap

did_swap true

Page 27: Bubble sort

An Animated Example

981445 6 67 3323 42

1 2 3 4 5 6 7 8

to_doindex

75

N 8

Swap

did_swap true

Page 28: Bubble sort

An Animated Example

981445 6 67 3323 42

1 2 3 4 5 6 7 8

to_doindex

76

N 8 did_swap true

Page 29: Bubble sort

An Animated Example

981445 6 67 3323 42

1 2 3 4 5 6 7 8

to_doindex

76

N 8

Swap

did_swap true

Page 30: Bubble sort

An Animated Example

331445 6 67 9823 42

1 2 3 4 5 6 7 8

to_doindex

76

N 8

Swap

did_swap true

Page 31: Bubble sort

An Animated Example

331445 6 67 9823 42

1 2 3 4 5 6 7 8

to_doindex

77

N 8 did_swap true

Page 32: Bubble sort

An Animated Example

331445 6 67 9823 42

1 2 3 4 5 6 7 8

to_doindex

77

N 8

Swap

did_swap true

Page 33: Bubble sort

An Animated Example

331445 6 67 4223 98

1 2 3 4 5 6 7 8

to_doindex

77

N 8

Swap

did_swap true

Page 34: Bubble sort

After First Pass of Outer Loop

331445 6 67 4223 98

1 2 3 4 5 6 7 8

to_doindex

78

N 8

Finished first “Bubble Up”

did_swap true

Page 35: Bubble sort

The Second “Bubble Up”

331445 6 67 4223 98

1 2 3 4 5 6 7 8

to_doindex

61

N 8 did_swap false

Page 36: Bubble sort

The Second “Bubble Up”

331445 6 67 4223 98

1 2 3 4 5 6 7 8

to_doindex

61

N 8 did_swap false

No Swap

Page 37: Bubble sort

The Second “Bubble Up”

331445 6 67 4223 98

1 2 3 4 5 6 7 8

to_doindex

62

N 8 did_swap false

Page 38: Bubble sort

The Second “Bubble Up”

331445 6 67 4223 98

1 2 3 4 5 6 7 8

to_doindex

62

N 8 did_swap false

Swap

Page 39: Bubble sort

The Second “Bubble Up”

334514 6 67 4223 98

1 2 3 4 5 6 7 8

to_doindex

62

N 8 did_swap true

Swap

Page 40: Bubble sort

The Second “Bubble Up”

334514 6 67 4223 98

1 2 3 4 5 6 7 8

to_doindex

63

N 8 did_swap true

Page 41: Bubble sort

The Second “Bubble Up”

334514 6 67 4223 98

1 2 3 4 5 6 7 8

to_doindex

63

N 8 did_swap true

Swap

Page 42: Bubble sort

The Second “Bubble Up”

33614 45 67 4223 98

1 2 3 4 5 6 7 8

to_doindex

63

N 8 did_swap true

Swap

Page 43: Bubble sort

The Second “Bubble Up”

33614 45 67 4223 98

1 2 3 4 5 6 7 8

to_doindex

64

N 8 did_swap true

Page 44: Bubble sort

The Second “Bubble Up”

33614 45 67 4223 98

1 2 3 4 5 6 7 8

to_doindex

64

N 8 did_swap true

No Swap

Page 45: Bubble sort

The Second “Bubble Up”

33614 45 67 4223 98

1 2 3 4 5 6 7 8

to_doindex

65

N 8 did_swap true

Page 46: Bubble sort

The Second “Bubble Up”

33614 45 67 4223 98

1 2 3 4 5 6 7 8

to_doindex

65

N 8 did_swap true

Swap

Page 47: Bubble sort

The Second “Bubble Up”

67614 45 33 4223 98

1 2 3 4 5 6 7 8

to_doindex

65

N 8 did_swap true

Swap

Page 48: Bubble sort

The Second “Bubble Up”

67614 45 33 4223 98

1 2 3 4 5 6 7 8

to_doindex

66

N 8 did_swap true

Page 49: Bubble sort

The Second “Bubble Up”

67614 45 33 4223 98

1 2 3 4 5 6 7 8

to_doindex

66

N 8 did_swap true

Swap

Page 50: Bubble sort

The Second “Bubble Up”

42614 45 33 6723 98

1 2 3 4 5 6 7 8

to_doindex

66

N 8 did_swap true

Swap

Page 51: Bubble sort

After Second Pass of Outer Loop

42614 45 33 6723 98

1 2 3 4 5 6 7 8

to_doindex

67

N 8 did_swap true

Finished second “Bubble Up”

Page 52: Bubble sort

The Third “Bubble Up”

42614 45 33 6723 98

1 2 3 4 5 6 7 8

to_doindex

51

N 8 did_swap false

Page 53: Bubble sort

The Third “Bubble Up”

42614 45 33 6723 98

1 2 3 4 5 6 7 8

to_doindex

51

N 8 did_swap false

Swap

Page 54: Bubble sort

The Third “Bubble Up”

42623 45 33 6714 98

1 2 3 4 5 6 7 8

to_doindex

51

N 8 did_swap true

Swap

Page 55: Bubble sort

The Third “Bubble Up”

42623 45 33 6714 98

1 2 3 4 5 6 7 8

to_doindex

52

N 8 did_swap true

Page 56: Bubble sort

The Third “Bubble Up”

42623 45 33 6714 98

1 2 3 4 5 6 7 8

to_doindex

52

N 8 did_swap true

Swap

Page 57: Bubble sort

The Third “Bubble Up”

42236 45 33 6714 98

1 2 3 4 5 6 7 8

to_doindex

52

N 8 did_swap true

Swap

Page 58: Bubble sort

The Third “Bubble Up”

42236 45 33 6714 98

1 2 3 4 5 6 7 8

to_doindex

53

N 8 did_swap true

Page 59: Bubble sort

The Third “Bubble Up”

42236 45 33 6714 98

1 2 3 4 5 6 7 8

to_doindex

53

N 8 did_swap true

No Swap

Page 60: Bubble sort

The Third “Bubble Up”

42236 45 33 6714 98

1 2 3 4 5 6 7 8

to_doindex

54

N 8 did_swap true

Page 61: Bubble sort

The Third “Bubble Up”

42236 45 33 6714 98

1 2 3 4 5 6 7 8

to_doindex

54

N 8 did_swap true

Swap

Page 62: Bubble sort

The Third “Bubble Up”

42236 33 45 6714 98

1 2 3 4 5 6 7 8

to_doindex

54

N 8 did_swap true

Swap

Page 63: Bubble sort

The Third “Bubble Up”

42236 33 45 6714 98

1 2 3 4 5 6 7 8

to_doindex

55

N 8 did_swap true

Page 64: Bubble sort

The Third “Bubble Up”

42236 33 45 6714 98

1 2 3 4 5 6 7 8

to_doindex

55

N 8 did_swap true

Swap

Page 65: Bubble sort

The Third “Bubble Up”

45236 33 42 6714 98

1 2 3 4 5 6 7 8

to_doindex

55

N 8 did_swap true

Swap

Page 66: Bubble sort

After Third Pass of Outer Loop

45236 33 42 6714 98

1 2 3 4 5 6 7 8

to_doindex

56

N 8 did_swap true

Finished third “Bubble Up”

Page 67: Bubble sort

The Fourth “Bubble Up”

45236 33 42 6714 98

1 2 3 4 5 6 7 8

to_doindex

41

N 8 did_swap false

Page 68: Bubble sort

The Fourth “Bubble Up”

45236 33 42 6714 98

1 2 3 4 5 6 7 8

to_doindex

41

N 8 did_swap false

Swap

Page 69: Bubble sort

The Fourth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_doindex

41

N 8 did_swap true

Swap

Page 70: Bubble sort

The Fourth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_doindex

42

N 8 did_swap true

Page 71: Bubble sort

The Fourth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_doindex

42

N 8 did_swap true

No Swap

Page 72: Bubble sort

The Fourth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_doindex

43

N 8 did_swap true

Page 73: Bubble sort

The Fourth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_doindex

43

N 8 did_swap true

No Swap

Page 74: Bubble sort

The Fourth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_doindex

44

N 8 did_swap true

Page 75: Bubble sort

The Fourth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_doindex

44

N 8 did_swap true

No Swap

Page 76: Bubble sort

After Fourth Pass of Outer Loop

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_doindex

45

N 8 did_swap true

Finished fourth “Bubble Up”

Page 77: Bubble sort

The Fifth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_doindex

31

N 8 did_swap false

Page 78: Bubble sort

The Fifth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_doindex

31

N 8 did_swap false

No Swap

Page 79: Bubble sort

The Fifth “Bubble Up”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_doindex

32

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_doindex

32

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_doindex

33

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_doindex

33

N 8 did_swap false

No Swap

Page 83: Bubble sort

After Fifth Pass of Outer Loop

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_doindex

34

N 8 did_swap false

Finished fifth “Bubble Up”

Page 84: Bubble sort

Finished “Early”

452314 33 42 676 98

1 2 3 4 5 6 7 8

to_doindex

34

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.

Page 85: 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