Professional Documents
Culture Documents
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
4
Implementing Bubble Sort Algorithm
Step-01:
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:
8
IMPLEMENTING BUBBLE SORT ALGORITHM (CONTD.)
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
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
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-
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