Buble Sort
Buble Sort
Bubble Sort
• The Bubble
Sort algorithm looks at
pairs of entries in the 70
array, and swaps their
order if needed. 60
50
40
30
20
10
0
[1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm
• The Bubble
Sort algorithm looks at
pairs of entries in the 70
array, and swaps their
order if needed. 60
50
40
30
20
10
0
Swap? [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm
• The Bubble
Sort algorithm looks at
pairs of entries in the 70
array, and swaps their
order if needed. 60
50
40
30
20
10
0
Yes! [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm
• The Bubble
Sort algorithm looks at
pairs of entries in the 70
array, and swaps their
order if needed. 60
50
40
30
20
10
0
Swap? [1]
[0]
[2]
[1]
[3]
[2]
[4]
[3]
[5]
[4]
[6]
[5]
The Bubble Sort Algorithm
• The Bubble
Sort algorithm looks at
pairs of entries in the 70
array, and swaps their
order if needed. 60
50
40
30
20
10
0
No. [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm
• The Bubble
Sort algorithm looks at
pairs of entries in the 70
array, and swaps their
order if needed. 60
50
40
30
20
10
0
Swap? [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm
• The Bubble
Sort algorithm looks at
pairs of entries in the 70
array, and swaps their
order if needed. 60
50
40
30
20
10
0
No. [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm
• The Bubble
Sort algorithm looks at
pairs of entries in the 70
array, and swaps their
order if needed. 60
50
40
30
20
10
0
Swap? [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm
• The Bubble
Sort algorithm looks at
pairs of entries in the 70
array, and swaps their
order if needed. 60
50
40
30
20
10
0
Yes! [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm
• The Bubble
Sort algorithm looks at
pairs of entries in the 70
array, and swaps their
order if needed. 60
50
40
30
20
10
0
Swap? [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm
• The Bubble
Sort algorithm looks at
pairs of entries in the 70
array, and swaps their
order if needed. 60
50
40
30
20
10
0
Yes! [1]
[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm
•Repeat.
70
60
50
40
30
20
10
•Repeat.
70
60
50
40
30
20
10
•Repeat.
70
60
50
40
30
20
10
•Repeat.
70
60
50
40
30
20
10
•Repeat.
70
60
50
40
30
20
10
•Repeat.
70
60
50
40
30
20
10
•Repeat.
70
60
50
40
30
20
10
50
40
30
20
10
50
40
30
20
10
50
40
30
20
10
50
40
30
20
10
50
40
30
20
10
50
40
30
20
10
50
40
30
20
10
• Continue
looping, until done.
70
60
50
40
30
20
10
•Compare, no swap
•Compare, noswap
•99 in position
Bubble Sort
•Pass 2
•swap (0, 1)
•no swap
•no swap
•swap (3, 4)
•21 in position
Bubble Sort
•Pass 3
•no swap
•no swap
•swap (2, 3)
•no swap
•swap (1, 2)
•8 in position, Pass 5
•swap (1, 2)
Bubble Sort – Algorithm
Complexity
• Lines Time
• for( k = 1 to n- 1) n-1
•{
• for(i=0 to n – 2) n-1
•{
• if(array[ i ] > array[ i + 1]) C
•{
• Swap (Array[ i ] , Array[ i + 1]
•}
•}
•}
• Time Complexity is
• (n – 1) x (n – 1) x C
2
O(n )
How to Improve Bubble Sort
Lets Assume n is Size of array and value of n = 5
for( k = 1 to 4)
{ for( i = 0 to 3) {
if(Array[i] > Array[ i + 1])
Swap (Array[ i ] , Array[ i + 1]
}
}
Array Data is as Below
Step by Step
Array Data 5 4 3 2 1 Execution of Bubble
Index 0 1 2 3 4 Sort Algorithm
Step 1 K=1
Pass 1 i=0 array[0] > array[1] Than Swap
Array Data 4 5 3 2 1
Index 0 1 2 3 4
Step 2 K=2
Pass 1 i=0 array[0] > array[1] Than Swap
Array Data 3 4 2 1 5
Index 0 1 2 3 4
Step 3 K=3
Pass 1 i=0 array[0] > array[1] Than Swap
Array Data 2 3 1 4 5
Index 0 1 2 3 4
Step 4 K=4
Pass 1 i=0 array[0] > array[1] Than Swap
Array Data 1 2 3 4 5
Index 0 1 2 3 4
• {
• Swap (Array[ i ] , Array[ i + 1]
• }
• }
•}
Array Data is as Below
Step by Step
Array Data 5 4 3 2 1 Execution of Bubble Sort
Index 0 1 2 3 4 Algorithm - Improved
Step 1 k = 1 to 4
Pass 1 i = 0 to 3 array[0] > array[1] Than Swap
Array Data 4 5 3 2 1
Index 0 1 2 3 4
Step 2 K=2 to 4
Pass 1 i= 0 to 2 array[0] > array[1] Than Swap
Array Data 3 4 2 1 5
Index 0 1 2 3 4
Step 3 K=3 to 4
Pass 1 i=0 to 1 array[0] > array[1] Than Swap
Array Data 2 3 1 4 5
Index 0 1 2 3 4
Step 4 K=4 to 4
Pass 1 i=0 to 0 array[0] > array[1] Than Swap
Array Data 1 2 3 4 5
Index 0 1 2 3 4
Pass 2 will not take place
Pass 3 will not take place
Bubble Sort – Improved
• Lines
• for( k = 1 to n- 1)
• {
• Flag = 0
• For(i=0 to n – k - 1)
• { If(Array[i] > Array[i + 1])
{
• Swap (Array[i] , Array[ i + 1]
• Flag = 1
• }
• }
• If (flag == 0)
• Break
•}
•