Thursday, March 3, 2011

write a program to sort the given array using selection sort


//implemention of selection sort
#include<stdio.h>
#include<conio.h>
void selsort(int[],int);
void main()
{  int ar[50],n,i;
   clrscr();
   printf("\n\n how many element do u want to enter...");
   scanf("%d",&n);
   printf("\n\n Enter Array elements:");
   for(i=0;i<n;i++)
        scanf("%d",&ar[i]);
   selsort(ar,n);
   printf("\n\n The sorted array is as show below...\n");
    for(i=0;i<n;i++)
           printf("%d",ar[i]);
     getch();
  }
   
   void selsort(int a[],int n)
    {
     int j,k,i,pos,small,temp;
     for(i=0;i<n;i++)
        {
         small=a[i];pos=i;
         for(j=i+1;j<n;j++)
                   {
                    if(a[j]<small)
                      {
                       small=a[j];pos=j}
                        }
                temp=a[i];
                a[i]=a[pos];
a[pos]=temp;
                printf("\n\n Array after every pass:");
               for(k=0;k<n;k++)
        printf("\t%d",a[k]);
             }

                   
      

No comments:

Post a Comment