Play and learn 300 000+ tabs online

Wednesday, August 11, 2010

Sorting Program in C

/* sort.c */
/* Author    : Mr. Jake Rodriguez Pomperada,MAED-IT */
/* Date      :  March 19, 2009 Thursday */
/* Language  : C  */
/* Tool      :  Turbo C 2.0 */
/* Email     :  jakerpomperada@yahoo.com */
 
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
 
 
 main()
{
 int a[10],n=0,choice=0;
 int read_array(int []);
 
 while(1)
 {
   clrscr();
   printf("\t  ============================ ");
   printf("\n\t ======== MAIN MENU ========= ");
    printf("\n\t ============================ ");
   printf("\n\n\t 1> BUBBLE SORT");
   printf("\n\n\t 2> SELECTION SORT");
   printf("\n\n\t 3> INSERTION SORT");
   printf("\n\n\t 4> BUCKET SORT");
   printf("\n\n\t 5> SHELL SORT");
   printf("\n\n\t 6> EXIT");
   printf("\n\n\t ENTER YOUR CHOICE :=> ");
   scanf("%d",&choice);
 
   switch(choice)
   {
    case 1:n=read_array(a);
           printf("\n\n\t THE ARRAY ELEMENTS ARE :: ");
           print_array(a,n);
           bubble_sort(a,n);
           printf("\n\n\t THE SORTED LIST IS :: ");
           print_array(a,n);
           break;
    case 2:n=read_array(a);
           printf("\n\n\t THE ARRAY ELEMENTS ARE :: ");
           print_array(a,n);
           select_sort(a,n);
           printf("\n\n\t THE SORTED LIST IS :: ");
           print_array(a,n);
           break;
    case 3:n=read_array(a);
           printf("\n\n\t THE ARRAY ELEMENTS ARE :: ");
           print_array(a,n);
           insert_sort(a,n);
           printf("\n\n\t THE SORTED LIST IS :: ");
           print_array(a,n);
           break;
    case 4:n=  read_array(a);
           printf("\n\n\t THE ARRAY ELEMENTS ARE :: ");
           print_array(a,n);
           bucket_sort(a,n);
           printf("\n\n\t THE SORTED LIST IS :: ");
           print_array(a,n);
           break;
    case 5:n=  read_array(a);
           printf("\n\n\t THE ARRAY ELEMENTS ARE :: ");
           print_array(a,n);
           shell_sort(a,n);
           printf("\n\n\t THE SORTED LIST IS :: ");
           print_array(a,n);
           break;
    case 6: printf("\n\n\t\t THANK YOU FOR USING THIS SOFTWARE");
        printf("\n\n\t       Created By: Mr. Jake R. Pomperada, MAED-IT");
        exit(0);
   }
getche();
 }
}
 
int read_array(int a[])
{
 int n,i;
 printf("\n\n\t ENTER THE ARRAY LENGTH :: ");
 scanf("%d",&n);
 for(i=0;i<n;i++)
  {
   printf("\n\n\t ENTER THE ELEMENT [%d] :: ",i);
   scanf("%d",&a[i]);
  }
 return(n);
}
 
print_array(int a[],int n)
{
 int i;
 for(i=0;i<n;i++)
  printf("%d \t",a[i]);
}
 
 bubble_sort(int a[],int n)
{
 int i,j,temp;
 for(i=0;i<n-1;i++)
   {
    for(j=0;j<n-1;j++)
     if(a[j]>a[j+1])
      {
       temp=a[j];
       a[j]=a[j+1];
       a[j+1]=temp;
      }
    printf("\n\n\t PASS %d :: ",i+1);
    print_array(a,n);
    }
}
 
select_sort(int a[],int n)
{
 int i,j,temp;
 for(i=0;i<n-1;i++)
   {
    for(j=i+1;j<n;j++)
      {
       if(a[i]>a[j])
          {
           temp=a[i];
           a[i]=a[j];
           a[j]=temp;
          }
       }
    printf("\n\n\t PASS %d :: ",i+1);
    print_array(a,n);
   }
}
 
insert_sort(int a[],int n)
{
 int i,j,temp;
 for(i=1;i<n;i++)
  {
   temp=a[i];
   for(j=i-1;temp<a[j] && j>=0;j--)
     a[j+1]=a[j];
   a[j+1]=temp;
   printf("\n\n\t PASS %d :: ",i);
   print_array(a,n);
  }
}
 
 
 shell_sort(int a[],int n)
{
 int i,j,k,d,t,x,flag=0;
 int p=1;
 for(d=n/2;d>=1;d=d/2)
  {
   flag=0;
   for(j=0;j+d<n;j++)
    {
      for(k=j;k>=0;k=k-d)
      {
         if(a[k]>a[k+d])
         {
          t=a[k];
          a[k]=a[k+d];
          a[k+d]=t;
          flag=1;
         }
      }
    }
    if(flag==1)
    {
      printf("\n\n\t PASS %d ::",p++);
      for(x=0;x<n;x++)
          {
            printf("\t %d",a[x]);
          }
    }
 
  }
}
 
 bucket_sort(int a[],int n)
{
 
 int b[10][20],i,j,row,col,no,x,p=1;
 for(i=1;i<=5;i++)
 {
   initzerocol(b);
   for(j=0;j<n;j++)
    {
      row=returndigit(a[j],i);
      b[row][0]=b[row][0]+1;
      col= b[row][0];
      b[row][col]=a[j];
    }
 
   merge(a,b);
   printf("\n\n\t PASS %d ::",p++);
   for(x=0;x<n;x++)
     printf("\t %d",a[x]);
  }
}
 
 initzerocol(int b[][20])
{
 int i;
 for(i=0;i<10;i++)
  b[i][0]=0;
}
 
int returndigit(int no,int i)
{
 int k;
 for(k=1;k<i;k++)
  no=no/10;
 return(no%10);
}
 
 merge(int a[],int b[][20])
{
 int i,j,k=0;
 for(i=0;i<10;i++)
   for(j=1;j<=b[i][0];j++)
    {
      a[k]=b[i][j];
      k=k+1;
    }
 }
 /* End of Code */

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.