Google Search

Sunday, January 29, 2012

C PROGRAM TO SORT ARRAY USING BUBBLE SORT


ALGORITHM TO SORT ARRAY USING BUBBLE SORT

1.  Repeat steps 2 & 3 for k = 1 to N-1
2.  Set ptr =1 
3.  Repeat while ptr <= N-k
4.  (a)  If data[ptr] > data[ptr + 1],then
            Interchange d ata[ptr] and data[ptr + 1]
     (b)  ptr = ptr + 1
5.  Exit


bubble sort


#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],n,i,j,temp;
clrscr();
printf("How many elements");
scanf("%d",&n);
printf("Enter the element of array");
for(i=0;i<=n-1;i++)
 {
  scanf("%d",&a[i]);
 }
for(i=0;i<=n-1;i++)
{
  for(j=0;j<=n-1-i;j++)
     {
      if(a[j]>a[j+1])
 {
  temp=a[j];
  a[j]=a[j+1];
  a[j+1]=temp;
 }
      }
}
printf("Element of array after the sorting are:\n");
for(i=0;i<=n-1;i++)
{
printf("%d\n",a[i]);
}
getch();

0 comments:

Post a Comment