Sort & Search Algorithm c code

bubble sort
#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>

int *data,n;
void Randvalue();
void Bubblesort();
void show();
void main()
{
       printf("Bubble sort\n");
       Randvalue();
       Bubblesort();
       puts("\nAfter sort");
       show();
       getch();
}

void Randvalue()
{
       printf("How many data:");
       scanf("%d",&n);
       data=new int [n];
       srand(time(0));
       printf("\nBefore sort\n");
       for(int i=0;i<n;i++)
       {
              data[i]=rand()%100+1;
              printf("%d\t",data[i]);
       }
       puts("\n");
}
void Bubblesort()
{ int d=n-1;
       int temp;
       for(int i=0;i<d;i++)
       {
              for(int j=0;j<d-i;j++)
              {
                     if(data[j]>data[j+1])
                     {
                           temp=data[j];
                           data[j]=data[j+1];
                           data[j+1]=temp;
                     }
                     show();
              }
             
       }
}
void show()
{
       for(int i=0;i<n;i++)
       {
              printf("%d\t",data[i]);
       }
       puts("\n");
}




insertion sort
#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>

int *data,n;

void Randvalue();
void Insetsort();
void show();

void main()
{     
       printf("Insertion sort\n");
       Randvalue();
       Insetsort();
       printf("\nAfter sort\n");
       for(int i=0;i<n;i++)
       {     
              printf("%d%\t",data[i]);
       }
       puts("\n");
       delete [] data;
       getch();
}
void Randvalue()
{
       printf("How many data:");
       scanf("%d",&n);
       data=new int [n];
       srand(time(0));
       printf("\nBefore sort\n");
       for(int i=0;i<n;i++)
       {
              data[i]=(rand()%1000)+1;  
              printf("%d%\t",data[i]);
       }
       puts("\n");
}
void Insetsort()
{
       int get,save;
       for(int i=1;i<n;i++)
       {
              get=data[i];
              int j=i-1;
              save=-1;
              while(j>=0)
              {
                     if(get<data[j])
                     {
                           save=j;
                           data[j+1]=data[j];
                           j--;
                     }
                     else
                           break;
              }
              if(save!=-1)
                     data[save]=get;
              show();
       }
}
void show()
{
for(int i=0;i<n;i++)
       {     
              printf("%d%\t",data[i]);
       }
puts("\n");
}


Quick sort
#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>
int *data,n;
void Randvalue();
void Reqick(int *f,int *r);
void Quicksort(int *data ,int *left,int *right);
void show();
void main()
{
       int *f,*r;
       Randvalue();
       f=&data[0];
       r=&data[n-1];
       Quicksort(data,f,r);
       printf("\nAfter sort\n"); 
       show();
       delete [] data;
getch();
}
void Randvalue()
{
       printf("How many data:");
       scanf("%d",&n);
       data=new int[n];
       srand(time(0));
       for(int i=0;i<n;i++)
       {
              data[i]=rand()%100+1;
       }
       printf("Befor sort\n");
       for(int i=0;i<n;i++)
       {
              printf("%d ",data[i]);
       }
}
void Quicksort(int *data ,int *left,int *right)
{
       int *f,*r,term=1,temp;
       f=left;
       r=right;
       while(f!=r)
       {
              if(*f>*r)
              {
                     temp=*f;
                     *f=*r;
                     *r=temp;
                     term++;
              }
              if(term%2==0)
              f++;
              else
              r--;
       }
       if(left<r)
              {
                     Quicksort(data,left,r-1);
              }

              if(f<right)
              {
                     Quicksort(data,f+1,right);
              }
}
void show()
{
       for(int i=0;i<n;i++)
       {
              printf("%d ",data[i]);
       }     
}



Heap sort
#include<stdio.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>
int data[100];
int n;
void show();
void heaptree(int i);
void heapsort();
void adddata(int i);
void buildtree();
void main()
{
       srand(time(0));
       printf("how many n:");
       scanf("%d",&n);
       heapsort();
       printf("After sort");
       show();
       getch();
}
void show()
{
       for(int i=1;i<=n;i++)
       {
              printf(" %d",data[i]);
       }
       printf("\n");
}
void heaptree(int i)
{
       int temp;
       while(i/2!=0)
       {
              if(data[i/2]<data[i])
              {
                     temp=data[i];
                     data[i]=data[i/2];
                     data[i/2]=temp;
              }
              i=i/2;
       }
}
void heapsort()
{
       int z=n;
       buildtree();
       while(z>0)
       {
              int temp;
              temp=data[1];
              data[1]=data[z];
              data[z]=temp;
              printf("heap sort");
              show();
              puts("");
              z--;
              int i=1;
              while(i<=z)
              {
                     heaptree(i);
                     i++;
              }     
              printf("Heap tree");
              show();
              puts("");

       }
}
void adddata(int i)
{
              data[i]=rand()%100+1;
}
void buildtree()
{
       int i=1;
       while(i<=n)
       {
                     adddata(i);
                     heaptree(i);
                     i++;
       }     
       printf(" Build Tree");
              show();
              puts("\n");
}




Radix sort
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
int bucket[10][10];
int *data,count=0,n,*save;
void check();
void Radixsort();
void showbucket();
void setbucket();
void Randvalue();
void main()
{
       Randvalue();
       check();
       Radixsort();
       puts("\n\nAfter sort");
       printf("data is ");
       for(int i=0;i<n;i++)
       {
              printf(" %d",data[i]);
       }
       getch();
}
void check()
{
       int mod=1,max;
       max=data[0];
       for(int i=0;i<n;i++)
       {
              if(data[i]>max)
              {
                     max=data[i];
              }
       }
       while(max/mod!=0)
       {
              mod=mod*10;
              count++;
       }
       puts("Before sort");
       printf("data is ");
       for(int i=0;i<n;i++)
       {
              printf(" %d",data[i]);
       }
}
void Radixsort()
{
       int mod=10,get;
       save=(int *)calloc(10,sizeof(int));
       for(int i=0;i<count;i++)
       {
              for(int j=0;j<n;j++)
              {
                     get=(data[j]%mod)/(mod/10);
                     switch(get)
                     {
                           case 0:bucket[0][save[0]++]=data[j];
                                         break;
                           case 1:bucket[1][save[1]++]=data[j];
                                         break;
                           case 2:bucket[2][save[2]++]=data[j];
                                         break;
                           case 3:bucket[3][save[3]++]=data[j];
                                         break;
                           case 4:bucket[4][save[4]++]=data[j];
                                         break;
                           case 5:bucket[5][save[5]++]=data[j];
                                         break;
                           case 6:bucket[6][save[6]++]=data[j];
                                         break;
                           case 7:bucket[7][save[7]++]=data[j];
                                         break;
                           case 8:bucket[8][save[8]++]=data[j];
                                         break;
                           case 9:bucket[9][save[9]++]=data[j];
                                         break;
                     }
              }
              printf("\n\n\t\t*****Roung[%d]*****\n\n",i+1);
              showbucket();
              mod=mod*10;
       }
}
void showbucket()
{
       int k=0;
       for(int i=0;i<10;i++)
       {
              printf("Bucket[%d]",i);
              for(int j=0;j<save[i];j++)
              {
                     printf(" %d",bucket[i][j]);
                           data[k]=bucket[i][j];k++;
                           bucket[i][j]=NULL;
              }
              puts("");
       }
       puts("---------------------------------------------------------");
       printf("data is ");
       for(int i=0;i<n;i++)
       {
              printf(" %d",data[i]);
       }
       delete [] save;
       save=(int *)calloc(10,sizeof(int));
}
void Randvalue()
{
       printf("How many data:");
       scanf("%d",&n);
       data=new int [n];
       srand(time(0));
       for(int i=0;i<n;i++)
       {
              data[i]=(rand()%1000)+1;  
       }
       puts("");
}



Binary search
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
int *data,n;
void Sort();
void Binarysearch();
void Randvalue();
void main()
{
       Randvalue();
       puts("");
       Sort();
       Binarysearch();
       getch();

}
void Sort()
{
       int temp;
       for(int i=0;i<n-1;i++)
       {
              for(int j=0;j<n-i-1;j++)
              {
                     if(data[j]>data[j+1])
                     {
                           temp=data[j];
                           data[j]=data[j+1];
                           data[j+1]=temp;
                     }
              }
       }
      
       for(int i=0;i<n;i++)
       {
              printf("%d\t",data[i]);
       }
}
void Binarysearch()
{
       int f,r,mid,key,x=0;
       printf("\nEnter Key:");
       scanf("%d",&key);
       f=0;
       r=n-1;
       do
       {
              mid=(r+f)/2;
              printf(" Address f=%d\tf=%d\n",f+1,data[f]);
              printf(" Address r=%d\tr=%d\n",r+1,data[r]);
              printf(" Address mid=%d\tmid=%d\n",mid+1,data[mid]);
              puts("");
              if(key==data[mid])
              {
                     printf("\nKey is %d\n",key);
                     printf("Adress is %X\n",mid+1);
                     printf("MemoryAdress is %X\n",&data[mid]);
                     x=1;
                     break;
              }
              if(data[mid]<key)
                     f=mid+1;
              else
                     r=mid-1;
       }while(f!=mid&&r!=mid);
       if(x==0)
              printf("Not found");
}
void Randvalue()
{
       printf("How many data:");
       scanf("%d",&n);
       data=new int [n];
       srand(time(0));
       printf("\nBefore sort\n");
       for(int i=0;i<n;i++)
       {
              data[i]=(rand()%1000)+1;  
              printf("%d%\t",data[i]);
       }
       puts("\n");
}




Binary search tree
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct node
{
       int data;
       struct node *left;
       struct node *right;
};
int i=0;
void show(struct node **walk);
void addnode(struct node **walk);
void Searchtree(struct node **walk,int key);
void main()
{
       struct node *start=NULL;
       int n,key;
       printf("How many data:");
       scanf("%d",&n);
       for(int i=0;i<n;i++)
       {
              addnode(&start);
              system("cls");
              printf("Tree\t");
              show(&start);
              printf("\n");
       }
       printf("--------------------------------------------------------------------------------\n");
       printf("Enter key:");
       scanf("%d",&key);
       Searchtree(&start,key);
       getch();
}
void addnode(struct node **walk)
{
       int temp;
       printf("\n");
       printf("Enter data[%d]:",i+1);
              scanf("%d",&temp);
       if(*walk==NULL)
       {
       *walk=new struct node;
       (*walk)->data=temp;
       (*walk)->left=NULL;
       (*walk)->right=NULL;
       }
       else
              while(*walk!=NULL)
              {
                     if((*walk)->data>temp)
                     {
                           walk=&(*walk)->left;
                     }
                     else
                           walk=&(*walk)->right;
              }
       *walk=new struct node;
       (*walk)->data=temp;
       (*walk)->left=NULL;
       (*walk)->right=NULL;
       i++;
}
void show(struct node **walk)
{
       if((*walk)->left!=NULL)
              show(&(*walk)->left);
       printf(" %d",(*walk)->data);
       if((*walk)->right!=NULL)
              show(&(*walk)->right);
}
void Searchtree(struct node **walk,int key)
{
       int x=0;
       while(*walk!=NULL)
       {
              if(key==(*walk)->data)
              {
                     printf("Adress key is %x\n",*walk);
                     x=1;
              }
              if(key<(*walk)->data)
                     walk=&(*walk)->left;
              else
                     walk=&(*walk)->right;
       }
       if(x==0)
              printf("Not found");
      
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น