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

Bubble Sort

Bubble sort is a simple sorting algorithm that compares adjacent elements and swaps them if they are out of order. It works by making multiple passes through an array, swapping adjacent elements that are out of order. Each pass through the array results in one more element being in its proper place until the array is fully sorted. Though simple to implement, bubble sort has a poor worst-case time complexity of O(n2), making it inefficient on large data sets.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Bubble Sort

Bubble sort is a simple sorting algorithm that compares adjacent elements and swaps them if they are out of order. It works by making multiple passes through an array, swapping adjacent elements that are out of order. Each pass through the array results in one more element being in its proper place until the array is fully sorted. Though simple to implement, bubble sort has a poor worst-case time complexity of O(n2), making it inefficient on large data sets.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Bubble sort

DAA
What is Bubble sort

 Bubble sort is a simple sorting algorithm.


 This sorting algorithm is comparison-based algorithm in which each pair of adjacent
elements is compared and the elements are swapped if they are not in order.
 This algorithm is not suitable for large data sets as its average and worst case complexity
are of Ο(n2) where n is the number of items.
How Bubble Sort Works?

 We take an unsorted array for our example. Bubble sort takes Ο(n 2) time so we're keeping
it short and precise.

 Bubble sort starts with very first two elements, comparing them to check which one is
greater.

 In this case, value 33 is greater than 14, so it is already in sorted locations. Next, we
compare 33 with 27.
How Bubble Sort Works?...

 We find that 27 is smaller than 33 and these two values must be swapped.

 The new array should look like this −

 Next we compare 33 and 35. We find that both are in already sorted positions.
How Bubble Sort Works?...

 Then we move to the next two values, 35 and 10.

 We know then that 10 is smaller 35. Hence they are not sorted.

 We swap these values. We find that we have reached the end of the array. After one
iteration, the array should look like this −
How Bubble Sort Works?...

 To be precise, we are now showing how an array should look like after each iteration. After
the second iteration, it should look like this −

 Notice that after each iteration, at least one value moves at the end.

 And when there's no swap required, bubble sorts learns that an array is completely sorted.
Algorithm

 We assume list is an array of n elements. We further assume that swap function swaps the


values of the given array elements.
Pseudo code
BUBBLE SORT( A, n)
{
For ( i=0 , i<n-1, i++)
{
For (j=0 , j<n-1-i , j++)
{
If A[ j ]> A[ j+1 ]
{
Temp = A [ j ]
A [ j ] = A [ j+1 ]
A [ j+1] = temp
}
}
}
}
Complexity Analysis of Bubble Sort:

Time Complexity
 Best O(n)
 Worst O(n2)
 Average O(n2)

Space Complexity O(1)

Stability Yes
Complexity in Detail
Cycle Number of Comparisons

1st (n-1)

2nd (n-2)

3rd (n-3)

....... ......

last 1

(n-1) + (n-2) + (n-3) +.....+ 1 = n(n-1)/2


nearly equals to n2

Hence, Complexity: O(n2)


Advantages of Bubble Sort:

 Bubble sort is easy to understand and implement.


 It does not require any additional memory space.
 It is a stable sorting algorithm, meaning that elements with the same key value maintain
their relative order in the sorted output.
Disadvantages of Bubble Sort:

 Bubble sort has a time complexity of O(N2) which makes it very slow for large data sets.
 Bubble sort is a comparison-based sorting algorithm, which means that it requires a
comparison operator to determine the relative order of elements in the input data set. It can
limit the efficiency of the algorithm in certain cases.

You might also like