Advertisement
defineSN

binary search

Oct 13th, 2012
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.13 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include <math.h>
  4. #include <time.h>
  5.  
  6. #include"merge_net.h"
  7.  
  8.  
  9. int populateArray (int *,int);// takes in the pointer to array, and size
  10. int printArray(int *,int);//takes in pointer to array and its size and prints the array
  11.  
  12.  
  13. int main()
  14. {
  15.     int *array,i,size,bool=0,search=0,mid=0,start=0,end=0;
  16.     do{
  17.         printf("enter the size of array:  ");
  18.         scanf("%d",&size);
  19.        
  20.         array=(int *)malloc(sizeof(int)*size);
  21.        
  22.         // printf("enter array elements: \n");
  23.  
  24.         // for(i=0;i<size;i++){
  25.             // scanf("%d",(array+i));
  26.         // }
  27.        
  28.         populateArray(array,size);
  29.         printArray(array,size);
  30.        
  31.         partition(array,0,(size-1));
  32.        
  33.         printf("\nthe array after sorting:\t");
  34.         for(i=0;i<size;i++){
  35.             printf("%d ",*(array+i));
  36.             }
  37.            
  38.         /*now that sorting is done, we can perform binary search*/ 
  39.        
  40.         printf("\n\nenter the value to search:  ");
  41.         scanf("%d",&search);
  42.        
  43.        
  44.  
  45.         start=0;        // there were problems without this initializtion, as it led to start>end,
  46.                         //and while loop getting bypassed, binary search fail, and "search item not found" being printed.
  47.        
  48.        
  49.         end=size-1;
  50.         mid=size/2;
  51.         while(start<=end){
  52.             if(*(array+mid)<search){
  53.                 start=mid+1;
  54.                 printf("\nstart, end, mid : %d  %d  %d \n",start,end,mid);
  55.                 }
  56.             else if(*(array+mid)==search){
  57.                 printf("\n%d found at array location: %d \n",search,mid);
  58.                 break;
  59.                 }
  60.             else if(*(array+mid)>search){
  61.                 printf("\nstart, end, mid : %d  %d  %d \n",start,end,mid);
  62.                 end=mid-1;
  63.                 }
  64.             mid=(start+end)/2; 
  65.             }      
  66.                
  67.         if(start>end){
  68.             printf("search item %d not found.\n\n",search);
  69.             }
  70.            
  71.         printf("to iterate once more, press 1, else 0 : ");
  72.         scanf("%d",&bool); 
  73.         }while(bool==1);   
  74.            
  75.     return 0;  
  76. }
  77.  
  78.  
  79.  
  80.  
  81.  
  82. int populateArray(int *array,int size){
  83.     int i;
  84.    
  85.     srand(time(NULL));
  86.     printf("populating the array....\n");
  87.    
  88.     for(i=0;i<size;i++) {
  89.         *(array+i)=rand()%1000; // populates the array with pseudo random numbers from 0-999
  90.     }
  91. }
  92.  
  93. int printArray(int *array,int size){
  94.     int i;
  95.    
  96.     printf("\nthe array is:\t");
  97.    
  98.     for(i=0;i<size;i++) {
  99.         printf("%d ",*(array+i));  
  100.     }
  101.     puts("\n");
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement