Write a program to arrange given N integers in ascending order using Bubble Sort.

#include<stdio.h>
#include<conio.h>
int main()
{
int n,i,j,a[10],temp;
clrscr();
printf("Enter the no. of elements : \n");
scanf("%d",&n);
printf("Enter the array elements \n");
for(i = 0 ; i < n ; i++)
scanf("%d",&a[i]);
printf("The original elements are \n");
for(i = 0 ; i < n ; i++)
printf("%d ",a[i]);
for(i= 0 ; i < n-1 ; i++) // Number of Passes
{
for(j= 0 ; j< n-i+1; j++) // Comparisons
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
printf("\n The Sorted elements are \n");
for(i = 0 ; i < n ; i++)
printf("%d ",a[i]);
getch();
}

Output:
Enter the no. of elements :
5
Enter the array elements
30 10 50 20 40
The original elements are
30 10 50 20 40
The Sorted elements are
10 20 30 40 50

Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: