Program for Bubble Sort elements in Ascending Order
Definition:- In Bubble sort each element compares with the other elements until finds its correct order place. This is repeated until all elements are in the order.
//Header Files
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, j, k, flag, limit, arr[20]; //Variables
clrscr(); //to clear screen
// User to input the limit of the array
printf("\n Enter the limit:-");
scanf("%d",&n); // To scan the limit of the array
//Asking user to input the elements of the array
printf("\n Enter the element of Array to sort:->\n");
for(i=0;i<n;i++)
scanf("%d", &arr[i]); //Scanning the elements in the array
limit=n-1; // Setting the limit to execute the array
flag =1;
for(i=0; i<n;i++)
{
for(j=0;j<limit-i;j++)
{
if(arr[j]>arr[j+1]) //Comparing array elements
{ //Swapping the elements
k=arr[j];
arr[j]= arr[j+1];
arr[j+1]=k;
flag=0;
}
}
if (flag==1)
break;
else
flag=1;
}
printf("\n Element in ascending order with Bubble sort are:-");
for(i=0;i<n;i++)
{
printf("\n%d",arr[i]); //Printing the Sorted array
}
getch();
}
Output:-
Enter the limit:-5
Enter the element of Array to sort:->
9
7
4
6
0
Element in ascending order with Bubble sort are:-
0
4
6
7
9
0 Comment(s)