You are on page 1of 28

MODULE-III

BCSE202L -
COURSE INSTRUCTOR:
DATA STRUCTURES AND
DR. SUGUNA M
ASSISTANT PROFESSOR,
ALGORITHMS
SCOPE,VIT.

DR.SUGUNA M 26-08-2022 1
BUBBLE SORTING
Bubble Sort

•Bubble sort is the easiest sorting algorithm to implement.


•It is inspired by observing the behavior of air bubbles over foam.
•It is an in-place sorting algorithm.
•It uses no auxiliary data structures (extra space) while sorting.

How Bubble Sort Works?

•Bubble sort uses multiple passes (scans) through an array.


•In each pass, bubble sort compares the adjacent elements of the
array.
•It then swaps the two elements if they are in the wrong order.
•In each pass, bubble sort places the next largest element to its proper
position.
•In short, it bubbles down the largest element to its correct position.
IMPLEMENTING BUBBLE SORT ALGORITHM

Algorithm BubbleSort ( A [],n )


{

for ( pass = 1 to n-1 )


for ( j= 0 to n – 1 – pass )
If ( A [j] > A [j+1] )
t = A [j]
A [j] = A [j+1]
A [j+1] = t
}

4
Implementing Bubble Sort Algorithm

Step-01:

• pass=1 and i=0. Step-02:


•perform the comparison A[0] > A[1] and swaps if the
0th element is greater than the 1th element. •pass=1 and i=1.
•Since 6 > 2, so swap the two elements. •perform the comparison A[1] > A[2] and swaps if the
1th element is greater than the 2th element.
•Since 6 < 11, so no swapping is required.

5
Implementing Bubble Sort Algorithm

Step-04:
Step-03:
•pass=1 and i=3.
•pass=1 and i=2.
•perform the comparison A[3] > A[4] and
•perform the comparison A[2] > A[3] and swaps if
swaps if the 3rd element is greater than
the 2nd element is greater than the 3rd element.
the 4th element.
•Since 11 > 7, so we swap the two elements.
•Since 11 > 5, so we swap the two
elements.

Finally after the first pass, To see that the largest element 11 reaches its correct position.
Implementing Bubble Sort Algorithm

Step-05:
Step-06:
•Similarly after pass=2, element 7
reaches its correct position. •Similarly after pass=3, element 6 reaches
•The modified array after pass=2 is its correct position.
shown below- •The modified array after pass=3 is shown
below-

Step-07:

•No further improvement is done in pass=4.


•This is because at this point, elements 2 and 5 are already
present at their correct positions.
•The loop terminates after pass=4.
•Finally, the array after pass=4 is shown below-
IMPLEMENTING BUBBLE SORT ALGORITHM

To understand the implementation of bubble sort


algorithm, consider an unsorted list of numbers stored
in an array.
0 1 2 3 4
arr 5 2 6 7 3

8
IMPLEMENTING BUBBLE SORT ALGORITHM (CONTD.)

Let us sort this unsorted list.

0 1 2 3 4
arr 5 2 6 7 3

9
IMPLEMENTING BUBBLE SORT ALGORITHM (CONTD.)

 Pass 1
 n=5
 Compare the element stored at index 0 with the element stored at index 1.
0 1 2 3 4
arr 5 2 6 7 3

10
IMPLEMENTING BUBBLE SORT ALGORITHM (CONTD.)

Pass 1
n=5
Swap the values if they are not in the correct order.
Swap
0 1 2 3 4
arr 2 5 6 7 3

11
IMPLEMENTING BUBBLE SORT ALGORITHM (CONTD.)

 Pass 1
 n=5
 Compare the element stored at index 1 with the element stored
at index 2 and swap the values if the value at index 1 is greater than the value at index 2.

 No Change

 0 1 2 3 4
arr 2 5 6 7 3

12
IMPLEMENTING BUBBLE SORT ALGORITHM (CONTD.)
 Pass 1
 n=5
 Compare the element stored at index 2 with the element stored
at index 3 and swap the values if the value at index 2 is greater
than the value at index 3.
 No Change

arr  02 15 2
6 3
7 4
3

13
IMPLEMENTING BUBBLE SORT ALGORITHM (CONTD.)

 Pass 1
 n=5
 Compare the element stored at index 3 with the element stored at index 4
and swap the values if the value at index 3 is greater than the value at index
4.

Swap arr 2 5 6 7 3

14
IMPLEMENTING BUBBLE SORT ALGORITHM (CONTD.)

 Pass 1
 n=5
 Compare the element stored at index 3 with the element stored at
index 4 and swap the values if the value at index 3 is greater than the
value at index 4.
arr 2 5 6 3 7
 0 1 2 3 4
Largest element is placed at its correct position after Pass 1

15
IMPLEMENTING BUBBLE SORT ALGORITHM (CONTD.)
 Pass 2
 n=5
 Compare the element stored at index 0 with the
element stored at index 1 and swap the values if the
value at index 0 is greater than the value at index 1.
 No Change
 0 1 2 3 4
 arr 2 5 6 3 7

16
IMPLEMENTING BUBBLE SORT ALGORITHM (CONTD.)
 Pass 2
 n=5
 Compare the element stored at index 1 with the element stored at
index 2 and swap the values if the value at
index 1 is greater than the value at index 2.
 No Change

 0 1 2 3 4
arr 2 5 6 3 7

17
IMPLEMENTING BUBBLE SORT ALGORITHM (CONTD.)
 Pass 2
 n=5
 Compare the element stored at index 2 with the element stored at
index 3 and swap the values if the value
at index 2 is greater than the value at index 3.
 Swap

 0 1 2 3 4
arr 2 5 3 6 7

18
IMPLEMENTING BUBBLE SORT ALGORITHM (CONTD.)

 Pass 2
 n=5
 Compare the element stored at index 2 with the element stored at
index 3 and swap the values if the value at index 2 is greater than the
value at index 3.
arr 2 5 3 6 7
 0 1 2 3 4
Second largest element is placed at its correct position after Pass 2

19
IMPLEMENTING BUBBLE SORT ALGORITHM (CONTD.)
 Pass 3
 n=5
 Compare the element stored at index 0 with the element stored at
index 1 and swap the values if the value
at index 0 is greater than the value at index 1.
 No Change

 0 1 2 3 4
2 5 3 6 7
 arr

20
IMPLEMENTING BUBBLE SORT ALGORITHM (CONTD.)
 Pass 3
 n=5
 Compare the element stored at index 1 with the element stored at index
and swap the values if the value
at index 1 is greater than the value at index 2.
 Swap

 0 1 2 3 4
arr 2 3 5 6 7

21
IMPLEMENTING BUBBLE SORT ALGORITHM (CONTD.)
 Pass 3
 n=5
 Compare the element stored at index 2 with the element stored at index
and swap the values if the value
at index 2 is greater than the value at index 3.

 0 1 2 3 4
arr 2 3 5 6 7

Third largest element is placed at its correct position after Pass 3

22
IMPLEMENTING BUBBLE SORT ALGORITHM (CONTD.)

 Pass 4

 n=5
 Compare the element stored at index 0 with the element
stored at index 1 and swap the values if the value at index 0 is
greater than the value at index 1.
 No Change

 0 1 2 3 4
 arr
2 3 5 6 7

23
IMPLEMENTING BUBBLE SORT ALGORITHM (CONTD.)
 Pass 4
 n=5
 Compare the element stored at index 0 with the element stored at
index 1 and swap the values if the value
at index 0 is greater than the value at index 1.

 0 1 2 3 4
arr 2 3 5 6 7

Fourth largest element is placed at its correct position after Pass 4

24
IMPLEMENTING BUBBLE SORT ALGORITHM (CONTD.)

Pass 4
n=5
At the end of Pass 4, the elements are sorted.

0 1 2 3 4
arr 2 3 5 6 7

25
Time Complexity Analysis-

•Bubble sort uses two loops- inner loop and outer loop.
•The inner loop deterministically performs O(n) comparisons.

Worst Case-

•In worst case, the outer loop runs O(n) times.


•Hence, the worst -case time complexity of bubble sort is O(n x n) = O(n2).

Best Case-

•In best case, the array is already sorted but still to check, bubble sort performs O(n)
comparisons.
•Hence, the best-case time complexity of bubble sort is O(n).

Average Case-

•In average case, bubble sort may require (n/2) passes and O(n) comparisons for each
pass.
•Hence, the average case time complexity of bubble sort is O(n/2 x n) = Θ(n2).

26
DR.SUGUNA M 26-08-2022 27
THANK YOU

You might also like