# Bubble Sorting

### What is Bubble sorting in C Programming language?

• The bubble is the swapping of the adjacent element until the array is sorted in an order
• This sort is one of the simplest and the most popular sorting method
• The basic idea behind bubble sort is as a bubble rises up in the water, the smallest element goes to the beginning
• This method is based on successive selecting the smallest element through the exchange of adjacent element
• (First pass of bubble sort) Let n be the number of elements in the array a[]. The first pass begins with the comparison of a[n-1] and a[n-2]
• If a[n-2] is larger than a[n-1],the two elements are exchanged
• The smaller elements, now at a[n-2] is compared with a[n-3] and if necessary the elements are exchanged to place the smaller ones in a[n-3]
• Comparison progresses backward and after the last comparison of A[1] and A[0] and possible exchange the smallest element will be placed at a[0]
• As a variation, the first pass could begin the comparison with a[0] and a[1]
• If a[0] is larger than a[1], the two elements are exchanged
• The larger one will be placed in a[1] after the first comparison
• Comparison can work forward and after the last comparison can work forward and after the last comparison of a[n-2] and a[n-1], the larger element will be placed in a[n-1]

Algorithm of BUBBLE SORT

main() function

• Step 1: START
• Step 2:PRINT “ENTER THE NUMBER OF ARRAY ELEMENTS”
• Step 3:INPUT n
• Step 4:i=0
• Step 5:if i>n-1 THEN go to step 10
• Step 6:PRINT “Enter a value”
• Step 7:INPUT a[i]
• Step 8:i=0
• Step 10:IF i<n-1,Then
• Step 11: j=0
• Step 12:IF j<n-1,
• Step 13:IF a[j]>a[j+1] Then temp=a[j], a[j]=a[j+1], a[j+1]=temp
• Step 14:j=j+1
• Step 15:i=i+1
• Step 16:PRINT “After sorting ”
• Step 17:i=0
• Step 18:IF i>n THEN, Stop
• Step 19:PRINT a[i]
• Step 20:STOP

#### 1.Display bubble sorting of the given array without using function

``````#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,a[20],temp;
printf("enter the number of array elements: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("After sorting\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
}
OUTPUT:
enter the number of array elements:5
1
3
5
4
2
After sorting
1  2  3  4  5``````

First pass of the bubble sort with comparison in the forward direction

• The second pass is an exact replica of the first pass except that this time, the pass ends with the comparison and possible exchange of a[n-3] and a[n-2]
• After the end of the second pass, the second largest element will be placed at a[n-2]
• Bubble sort requires a total of n-1 passes.If n-1 elements are arranged(one element each pass) in ascending order from a[1] to a[n-1],smallest element will finally left at a[0]
• And this process continues until the element come in the ascending order

#### Algorithm of bubble sort using the function

main() function

• Step 1: START
• Step 2:PRINT “Enter value of n”
• Step 3:INPUT n
• Step 4:i=0
• Step 5:if i>n-1 Then goto step 10
• Step 6:PRINT “Enter a value”
• Step 7:INPUT a[i]
• Step 8:i=i+1
• Step 9:GOTO step 5
• Step 10:Call ascend(arguments:a,n)
• Step 11: Stop
• ascend(parameters:a[],n)
• Step 1:i=0
• Step 2:IF i>n-2 THEN goto Step 10
• Step 3:j=0
• Step 4:IF j>n-2,THEN goto Step 8
• Step 5:IF a[j]>a[j+1] THEN temp=a[j], a[j]=a[j+1], a[j+1]=temp
• Step 6:j=j+1
• Step 7:goto step 4
• Step 8:i=i+1
• Step 9:goto step 2
• Step 10:PRINT “Sorted numbers”
• Step 11:i=0
• Step 12:if i>n-1 Then goto Step 16
• Step 13:PRINT a[i]
• Step 14:i=i+1
• Step 15:goto Step 12
• Step 16:STOP

#### 2.Displaying bubble sort using the function

``````#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,a[100];
void ascend(int a[],int n);
clrscr();
printf("Enter the number of elements:");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
printf("Enter a value:");
scanf("%d",&a[i]);
}
ascend(a,n);
getch();
}
void ascend(int a[],int n)
{
int i,j,temp;
for(i=0;i<=n-2;i++)
{
for(j=0;j<=n-2;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("After sorting\n");
for(i=0;i<=n-1;i++)
{
printf("%d\n",a[i]);
}
}

OUTPUT:
Enter the number of elements:5
Enter the value:1
Enter the value:8
Enter the value:4
Enter the value:3
Enter the value:2

After sorting
1
2
3
4
8``````
Translate »