Sunday 23 June 2013

Merge Sort C Code

Merge Sort

#include<stdio.h>
#include<conio.h>

int c[10],k;

void merge(int *a,int u,int l,int n)
{int i;
     k=u;
     int st=u;
     while(u<l && l<=n)
     {
                      if(a[u]<=a[l])
                      {
                          c[k]=a[u];        
                          u++;
                          k++;
                      }
                      else
                      {
                          c[k]=a[l];
                          k++;
                          l++;
     }
}
     while(u<l)
     {
                c[k]=a[u];        
                u++;
                k++;
     }
     while(l<=n)
     {
                c[k]=a[l];
                k++;
                l++;
     }
    i=st;
     while(i<=n)
     {
                a[i]=c[i];
                i++;
     }
}

void mergesort(int *a,int u,int l)
{
     if(u<l)
     {
     int i=(u+l)/2;
   
     mergesort(a,u,i-1);
     mergesort(a,i,l);
     merge(a,u,i+1,l);
     }
}

 int main()
{
      int a[50];
     
      FILE *f=fopen("hinput.txt","r");
      char c[5];
      int i=0;
      while(fscanf(f,"%s",c)!=EOF)
                                  a[i++]=atoi(c);
      int n=i-1;
      for(;i>=0;i--)
      printf("a[%d]=%d\n",i,a[i]);

      mergesort(a,0,n);


      for(i=0;i<=n;i++)
      printf("c[%d]=%d\n",i,c[i]);
   
         
      fclose(f);
      getch();
      return 0;
}

Quick Sort C Code

Quick Sort

#include<stdio.h>
#include<conio.h>

void swap(int *a,int *b)
{
     int temp;
     temp=*a;
     *a=*b;
     *b=temp;
}

int par(int *a,int p,int r)
{
    int i=p-1,j=p,pivot;
    pivot=r;
   
    while(j<pivot)
    {
                   if(a[j]<=a[pivot])
                   {
                                    swap(&a[j],&a[i+1]);
                                    i++;            
                                    j++;                      
                   }      
                   else
                   {
                                    j++;
                   }  
    }
    swap(&a[i+1],&a[pivot]);
   
    return i+1;
}

void quicksort(int *a,int p,int r)
{
     int q;
     if(p<r)
     {
           q=par(a,p,r);
           quicksort(a,p,q-1);
           quicksort(a,q+1,r);
     }
}

main()
{
      FILE *f=fopen("hinput.txt","r");
      char c[10];
      int a[10],i=0;
   
       while(fscanf(f,"%s",c)!=EOF)
      {
              a[i++]=atoi(c);              
      }
      int n=i-1;
   
     
       for(i-=1;i>=0;i--)
                        printf("a[%d]=%d\n",n-i,a[n-i]);
      quicksort(a,0,n);
      printf("\n#######\n Quick Sort\n#########\n");
      for(i=0;i<=n;i++)
                        printf("a[%d]=%d\n",i,a[i]);
     
      fclose(f);
      getch();
}

Heap Sort

Heap Sort


#include<stdio.h>
#include<conio.h>
#define MAX 50
int n;
void swap(int *a,int *b)
{
     int temp;
     temp=*a;
     *a=*b;
     *b=temp;
}

void max_heapify(int *a,int x,int n)
{
     int largest=x;
     if(2*x<=n)
              if(a[x]<a[2*x])
                             largest=2*x;
     if((2*x+1)<=n)
               if(a[largest]<a[2*x+1])
                             largest=2*x+1;
     if(x!=largest)
     {
          swap(&a[x],&a[largest]);
          max_heapify(a,largest,n);
     }
}

void build(int *a,int n)
{
     int i;
     for(i=n/2;i>=0;i--)
                        max_heapify(a,i,n);
}
 
void heap_sort(int *a,int n)
{
     int i;

     build(a,n);
     for(i=n;i>=2;i--)
     {
                      swap(&a[n],&a[0]);
                      n--;
                      max_heapify(a,0,n);
     }
}

main()
{
      int a[MAX];
      FILE *f=fopen("hinput.txt","r");
     
      char c[5];
      int i=0;
      printf("Input\n");
      while(fscanf(f,"%s",c)!=EOF)
      {
              a[i++]=atoi(c);              
      }
      n=i-1;

      for(i-=1;i>=0;i--)
                        printf("a[%d]=%d\n",n-i,a[n-i]);
      heap_sort(a,n);
      printf("\n#######\n Heap Sort\n#########\n");
      for(i=0;i<=n;i++)
                        printf("a[%d]=%d\n",i,a[i]);
     
      fclose(f);
      getch();
}