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

Bubblesort-Algorithm

The document provides an overview of sorting algorithms, focusing on Bubble Sort, which arranges elements in a list in ascending or descending order. It explains how Bubble Sort works through multiple passes, comparing and swapping adjacent elements, and includes an optimized version that uses a flag to terminate early if no swaps are made. The document also outlines the time complexity of Bubble Sort, noting its best case as O(n) and worst case as O(n^2), along with its properties as a stable and in-place sorting algorithm.

Uploaded by

prakashchellam21
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Bubblesort-Algorithm

The document provides an overview of sorting algorithms, focusing on Bubble Sort, which arranges elements in a list in ascending or descending order. It explains how Bubble Sort works through multiple passes, comparing and swapping adjacent elements, and includes an optimized version that uses a flag to terminate early if no swaps are made. The document also outlines the time complexity of Bubble Sort, noting its best case as O(n) and worst case as O(n^2), along with its properties as a stable and in-place sorting algorithm.

Uploaded by

prakashchellam21
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

SORTING AND

SEARCHING
Sorting
 The process of arranging the elements in a list in either ascending
or
descending order.
 Types of Sorting:
 Bubble Sort
 Insertion Sort
 Selection Sort
 Merge Sort
 Quick sort
 Heap Sort
 Radix Sort
Bubble
Sorting
 Bubble sort is the easiest sorting algorithm to implement.
 It is inspired by observing the behaviour of air bubbles over
foam.
 It is an in-place sorting algorithm.
 It uses no auxiliary data structures (extra space) while sorting.
How Bubble Sort Works?

 Bubble sort uses multiple passes (scans) through an array.


 In each pass, bubble sort compares the adjacent elements of the array.
 It then swaps the two elements if they are in the wrong order.
 In each pass, bubble sort places the next largest element to its
proper position.
 In short, it bubbles down the largest element to its correct position.
Bubble Sort Algorithm-
▸ for(int pass=1 ; pass<=n-1 ; ++pass) // Making passes through array
▸ {for(int i=0 ; i<=n-2 ; ++i)
▸ {if(A[i] > A[i+1]) // If adjacent elements are in wrong order
▸ swap(i,i+1,A); // Swap them
▸ }}
▸ //swap function : Exchange elements from array A at position x,y
▸ void swap(int x, int y, int[] A)
▸ {int temp = A[x];
▸ A[x] = A[y];
▸ A[y] = temp;
▸ return ;
▸ }// pass : Variable to count the number of passes that are done till
now
▸ // n : Size of the array
▸ // i : Variable to traverse the array A
▸ // x,y : Indices
swap() of thetoarray
: Function swapthat
twoneeds to befrom the array
numbers
swapped
Bubble Sort
Example-
▸ Consider the following array A-Now, we shall implement the above bubble sort algorithm on this
array.

Step-01:
We have pass=1 and i=0.
We perform the comparison A[0] > A[1] and swaps if the
0th element is greater than the 1th element.
Since 6 > 2, so we swap the two elements.
Bubble Sort
▸ Step-02: Example-

▸ We have pass=1 and i=1.
▸ We perform the comparison A[1] > A[2] and swaps if the 1th element is
greater than the 2th element.
▸ Since 6 < 11, so no swapping is required.
Bubble Sort
Example-
▸ Step-03:

▸ We have pass=1 and i=2.
▸ We perform the comparison A[2] > A[3] and swaps if the 2nd element is
greater than the 3rd element.
▸ Since 11 > 7, so we swap the two elements.
Bubble Sort
Example-
▸ Step-04:

▸ We have pass=1 and i=3.
▸ We perform the comparison A[3] > A[4] and swaps if the 3rd element
is
greater than the 4th element.
▸ Since 11 > 5, so we swap the two elements.

Finally after the first pass, we see that the largest element 11 reaches its correct
position
Bubble Sort
Example-
▸ Step-05:
▸ Similarly after pass=2, element 7 reaches its correct
▸ The modified array after pass=2 is shown
position.
below-

▸ Step-06:
▸ Similarly after pass=3, element 6 reaches its
correct
position.
▸ The modified array after pass=3 is shown below-
Bubble Sort
▸ Step-07: Example-
▸ No further improvement is done in
pass=4.
▸ This is because at this point, elements 2 and 5 are already present at their correct
positions.
▸ The loop terminates after pass=4.
▸ Finally, the array after pass=4 is shown below-
Optimization Of Bubble Sort
Algorithm-
▸ If the array gets sorted after a few passes like one or two, then
ideally the algorithm should terminate.
▸ But still the above algorithm executes the remaining passes which
costsextra comparisons. for (int pass=1 ; pass<=n-1 ; ++pass)
{
flag=0 // flag denotes are there any swaps done in pass
for (int i=0 ; i<=n-2 ; ++i)
{
if(A[i] > A[i+1])
{
swap(i,i+1,A);
flag=1 // After swap, set flag to 1
}
}
if(flag == 0) break; // No swaps indicates we can terminate loop
}
void swap(int x, int y, int[] A)
{
int temp = A[x];
A[x] = A[y];
A[y] = temp;
return;
}
Optimization Of Bubble Sort
Algorithm-

▸ To avoid extra comparisons, we maintain a flag variable.


▸ The flag variable helps to break the outer loop of passes after
obtaining
the sorted array.
▸ The initial value of the flag variable is set to 0.
▸ The zero value of flag variable denotes that we have not
encountered any swaps.
▸ Once we need to swap adjacent values for correcting their wrong order,
the value of flag variable is set to 1.
▸ If we encounter a pass where flag == 0, then it is safe to break the
outer loop and declare the array is sorted.
Bubble Sort Algorithm- Time
Complexity

Bubble Sort Algorithm Time Complexity

Best Case O(n)

Average Case Θ(n2)

Worst Case O(n2)


Properties- Bubble Sort
Algorithm

▸ Some of the important properties of bubble sort algorithm


are-

▸▸ Bubble sort is a stable sorting algorithm.


Bubble sort is an in-place sorting algorithm.
▸ The worst case time complexity of bubble sort algorithm is O(n2).
▸ The space complexity of bubble sort algorithm is O(1).
▸ Number of swaps in bubble sort = Number of inversion pairs present
in
the given array.
▸ Bubble sort is beneficial when array elements are less and the array is
nearly sorted.
Thank You
Queries????

You might also like