All pastes #59350 Raw Edit

awan

public text v1 · immutable
#59350 ·published 2006-05-28 15:36 UTC
rendered paste body
/* Simple binary search algorithm */

#include <stdio.h>
#include <stdlib.h>

#define max 5

int main()
{
   int m[max]= {1,2,3,4,5}; // sorted array
   int low=0, high=max;
   
   int mid = 0;
   
   int ieskom = 3; // value we need to find
   int bb=0;
   
   while (!bb && high >= low)
     {
         mid = (low + high) / 2;
         if (ieskom == m[mid]) { printf("I found the value\n"); bb = 1; }
        if (ieskom > m[mid]) low = mid + 1;
       else high = mid - 1;
     }
     
   getchar();
   
}