0% found this document useful (0 votes)
170 views14 pages

Sorting Algorithm: Bubble Sort

Bubble sort is an algorithm that sorts elements in ascending or descending order by repeatedly comparing adjacent pairs of elements and swapping them if they are in the wrong order. It requires n-1 passes to fully sort an array of n elements, where in each pass adjacent elements are compared and swapped into position from left to right. The algorithm runs in O(n2) time complexity as the number of comparisons decreases by one each pass.

Uploaded by

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

Sorting Algorithm: Bubble Sort

Bubble sort is an algorithm that sorts elements in ascending or descending order by repeatedly comparing adjacent pairs of elements and swapping them if they are in the wrong order. It requires n-1 passes to fully sort an array of n elements, where in each pass adjacent elements are compared and swapped into position from left to right. The algorithm runs in O(n2) time complexity as the number of comparisons decreases by one each pass.

Uploaded by

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

Sorting Algorithm

Bubble Sort
Bubble Sort
In this technique we sort the given elements
in ascending and descending order by
comparing two adjacent elements at a time
and placing them in correct position.
If we have n elements then it requires (n-1)
pass to sort.
Example
Consider an array arr having 5 elements
5 4 3 1 2

Arrange the elements in ascending order

We have 5 elements so we need max. 4 pass to sort the


elements
Pass 1 of 4
5>4 5>3 5>1 5>2
Given array swap swap swap swap
5 5 4 4 4 4

4 4 5 3 3 3

3 3 3 5 1 1

1 1 1 1 5 2

2 2 2 2 2 5

At the end of Pass 1


5 is at correct position
Compare two adjacent elements and arrange them in ascending order
Pass 2 of 4
4>3 4>1 4>2
From Pass 1 swap swap swap
4 4 3 3 3

3 3 4 1 1

1 1 1 4 2

2 2 2 2 4

5 5 5 5 5

At the end of Pass 2


4 & 5 are at correct position
Compare two adjacent elements and arrange them in ascending order
Pass 3 of 4
3>1 3>2
From Pass 2 swap swap
3 3 1 1

1 1 3 2

2 2 2 3

4 4 4 4

5 5 5 5

Compare two adjacent elements and arrange them in ascending order


Pass 4 of 4
From Pass 3
1 1
2 2

3
All the elements are sorted in ascending order
3
at the end of Pass 4
4 4

5 5

Compare two adjacent elements and arrange them in ascending order


About Bubble Sort
• Bubble sort requires n-1 pass to sort an array of n
elements.
• In each pass every adjacent elements a[i] and a[i+1] is
compared and if they are not in order then they are
swapped.
• In each pass we have n-k comparisons, where n is the
number of elements and k is the pass number.
• So, 1st pass requires n-1 comparisons and kth pass
requires n-k comparisons and the last pass requires 1
comparison.
Algorithm
/*a[0:n-1] is an array of n elements, temp is the variable to facilitate exchange
*/
BubbleSort(a,n)
Begin
for k=1 to n-1 by 1 do
for j=0 to n-k-1 by 1 do
if a[j]>a[j+1] then
Set temp=a[j];
Set a[j]=a[j+1];
Set a[j+1]=temp;
endif
endfor
Order of Bubble Sort
For n elements array,
In 1st pass we have n-1 comparisons
In 2nd pass we have n-2 comparisons
Similarly, in 𝑘 𝑡ℎ pass we have n-k comparisons
And the last pass requires 1 comparison
Therefore, total comparisons are
F(n) = (n-1)+(n-2)+(n-3)+….+3+2+1
𝑛(𝑛−1)
= =0(𝑛2 )
2
Write a program in C to enter 5
elements and arrange them in
ascending order
#include <stdio.h>

//function declaration
void bubbleSort(int *a, int n);
int main(){
//variable declaration
int arr[5], i;
//input
for(i = 0; i < 5; i++)
scanf("%d", &arr[i]);
//sort
bubbleSort(arr,5); //sending arr address and no. of elements

//output
for(i = 0; i < 5; i++)
printf("%d\n", arr[i]);

return 0;
}
//function definition
void bubbleSort(int *a, int n){
int k, j, temp;
for(k = 1; k <= n-1; k++){
for(j = 0; j <= n-k-1; j++){
if(a[j] > a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}

You might also like