Tuesday, April 8, 2014

C Program to sort n number of integers by using bubble sort technique

C Program to sort n number of integers by using bubble sort technique 

#include<stdio.h>
#include<conio.h>
void swap(int[],int,int);
void dis(int[],int);
void main ()
{
int a[100];
int n,i,temp,j;
clrscr();
printf("Enter how many numbers do you want");
scanf("%d",&n);
printf("Enter the %d numbers",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Unsorted values\n");
dis(a,n);
for (i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
  {
if(a[i]<a[j])
    {
swap(a,i,j);
    }
    }
}
printf("Sorted values are as follow\n");
dis(a,n);
getch();
}

void dis (int b[],int n)
{
int i;
for(i=0;i<n;i++)
printf("%d\n",b[i]);
}
void swap(int b[],int k,int l)
{
int temp;
temp=b[k];
b[k]=b[l];
b[l]=temp;
}

No comments:

Post a Comment