C Program for Bubble Sorting - Sort an array


Algorithm:-

1.Start
2.Accept the array size 'n'
3.Accept the elements of the array by a for loop
4.Set i=0
5.Check whether i<n-1 if yes goto step 6 else step 11
6.Set j=i+1
7.If j<n foto step 8 else step 10
8.If a[i]>a[j]
   t=a[j]
   a[j]=a[i]
   a[i]=t
9.j=j+1 goto step 7
10.i=i+1 goto step 5
11.Print the array using a for loop

Program:-

/*Program to find Bubble Sort - Crazy Factzz */
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[20],i,j,n,c,t;
printf("\n Enter the limit : ");
scanf("%d",&n);
printf("Enter the elements \n");
for (i=0;i<n-1;i++)
scanf("%d",&a[i]);
for (i=0;i<n-1;i++)
{
for (j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}}}
printf("\n The sorted array is \n");
for(i=0;i<n;i++)
printf("%d \t ",a[i]);
getch();
}

For more C programs - click here

Popular posts from this blog

8051 Assembly Program Code for Sorting in Descending Order - Keil - AT89C51

8051 Assembly Program Code for Sorting in Ascending Order - Keil -AT89C51