Bubble Sort: Presented By:-012: - Shubham Koshti 042: - Het Shah 047: - Ziyan Shaikh

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Bubble Sort

Presented By :-
012 :- Shubham Koshti
042 :- Het Shah
047 :- Ziyan Shaikh
What is Sorting

● A Sorting Algorithm is used to rearrange a given


array or list elements according to a comparison
operator on the elements.
● The comparison operator is used to decide the new
order of element in the respective data structure.
Types of Sorting

● Bubble Sort
● Selection Sort
● Insertion Sort
● Merge Sort
● Quick Sort
● Heap Sort
Bubble Sort

● Bubble Sort is Simple Sorting Algorithm.


● In Bubble sort, Each element of the array is
compared with its adjacent element.
● A list with n elements requires n-1 passes for sorting.
● It is easy because it deals with only two element at a
time.
● Bubble sort is little slower compared to other sorting
techniques.
Example :- Step 1
Step 2
Step 3
Step 4
Algorithm

Bubble_sort(A[],N)

Step 1: Repeat Step 2 For i = 0 to N-1

Step 2: Repeat For J = 0 to N- I-1

Step 3: IF A[J] > A[J+1] THEN

TEMP=A[J]

A[J]=A[J+1]

A[J+1]=TEMP

END IF

END FOR

END FOR

Step 4: EXIT
Program
#include<stdio.h> for(i=0;i<n-1;i++) printf(“\n\n”);
Int main() {
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
{
{
{
Int n;
if(a[j]>a[j+1]) printf(“\n a[%d] : %d”,i,a[i]);
printf(“Enter Size of Array : ”);
{
}
scanf(“%d”,&n);
int temp;
Int a[n],i,j; }
temp=a[j];
for(i=0;i<n;i++)
a[j]=a[j+1];
{ a[j+1]=temp;

printf(“Enter a[%d] : “,i); }

scanf(“%d”,&a[i]); }

} }
Real Life Example

● Imagine there are five cars all travelling down a straight road.
They are all being driven on cruise control, but each of the cars’
speeds have been set to slightly different values.
● When a car is travelling faster than the car in front, it will
overtake it, and occupy the slower car’s position in the traffic.
● This will keep happening, with each car switching positions with
any slower car in front of it.
● Eventually the cars will sort themselves according to their
speeds, with the fastest car being at the front of the queue of
traffic.

You might also like