0% found this document useful (0 votes)
11 views

5 Lecture Bubble Sort and Analysis

Uploaded by

Bilal Shabbir
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

5 Lecture Bubble Sort and Analysis

Uploaded by

Bilal Shabbir
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 94

Lecture.

Bubble sort

1
Sorting
 Sorting takes an unordered
collection and makes it an ordered
one. 1 2 3 4 5 6

77 42 35 12 101 5

1 2 3 4 5 6

5 12 35 42 77 101

2
What is Bubble Sort

 Idea:
– Repeatedly pass through the array
– Swaps adjacent elements that are out of order
i
1 2 3 n

8 4 6 9 2 3 1
j

 Easier to implement, but slower than


Insertion sort

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

77 42 35 12 101 5

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

42 Swap 42
77 77 35 12 101 5

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

42 7735 Swap 35
77 12 101 5

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

42 35 12 Swap 12
77 77 101 5

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

42 35 12 77 101 5

No need to swap

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

42 35 12 77 5 Swap 101
101 5

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

42 35 12 77 5 101

Largest value correctly placed

10
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 2 3 4 5 6

42 35 12 77 5 101

Largest value correctly placed

11
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.
12
“Bubbling” All the Elements
1 2 3 4 5 6
42 35 12 77 5 101

1 2 3 4 5 6
35 12 42 5 77 101

1 2 3 4 5 6
N-1

12 35 5 42 77 101

1 2 3 4 5 6
12 5 35 42 77 101

1 2 3 4 5 6
5 12 35 42 77 101

13
Reducing the Number of
Comparisons
1 2 3 4 5 6
77 42 35 12 101 5
1 2 3 4 5 6
42 35 12 77 5 101

1 2 3 4 5 6
35 12 42 5 77 101

1 2 3 4 5 6
12 35 5 42 77 101

1 2 3 4 5 6
12 5 35 42 77 101

14
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 1we have
2 3
2 comparisons
4 5 6
to do
12 35 5 42 77 101

15
Already Sorted Array?
 What if the array was already
sorted?
 What if only a few elements were
out of place and after a couple of
“bubble ups,” the collection was
sorted?
1 2 3 4 5 6
 We5
want12 to be
35 able
42 to detect
77 this
101
and “stop early”!

16
An Animated Example

N 8 did_swap true

to_do 7

index

98 23 45 14 6 67 33 42

1 2 3 4 5 6 7 8

17
An Animated Example

N 8 did_swap false

to_do 7

index 1

98 23 45 14 6 67 33 42

1 2 3 4 5 6 7 8

18
An Animated Example

N 8 did_swap false

to_do 7

index 1

Swap

98 23 45 14 6 67 33 42

1 2 3 4 5 6 7 8

19
An Animated Example

N 8 did_swap true

to_do 7

index 1

Swap

23 98 45 14 6 67 33 42

1 2 3 4 5 6 7 8

20
An Animated Example

N 8 did_swap true

to_do 7

index 2

23 98 45 14 6 67 33 42

1 2 3 4 5 6 7 8

21
An Animated Example

N 8 did_swap true

to_do 7

index 2

Swap

23 98 45 14 6 67 33 42

1 2 3 4 5 6 7 8

22
An Animated Example

N 8 did_swap true

to_do 7

index 2

Swap

23 45 98 14 6 67 33 42

1 2 3 4 5 6 7 8

23
An Animated Example

N 8 did_swap true

to_do 7

index 3

23 45 98 14 6 67 33 42

1 2 3 4 5 6 7 8

24
An Animated Example

N 8 did_swap true

to_do 7

index 3

Swap

23 45 98 14 6 67 33 42

1 2 3 4 5 6 7 8

25
An Animated Example

N 8 did_swap true

to_do 7

index 3

Swap

23 45 14 98 6 67 33 42

1 2 3 4 5 6 7 8

26
An Animated Example

N 8 did_swap true

to_do 7

index 4

23 45 14 98 6 67 33 42

1 2 3 4 5 6 7 8

27
An Animated Example

N 8 did_swap true

to_do 7

index 4

Swap

23 45 14 98 6 67 33 42

1 2 3 4 5 6 7 8

28
An Animated Example

N 8 did_swap true

to_do 7

index 4

Swap

23 45 14 6 98 67 33 42

1 2 3 4 5 6 7 8

29
An Animated Example

N 8 did_swap true

to_do 7

index 5

23 45 14 6 98 67 33 42

1 2 3 4 5 6 7 8

30
An Animated Example

N 8 did_swap true

to_do 7

index 5

Swap

23 45 14 6 98 67 33 42

1 2 3 4 5 6 7 8

31
An Animated Example

N 8 did_swap true

to_do 7

index 5

Swap

23 45 14 6 67 98 33 42

1 2 3 4 5 6 7 8

32
An Animated Example

N 8 did_swap true

to_do 7

index 6

23 45 14 6 67 98 33 42

1 2 3 4 5 6 7 8

33
An Animated Example

N 8 did_swap true

to_do 7

index 6

Swap

23 45 14 6 67 98 33 42

1 2 3 4 5 6 7 8

34
An Animated Example

N 8 did_swap true

to_do 7

index 6

Swap

23 45 14 6 67 33 98 42

1 2 3 4 5 6 7 8

35
An Animated Example

N 8 did_swap true

to_do 7

index 7

23 45 14 6 67 33 98 42

1 2 3 4 5 6 7 8

36
An Animated Example

N 8 did_swap true

to_do 7

index 7

Swap

23 45 14 6 67 33 98 42

1 2 3 4 5 6 7 8

37
An Animated Example

N 8 did_swap true

to_do 7

index 7

Swap

23 45 14 6 67 33 42 98

1 2 3 4 5 6 7 8

38
After First Pass of Outer
Loop

N 8 did_swap true

to_do 7

index 8 Finished first “Bubble Up”

23 45 14 6 67 33 42 98

1 2 3 4 5 6 7 8

39
The Second “Bubble Up”

N 8 did_swap false

to_do 6

index 1

23 45 14 6 67 33 42 98

1 2 3 4 5 6 7 8

40
The Second “Bubble Up”

N 8 did_swap false

to_do 6

index 1

No Swap

23 45 14 6 67 33 42 98

1 2 3 4 5 6 7 8

41
The Second “Bubble Up”

N 8 did_swap false

to_do 6

index 2

23 45 14 6 67 33 42 98

1 2 3 4 5 6 7 8

42
The Second “Bubble Up”

N 8 did_swap false

to_do 6

index 2

Swap

23 45 14 6 67 33 42 98

1 2 3 4 5 6 7 8

43
The Second “Bubble Up”

N 8 did_swap true

to_do 6

index 2

Swap

23 14 45 6 67 33 42 98

1 2 3 4 5 6 7 8

44
The Second “Bubble Up”

N 8 did_swap true

to_do 6

index 3

23 14 45 6 67 33 42 98

1 2 3 4 5 6 7 8

45
The Second “Bubble Up”

N 8 did_swap true

to_do 6

index 3

Swap

23 14 45 6 67 33 42 98

1 2 3 4 5 6 7 8

46
The Second “Bubble Up”

N 8 did_swap true

to_do 6

index 3

Swap

23 14 6 45 67 33 42 98

1 2 3 4 5 6 7 8

47
The Second “Bubble Up”

N 8 did_swap true

to_do 6

index 4

23 14 6 45 67 33 42 98

1 2 3 4 5 6 7 8

48
The Second “Bubble Up”

N 8 did_swap true

to_do 6

index 4

No Swap

23 14 6 45 67 33 42 98

1 2 3 4 5 6 7 8

49
The Second “Bubble Up”

N 8 did_swap true

to_do 6

index 5

23 14 6 45 67 33 42 98

1 2 3 4 5 6 7 8

50
The Second “Bubble Up”

N 8 did_swap true

to_do 6

index 5

Swap

23 14 6 45 67 33 42 98

1 2 3 4 5 6 7 8

51
The Second “Bubble Up”

N 8 did_swap true

to_do 6

index 5

Swap

23 14 6 45 33 67 42 98

1 2 3 4 5 6 7 8

52
The Second “Bubble Up”

N 8 did_swap true

to_do 6

index 6

23 14 6 45 33 67 42 98

1 2 3 4 5 6 7 8

53
The Second “Bubble Up”

N 8 did_swap true

to_do 6

index 6

Swap

23 14 6 45 33 67 42 98

1 2 3 4 5 6 7 8

54
The Second “Bubble Up”

N 8 did_swap true

to_do 6

index 6

Swap

23 14 6 45 33 42 67 98

1 2 3 4 5 6 7 8

55
After Second Pass of Outer
Loop

N 8 did_swap true

to_do 6

index 7 Finished second “Bubble Up”

23 14 6 45 33 42 67 98

1 2 3 4 5 6 7 8

56
The Third “Bubble Up”

N 8 did_swap false

to_do 5

index 1

23 14 6 45 33 42 67 98

1 2 3 4 5 6 7 8

57
The Third “Bubble Up”

N 8 did_swap false

to_do 5

index 1

Swap

23 14 6 45 33 42 67 98

1 2 3 4 5 6 7 8

58
The Third “Bubble Up”

N 8 did_swap true

to_do 5

index 1

Swap

14 23 6 45 33 42 67 98

1 2 3 4 5 6 7 8

59
The Third “Bubble Up”

N 8 did_swap true

to_do 5

index 2

14 23 6 45 33 42 67 98

1 2 3 4 5 6 7 8

60
The Third “Bubble Up”

N 8 did_swap true

to_do 5

index 2

Swap

14 23 6 45 33 42 67 98

1 2 3 4 5 6 7 8

61
The Third “Bubble Up”

N 8 did_swap true

to_do 5

index 2

Swap

14 6 23 45 33 42 67 98

1 2 3 4 5 6 7 8

62
The Third “Bubble Up”

N 8 did_swap true

to_do 5

index 3

14 6 23 45 33 42 67 98

1 2 3 4 5 6 7 8

63
The Third “Bubble Up”

N 8 did_swap true

to_do 5

index 3

No Swap

14 6 23 45 33 42 67 98

1 2 3 4 5 6 7 8

64
The Third “Bubble Up”

N 8 did_swap true

to_do 5

index 4

14 6 23 45 33 42 67 98

1 2 3 4 5 6 7 8

65
The Third “Bubble Up”

N 8 did_swap true

to_do 5

index 4

Swap

14 6 23 45 33 42 67 98

1 2 3 4 5 6 7 8

66
The Third “Bubble Up”

N 8 did_swap true

to_do 5

index 4

Swap

14 6 23 33 45 42 67 98

1 2 3 4 5 6 7 8

67
The Third “Bubble Up”

N 8 did_swap true

to_do 5

index 5

14 6 23 33 45 42 67 98

1 2 3 4 5 6 7 8

68
The Third “Bubble Up”

N 8 did_swap true

to_do 5

index 5

Swap

14 6 23 33 45 42 67 98

1 2 3 4 5 6 7 8

69
The Third “Bubble Up”

N 8 did_swap true

to_do 5

index 5

Swap

14 6 23 33 42 45 67 98

1 2 3 4 5 6 7 8

70
The Third “Bubble Up”

N 8 did_swap true

to_do 5

index 6 Finished third “Bubble Up”

14 6 23 33 42 45 67 98

1 2 3 4 5 6 7 8

71
The Fourth “Bubble Up”

N 8 did_swap false

to_do 4

index 1

14 6 23 33 42 45 67 98

1 2 3 4 5 6 7 8

72
The Fourth “Bubble Up”

N 8 did_swap false

to_do 4

index 1

Swap

14 6 23 33 42 45 67 98

1 2 3 4 5 6 7 8

73
The Fourth “Bubble Up”

N 8 did_swap true

to_do 4

index 1

Swap

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

74
The Fourth “Bubble Up”

N 8 did_swap true

to_do 4

index 2

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

75
The Fourth “Bubble Up”

N 8 did_swap true

to_do 4

index 2

No Swap

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

76
The Fourth “Bubble Up”

N 8 did_swap true

to_do 4

index 3

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

77
The Fourth “Bubble Up”

N 8 did_swap true

to_do 4

index 3

No Swap

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

78
The Fourth “Bubble Up”

N 8 did_swap true

to_do 4

index 4

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

79
The Fourth “Bubble Up”

N 8 did_swap true

to_do 4

index 4

No Swap

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

80
The Fourth “Bubble Up”

N 8 did_swap true

to_do 4

index 5 Finished fourth “Bubble Up”

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

81
The Fourth “Bubble Up”

N 8 did_swap false

to_do 3

index 1

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

82
The Fifth “Bubble Up”

N 8 did_swap false

to_do 3

index 1

No Swap

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

83
The Fifth “Bubble Up”

N 8 did_swap false

to_do 3

index 2

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

84
The Fifth “Bubble Up”

N 8 did_swap false

to_do 3

index 2

No Swap

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

85
The Fifth “Bubble Up”

N 8 did_swap false

to_do 3

index 3

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

86
The Fifth “Bubble Up”

N 8 did_swap false

to_do 3

index 3

No Swap

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

87
After Fifth Pass of Outer
Loop

N 8 did_swap false

to_do 3

index 4 Finished fifth “Bubble Up”

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

88
Finished “Early”

N 8 did_swap false

to_do 3
We didn’t do any swapping,
index 4 so all of the other elements
must be correctly placed.

We can “skip” the last two


passes of the outer loop.

6 14 23 33 42 45 67 98

1 2 3 4 5 6 7 8

89
Bubble Sort (Example-2)

2 9 5 4 8 1 2 5 4 8 1 9 2 4 5 1 8 9 2 4 1 5 8 9 1 2 4 5 8 9
2 5 9 4 8 1 2 4 5 8 1 9 2 4 5 1 8 9 2 1 4 5 8 9
2 5 4 9 8 1 2 4 5 8 1 9 2 4 1 5 8 9
2 5 4 8 9 1 2 4 5 1 8 9
2 5 4 8 1 9

(a) 1st pass (b) 2nd pass (c) 3rd pass (d) 4th pass (e) 5th pass

Bubble sort time: O(n2)

n2 n
(n  1)  (n  2)  ...  2  1  
2 2
90
Algorithm of Bubble Sort

proc bubbleSort(A as array)


n  |A|
for i  n to 2 do
for j 1 to i-1 do
if A[j] > A[j+1] then
swap (A[j], A[j+1])
end-if
next
next
end proc

How many times each of above statements will execu


91
Sorting Algorithms ……
Exchange(Bubble) Sort

Calculate the running cost of Bubble Sort


in class

Statement Execution Cost


proc bubbleSort(A as array)
n  |a| ?
for i  n to 2 do ?
for j ← 1 to i – 1 do ?
if a[j] > a[j+1] then ?
swap (a[j], ??
a[j+1])
end if
next
next
end proc
Sorting Algorithms ……..
Bubble Sort

Calculate the running cost of Bubble Sort


in class

Statement Execution Cost


proc bubbleSort(A as array)
n  |a| 1 times
for i  n to 2 do n times
for j ← 1 to i – 1 do (n+n-1………3++2+1 times)
if a[j] > a[j+1] then (n-1………3++2+1 times)

swap (a[j], a[j+1]) (n-1………3++2+1 times)

end if
next
next
end proc
Using a Boolean “Flag”

 We can use a boolean variable to


determine if any swapping occurred
during the “bubble up.”

 Ifno swapping occurred, then we


know that the collection is already
sorted!

 This boolean “flag” needs to be reset


after each “bubble up.”
94

You might also like