Monday, 20 May 2013

Bubble Sort


#include<stdio.h>
#include<conio.h>
void main()
{
int a[50];
int i,n,j,k,x,temp;
clrscr();
printf("Enter the number of elements you want to enter");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("a[%d] =",i);
scanf(" %d",&a[i]);
}

for(j=0;j<n-1;j++)
 {
 for(k=0;k<n-j-1;k++)
  {
   if(a[k]>a[k+1])
    {
     temp=a[k];
     a[k]=a[k+1];
     a[k+1]=temp;
    }
   }
  }

 printf("\n\nSORTED ARRAY IS:\n\n");
 for(x=0;x<n;x++)
 {
 printf("\na[%d] = %d",x,a[x]);
 }
getch();
}



Selection Sort


#include<stdio.h>
#include<conio.h>
void main()
{
int a[50];
int i,n,j,k,x,temp;
printf("Enter the number of elements you want to enter");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("a[%d] =",i);
scanf(" %d",&a[i]);
}

for(j=0;j<n-1;j++)
 {
 for(k=j+1;k<n;k++)
  {
  if(a[j]>a[k])
   {
   temp=a[j];
   a[j]=a[k];
   a[k]=temp;
   }
  }
 }
 printf("\n\nSORTED ARRAY IS:\n\n");
 for(x=0;x<n;x++)
 {
 printf("\na[%d] = %d",x,a[x]);
 }
getch();
}

Insert an element at desired position in array



#include<stdio.h>
#include<conio.h>
void main()
{
 clrscr();
 int a[10]={22,33,44,55,77,88};
 int i,j,k,n,t,x,z=0;
 printf("The elements of array are");
 for(i=0;i<=5;i++)
 {
   printf("\na[%d]= %d",i,a[i]);
 }
 printf("\nenter the location where you want to insert the element in the array");
 scanf("%d",&k);

 for(j=i-1;j>=k;j--)
  {
   a[j+1]=a[j];
   printf("\nNow a[%d] contains %d",j+1,a[j]);
  }
 printf("\nNow a[%d] contains %d",j+1,z);

 for(j=k-1;j>=0;j--)
 {
  printf("\nNow a[%d] contains %d",j,a[j]);
 }
 printf("enter the element to be inserted into the array");
 scanf("%d",&n);
 a[k]=n;
 t=i+1;
 printf("\n\nNew array after insertion is\n\n");
 for(i=0;i<t;i++)
  {
  printf("\nNow a[%d] contains %d",i,a[i]);
 }
 getch();
}





full details in json
Proudly Powered by Blogger.
back to top