Friday 27 April 2018

Program To Perform Bubble Sort Using C Language

Program :-

#include<stdio.h>
int A[100];  //global variable
void bblsrt(int n)  //Entering the function to perform bubble sort
{
int i=0;char ch;
int intrchng=0,pass=0;
while(pass<n-1 && intrchng==0)  //Passing starts
{
i=0;intrchng=1;
while(i<n-(pass+1))
{
if(A[i]>A[i+1])  //Swapping
{
int t=A[i];
A[i]=A[i+1];
A[i+1]=t;
intrchng=0;
}
i++;  //Incrementing i
}
if(intrchng==1)  //Return
return;
pass++;  //Incrementing pass
}
}
void main()  //Entering main function
{
int n,i;
printf("Enter the array size : ");  //Reading array size
scanf("%d",&n);
printf("Enter the array elements :"); //Reading array elements
for(i=0;i<n;++i)
scanf("%d",&A[i]);
bblsrt(n);  //Calling function to perform Bubble sort
printf("The sorted array is :");  //Printing sorted elements
for(i=0;i<n;++i)
printf("%d\t ",A[i]);
}



Output :-

Eg 1:-
Enter the array size : 5
Enter the array elements  : 32 45 18 25 56
The sorted array is : 18 25 32 45 56




No comments:

Post a Comment

How to Invert an SVG image using CSS ?

You can invert your svg easily by using following css code svg { -webkit-filter: invert(100%); /* safari 6.0 - 9.0 */ filter...