Pages

Related Posts with Thumbnails
Different Number Seaching Algorithoms & Comparisons – C Programming(Sequential,Binary,Interpollation) >> Codes-N-Tricks
Content feed Comments Feed

This C Program comapres among different searching algorithoms like

  1. Sequential(Unordered)
  2. Sequential(Ordered)
  3. Binary Search(Only for Ordered List)
  4. Interpollation Seach(Only For Ordered List)

This program doesn’t take any inputs.It takes random inputs and generates random numbers to be searched. And it outputs the time-complexity i.e number of comparisons of the search algorithoms.It also output a file in .xls format containning the data.

Code Snippet
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<time.h>
  4.  
  5. int seq_unord(int list[],int serch_el[],int no);
  6. int seq_ord(int ord_list[],int serch_el[],int no);
  7. int binary(int ord_list[],int serch_el[],int no);
  8. int intrpoll(int ord_list[],int serch_el[],int no);
  9.  
  10. int unord_cnt=0,ord_cnt=0,bin_cnt=0,intr_cnt=0;
  11.  
  12. int main()
  13. {
  14. int list[8192],serch_el[8192],ord_list[8192];
  15. int i=2,j,k,cnt=0,swap;
  16. FILE *fp;
  17. fp=fopen("data.xls","w");
  18. if(fp==NULL)
  19. {
  20.     printf("\nFile cannot be openned.\n");
  21.     exit(1);
  22. }
  23. fprintf(fp,"Number\tSeqUnord\tSeqOrd\tBinary\tIntrpoll");
  24.     for(j=0;j<8192;j++)
  25.     {
  26.      list[j]=rand()%10000;
  27.      ord_list[j]=list[j];
  28.     }
  29.     for(j=0;j<8192;j++)
  30.      serch_el[j]=rand()%10000;
  31. i=2;
  32. while(i<=8192)    
  33. {
  34.     srand(time(0));
  35.     for(j=0;j<i-1;j++)
  36.     {
  37.      for(k=j+1;k<i;k++)
  38.      {
  39.          if(ord_list[j]>ord_list[k])
  40.         {
  41.          swap=ord_list[j];
  42.          ord_list[j]=ord_list[k];
  43.          ord_list[k]=swap;
  44.         }
  45.      }
  46.     }
  47.     fprintf(fp,"\n%d\t%d\t%d\t%d\t%d",i,(seq_unord(list,serch_el,i)/i),(seq_ord(ord_list,serch_el,i)/i),(binary(ord_list,serch_el,i)/i),(intrpoll(ord_list,serch_el,i)/i));
  48.     printf("\ni=%d,Total Searches--- Unorder=%d,Order=%d,Binary=%d,Interpollation=%d",i,unord_cnt,ord_cnt,bin_cnt,intr_cnt);
  49.     i=i*2;
  50. }
  51. printf("\nYour data file has been processed and named 'data.xls'.\n");
  52. return 0;
  53. }
  54.  
  55. int seq_unord(int list[],int serch_el[],int no)
  56. {
  57. int i,j,cnt=0;
  58. unord_cnt=0;
  59. for(i=0;i<no;i++)
  60. {
  61.     for(j=0;j<no;j++)
  62.       {
  63.      cnt++;
  64.      if(serch_el[i]==list[j])
  65.      {
  66.         unord_cnt++;
  67.         break;
  68.      }
  69.     }
  70. }
  71. return(cnt);
  72. }
  73.  
  74. int seq_ord(int ord_list[],int serch_el[],int no)
  75. {
  76. int i,j,cnt=0;
  77. ord_cnt=0;
  78. for(i=0;i<no;i++)
  79. {
  80.     for(j=0;j<no;j++)
  81.     {
  82.      cnt++;
  83.      if(serch_el[i]<=ord_list[j])
  84.      {
  85.         if(serch_el[i]==ord_list[j]) ord_cnt++;
  86.         break;
  87.      }
  88.     }
  89. }
  90. return(cnt);
  91. }
  92.  
  93. int binary(int ord_list[],int serch_el[],int no)
  94. {
  95. int i,first,last,mid,cnt=0;
  96. bin_cnt=0;
  97. for(i=0;i<no;i++)
  98. {
  99.     first=0;last=no-1;
  100.     while(first<=last)
  101.     {
  102.      mid=(first+last)/2;
  103.      cnt++;
  104.      if(serch_el[i]==ord_list[mid])
  105.      {
  106.         bin_cnt++;
  107.         break;
  108.      }
  109.      else if(serch_el[i]<ord_list[mid]) last=mid-1;
  110.           else first=mid+1;
  111.     }
  112. }
  113. return (cnt);
  114. }
  115.  
  116. int intrpoll(int ord_list[],int serch_el[],int no)
  117. {
  118. int i,first,last,mid,cnt=0,dn;
  119. intr_cnt=0;
  120. for(i=0;i<no;i++)
  121. {
  122.     first=0;
  123.     last=no-1;
  124.      while((serch_el[i]>=ord_list[first] && serch_el[i]<=ord_list[last]) && first<=last)
  125.      {
  126.       cnt++;
  127.       dn=ord_list[last]-ord_list[first];
  128.       if(dn==0)
  129.        mid=first;
  130.       else
  131.        mid=first+((serch_el[i]-ord_list[first])*(last-first)/dn);
  132.       if (serch_el[i]==ord_list[mid])
  133.       {
  134.         intr_cnt++;
  135.         break;
  136.       }
  137.       else if(serch_el[i]<ord_list[mid]) last=mid-1;
  138.            else first=mid+1;
  139.      }
  140. }
  141. return (cnt);
  142. }

For any Information we can contact me.Further detail discussion on this will be carried out later.

Any Suggestion is highly expected.

0 Responses to Different Number Seaching Algorithoms & Comparisons – C Programming(Sequential,Binary,Interpollation)

Post a Comment

SyntaxHighlighter

E-Mail Subscription

Enter your email address:

Subscribe & get Regular E-Mail Updates From Codes-N-Tricks.
Delivered by FeedBurner

Share with Orkut

Loading

Recent Posts

About Us