You are on page 1of 25

BUBBLE SORT

Pass 1:
55 33 22 11 44 55>33 SWAP
BUBBLE SORT
Pass 1:
55 33 22 11 44 55>33 SWAP
33 55 22 11 44 55>22 SWAP
BUBBLE SORT
Pass 1:
55 33 22 11 44 55>33 SWAP
33 55 22 11 44 55>22 SWAP
33 22 55 11 44 55>11 SWAP
BUBBLE SORT
Pass 1:
55 33 22 11 44 55>33 SWAP
33 55 22 11 44 55>22 SWAP
33 22 55 11 44 55>11 SWAP
33 22 11 55 44 55>44 SWAP
BUBBLE SORT
Pass 1:
55 33 22 11 44 55>33 SWAP
33 55 22 11 44 55>22 SWAP
33 22 55 11 44 55>11 SWAP
33 22 11 55 44 55>44 SWAP
33 22 11 44 55
UNSORTED SORTED
BUBBLE SORT
Pass 1:
55 33 22 11 44 55>33 SWAP
33 55 22 11 44 55>22 SWAP
33 22 55 11 44 55>11 SWAP
33 22 11 55 44 55>44 SWAP
33 22 11 44 55
UNSORTED SORTED

Pass 2:
33 22 11 44 33>22 SWAP
22 33 11 44 33>11 SWAP
22 11 33 44 33>44 NO SWAP
22 11 33 44
BUBBLE SORT
Pass 1: Pass 3:
55 33 22 11 44 55>33 SWAP 22 11 33 22>11 SWAP
11 22 33
33 55 22 11 44 55>22 SWAP 22>33 NO SWAP
11 22 33
33 22 55 11 44 55>11 SWAP
33 22 11 55 44 55>44 SWAP
33 22 11 44 55
UNSORTED SORTED

Pass 2:
33 22 11 44 33>22 SWAP
22 33 11 44 33>11 SWAP
22 11 33 44 33>44 NO SWAP
22 11 33 44
BUBBLE SORT
Pass 1: Pass 3:
55 33 22 11 44 55>33 SWAP 22 11 33 22>11 SWAP
11 22 33
33 55 22 11 44 55>22 SWAP 22>33 NO SWAP
11 22 33
33 22 55 11 44 55>11 SWAP
33 22 11 55 44 55>44 SWAP
33 22 11 44 55 Pass 4:
UNSORTED SORTED 11 22
11 22 11>22 NO SWAP

Pass 2:
33 22 11 44 33>22 SWAP
22 33 11 44 33>11 SWAP
22 11 33 44 33>44 NO SWAP
22 11 33 44
BUBBLE SORT
Pass 1: Pass 3:
55 33 22 11 44 55>33 SWAP 22 11 33 22>11 SWAP
11 22 33
33 55 22 11 44 55>22 SWAP 22>33 NO SWAP
11 22 33
33 22 55 11 44 55>11 SWAP
33 22 11 55 44 55>44 SWAP
33 22 11 44 55 Pass 4:
UNSORTED SORTED 11 22
11 22 11>22 NO SWAP

Pass 2: At the end of each pass:


33 22 11 44 33>22 SWAP 1st Pass: 33 22 11 44 55
22 33 11 44 33>11 SWAP 2nd Pass: 22 11 33 44 v
3rd Pass: 11 22 33
22 11 33 44 33>44 NO SWAP
4th Pass: 11 22
22 11 33 44
BUBBLE SORT
Pass 1: Pass 3:
Pass 1: 5-1 = 4 Pass 3: 5-3 = 2
55 33 22 11 44 55>33 SWAP 22 11 33 22>11 SWAP
11 22 33
33 55 22 11 44 55>22 SWAP 22>33 NO SWAP
11 22 33
33 22 55 11 44 55>11 SWAP
33 22 11 55 44 55>44 SWAP Pass 4:
5-4 = 1
33 22 11 44 55 Pass 4:
UNSORTED SORTED 11 22
11 22 11>22 NO SWAP
Pass 2:
5-2 = 3
Pass 2: At the end of each pass:
33 22 11 44 33>22 SWAP 1st Pass: 33 22 11 44 55
22 33 11 44 33>11 SWAP 2nd Pass: 22 11 33 44 v
3rd Pass: 11 22 33
22 11 33 44 33>44 NO SWAP
4th Pass: 11 22
22 11 33 44

(5-i) i.e., (n-i)


BUBBLE SORT

Step 1: Repeat steps 2 and 3 For i=1 to N-1


Step 2: Repeat steps 3 For j=1 to N-i
Step 3: [Exchange Elements]
if A[j]>A[j+1], then
Temp = A[j]
A[j] = A[j+1] Swap A[j] & A[j+1]
A[j+1] = Temp
[End of if statement]
[End of step 2 statement]
[End of step 1 statement]
Step 4: Exit
Complexity of BUBBLE SORT
We have a total of “n” elements, in different phases:

1. After First Pass, the comparisons done are 4 n-1


2. After Second Pass, the comparisons done are 3 n-2
3. After Third Pass, the comparisons done are 2 n-3
.
.
.
k. After n-1th Pass, the comparisons done is 1 1

i.e., Complexity = (n-1)(n-2)(n-3)….1


= ∑(n-1)
= n(n-1)/2

So, the time complexity is O(n2)


BUBBLE SORT
44 60 18 10 22 15 36 19
PASS 1: (i) 44 60 18 10 22 15 36 19 If 44<60 Then no change
1 2 3 4 5 6 7 8
BUBBLE SORT
44 60 18 10 22 15 36 19
PASS 1: (i) 44 60 18 10 22 15 36 19 If 44<60 Then no change
1 2 3 4 5 6 7 8
(ii) 44 60 18 10 22 15 36 19 If 60>18 Then interchange
1 2 3 4 5 6 7 8
BUBBLE SORT
44 60 18 10 22 15 36 19
PASS 1: (i) 44 60 18 10 22 15 36 19 If 44<60 Then no change
1 2 3 4 5 6 7 8
(ii) 44 60 18 10 22 15 36 19 If 60>18 Then interchange
1 2 3 4 5 6 7 8

(iii) 44 18 60 10 22 15 36 19 If 60>10 Then interchange


1 2 3 4 5 6 7 8
BUBBLE SORT
44 60 18 10 22 15 36 19
PASS 1: (i) 44 60 18 10 22 15 36 19 If 44<60 Then no change
1 2 3 4 5 6 7 8
(ii) 44 60 18 10 22 15 36 19 If 60>18 Then interchange
1 2 3 4 5 6 7 8

(iii) 44 18 60 10 22 15 36 19 If 60>10 Then interchange


1 2 3 4 5 6 7 8
(iv) 44 18 10 60 22 15 36 19 If 60>22 Then interchange
1 2 3 4 5 6 7 8
BUBBLE SORT
44 60 18 10 22 15 36 19
PASS 1: (i) 44 60 18 10 22 15 36 19 If 44<60 Then no change
1 2 3 4 5 6 7 8
(ii) 44 60 18 10 22 15 36 19 If 60>18 Then interchange
1 2 3 4 5 6 7 8

(iii) 44 18 60 10 22 15 36 19 If 60>10 Then interchange


1 2 3 4 5 6 7 8
(iv) 44 18 10 60 22 15 36 19 If 60>22 Then interchange
1 2 3 4 5 6 7 8

(v) 44 18 10 22 60 15 36 19 If 60>15 Then interchange


1 2 3 4 5 6 7 8
BUBBLE SORT
44 60 18 10 22 15 36 19
PASS 1: (i) 44 60 18 10 22 15 36 19 If 44<60 Then no change
1 2 3 4 5 6 7 8
(ii) 44 60 18 10 22 15 36 19 If 60>18 Then interchange
1 2 3 4 5 6 7 8

(iii) 44 18 60 10 22 15 36 19 If 60>10 Then interchange


1 2 3 4 5 6 7 8
(iv) 44 18 10 60 22 15 36 19 If 60>22 Then interchange
1 2 3 4 5 6 7 8

(v) 44 18 10 22 60 15 36 19 If 60>15 Then interchange


1 2 3 4 5 6 7 8

(vi) 44 18 10 22 15 60 36 19 If 60>36 Then interchange


1 2 3 4 5 6 7 8
BUBBLE SORT
44 60 18 10 22 15 36 19
PASS 1: (i) 44 60 18 10 22 15 36 19 If 44<60 Then no change
1 2 3 4 5 6 7 8
(ii) 44 60 18 10 22 15 36 19 If 60>18 Then interchange
1 2 3 4 5 6 7 8

(iii) 44 18 60 10 22 15 36 19 If 60>10 Then interchange


1 2 3 4 5 6 7 8
(iv) 44 18 10 60 22 15 36 19 If 60>22 Then interchange
1 2 3 4 5 6 7 8

(v) 44 18 10 22 60 15 36 19 If 60>15 Then interchange


1 2 3 4 5 6 7 8

(vi) 44 18 10 22 15 60 36 19 If 60>36 Then interchange


1 2 3 4 5 6 7 8

(vii) 44 18 10 22 15 36 60 19 If 60>19 Then interchange


1 2 3 4 5 6 7 8
44 18 10 22 15 36 19 60
1 2 3 4 5 6 7 8
BUBBLE SORT
44 60 18 10 22 15 36 19
PASS 2: (i) 18 44 10 22 15 36 19 60 If 44>18 Then interchange
1 2 3 4 5 6 7 8
BUBBLE SORT
44 60 18 10 22 15 36 19
PASS 2: (i) 18 44 10 22 15 36 19 60 If 44>18 Then interchange
1 2 3 4 5 6 7 8
(ii) 18 10 44 22 15 36 19 60 If 44>10 Then interchang
1 2 3 4 5 6 7 8
BUBBLE SORT
44 60 18 10 22 15 36 19
PASS 2: (i) 18 44 10 22 15 36 19 60 If 44>18 Then interchange
1 2 3 4 5 6 7 8
(ii) 18 10 44 22 15 36 19 60 If 44>10 Then interchange
1 2 3 4 5 6 7 8
(iii) 18 10 22 44 15 36 19 60 If 44>22 Then interchange
1 2 3 4 5 6 7 8
BUBBLE SORT
44 60 18 10 22 15 36 19
PASS 2: (i) 18 44 10 22 15 36 19 60 If 44>18 Then interchange
1 2 3 4 5 6 7 8
(ii) 18 10 44 22 15 36 19 60 If 44>10 Then interchange
1 2 3 4 5 6 7 8
(iii) 18 10 22 44 15 36 19 60 If 44>22 Then interchange
1 2 3 4 5 6 7 8
(iv) 18 10 22 15 44 36 19 60 If 44>15 Then interchange
1 2 3 4 5 6 7 8
BUBBLE SORT
44 60 18 10 22 15 36 19
PASS 2: (i) 18 44 10 22 15 36 19 60 If 44>18 Then interchange
1 2 3 4 5 6 7 8
(ii) 18 10 44 22 15 36 19 60 If 44>10 Then interchange
1 2 3 4 5 6 7 8
(iii) 18 10 22 44 15 36 19 60 If 44>22 Then interchange
1 2 3 4 5 6 7 8
(iv) 18 10 22 15 44 36 19 60 If 44>15 Then interchange
1 2 3 4 5 6 7 8
(v) 18 10 22 15 36 44 19 60 If 44>36 Then interchange
1 2 3 4 5 6 7 8
BUBBLE SORT
44 60 18 10 22 15 36 19
PASS 2: (i) 18 44 10 22 15 36 19 60 If 44>18 Then interchange
1 2 3 4 5 6 7 8
(ii) 18 10 44 22 15 36 19 60 If 44>10 Then interchange
1 2 3 4 5 6 7 8
(iii) 18 10 22 44 15 36 19 60 If 44>22 Then interchange
1 2 3 4 5 6 7 8
(iv) 18 10 22 15 44 36 19 60 If 44>15 Then interchange
1 2 3 4 5 6 7 8
(v) 18 10 22 15 36 44 19 60 If 44>36 Then interchange
1 2 3 4 5 6 7 8

(vi) 18 10 22 15 36 19 44 60 If 44>19 Then interchange


1 2 3 4 5 6 7 8
18 10 22 15 36 19 44 60

You might also like