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

Buble Sort

The document provides an in-depth explanation of the Bubble Sort algorithm, describing its functionality as a comparison sort that swaps adjacent elements in an array to sort them. It outlines the algorithm's structure, complexity, and step-by-step execution with examples. Additionally, it discusses potential improvements to the algorithm to enhance its efficiency.

Uploaded by

rh132004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Buble Sort

The document provides an in-depth explanation of the Bubble Sort algorithm, describing its functionality as a comparison sort that swaps adjacent elements in an array to sort them. It outlines the algorithm's structure, complexity, and step-by-step execution with examples. Additionally, it discusses potential improvements to the algorithm to enhance its efficiency.

Uploaded by

rh132004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 43

Advanced Analysis of Algorithms

Bubble Sort

 Bubble sort and it is also called sinking sort.

 Bubble sort algorithm gets name bubble because of


sorting the elements in array in shorter range i.e just next
value of the element in array is checked and swapped or
we can say sorting function is perform in very smaller
time that is why it is also called comparison sort.

 Now we will see the algorithm structure as follows:


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

Swap? No. 0 [1]


[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm

•Repeat.

70
60

50

40
30

20
10

Swap? No. 0 [1]


[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm

•Repeat.

70
60

50

40
30

20
10

Swap? Yes.0 [1]


[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm

•Repeat.

70
60

50

40
30

20
10

Swap? Yes.0 [1]


[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm

•Repeat.

70
60

50

40
30

20
10

Swap? Yes.0 [1]


[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm

•Repeat.

70
60

50

40
30

20
10

Swap? Yes.0 [1]


[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [6]
[5]
The Bubble Sort Algorithm

•Repeat.

70
60

50

40
30

20
10

Swap? No. 0 [1]


[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [5][6]
The Bubble Sort Algorithm

• Loop over array


n-1 times, swapping
pairs of entries as 70
needed.
60

50

40
30

20
10

Swap? No. 0 [1]


[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [5][6]
The Bubble Sort Algorithm

• Loop over array


n-1 times, swapping
pairs of entries as 70
needed.
60

50

40
30

20
10

Swap? Yes.0 [1]


[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [5][6]
The Bubble Sort Algorithm

• Loop over array


n-1 times, swapping
pairs of entries as 70
needed.
60

50

40
30

20
10

Swap? Yes.0 [1]


[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [5][6]
The Bubble Sort Algorithm

• Loop over array


n-1 times, swapping
pairs of entries as 70
needed.
60

50

40
30

20
10

Swap? Yes.0 [1]


[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [5][6]
The Bubble Sort Algorithm

• Loop over array


n-1 times, swapping
pairs of entries as 70
needed.
60

50

40
30

20
10

Swap? Yes.0 [1]


[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [5][6]
The Bubble Sort Algorithm

• Loop over array


n-1 times, swapping
pairs of entries as 70
needed.
60

50

40
30

20
10

Swap? No. 0 [1]


[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [5][6]
The Bubble Sort Algorithm

• Loop over array


n-1 times, swapping
pairs of entries as 70
needed.
60

50

40
30

20
10

Swap? No. 0 [1]


[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [5][6]
The Bubble Sort Algorithm

• Continue
looping, until done.
70
60

50

40
30

20
10

Swap? Yes.0 [1]


[0] [2]
[1] [3]
[2] [4]
[3] [5]
[4] [5][6]
Bubble Sort
•Start – Unsorted

•Compare, swap (0, 1)

•Compare, swap (1, 2)

•Compare, no swap

•Compare, noswap

•Compare, swap (4, 5)

•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)

•12 in position, Pass 4

•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

Pass 2 i=1 array[1] > array[2] Than Swap


Array Data 4 3 5 2 1
Index 0 1 2 3 4

Pass 3 i=2 array[2] > array[3] Than Swap


Array Data 4 3 2 5 1
Index 0 1 2 3 4

Pass 4 i=3 array[3] > array[4] Than Swap


Array Data 4 3 2 1 5
Index 0 1 2 3 4
Now Array Data is as Below
Step by Step
Array Data 4 3 2 1 5 Execution of Bubble
Index 0 1 2 3 4 Sort Algorithm

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

Pass 2 i=1 array[1] > array[2] Than Swap


Array Data 3 2 4 1 5
Index 0 1 2 3 4

Pass 3 i=2 array[2] > array[3] Than Swap


Array Data 3 2 1 4 5
Index 0 1 2 3 4

Pass 4 i=3 array[3] > array[4] Than Swap


Array Data 3 2 1 4 5
Index 0 1 2 3 4
Now Array Data is as Below
Step by Step
Array Data 3 2 1 4 5 Execution of Bubble
Index 0 1 2 3 4 Sort Algorithm

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

Pass 2 i=1 array[1] > array[2] Than Swap


Array Data 2 1 3 4 5
Index 0 1 2 3 4

Pass 3 i=2 array[2] > array[3] Than Swap


Array Data 2 1 3 4 5
Index 0 1 2 3 4

Pass 4 i=3 array[3] > array[4] Than Swap


Array Data 2 1 3 4 5
Index 0 1 2 3 4
Now Array Data is as Below
Step by Step
Array Data 2 1 3 4 5 Execution of Bubble
Index 0 1 2 3 4 Sort Algorithm

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

Pass 2 i=1 array[1] > array[2] Than Swap


Array Data 1 2 3 4 5
Index 0 1 2 3 4

Pass 3 i=2 array[2] > array[3] Than Swap


Array Data 1 2 3 4 5
Index 0 1 2 3 4

Pass 4 i=3 array[3] > array[4] Than Swap


Array Data 1 2 3 4 5
Index 0 1 2 3 4
Bubble Sort – Improved
• for( k = 1 to n- 1)
•{
• for(i=0 to n – k - 1)
• {
• 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 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

Pass 2 i= 1 to 3 array[1] > array[2] Than Swap


Array Data 4 3 5 2 1
Index 0 1 2 3 4

Pass 3 i= 2 to 3 array[2] > array[3] Than Swap


Array Data 4 3 2 5 1
Index 0 1 2 3 4

Pass 4 i=3 to 3 array[3] > array[4] Than Swap


Array Data 4 3 2 1 5
Index 0 1 2 3 4
Now Array Data is as Below
Step by Step
Array Data 4 3 2 1 5 Execution of Bubble Sort
Index 0 1 2 3 4 Algorithm - Improved

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

Pass 2 i= 1 to 2 array[1] > array[2] Than Swap


Array Data 3 2 4 1 5
Index 0 1 2 3 4

Pass 3 i= 2 to 2 array[2] > array[3] Than Swap


Array Data 3 2 1 4 5
Index 0 1 2 3 4

Pass 4 will not take place


Now Array Data is as Below Step by Step
Execution of Bubble Sort
Array Data 3 2 1 4 5 Algorithm - Improved
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

Pass 2 i=1 to 1 array[1] > array[2] Than Swap


Array Data 2 1 3 4 5
Index 0 1 2 3 4

Pass 3 will not take place


Pass 4 will not take place
Now Array Data is as Below Step by Step
Execution of Bubble Sort
Algorithm - Improved
Array Data 2 1 3 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
•}

You might also like