Advertisement
lIlIlIlIIlI

Algorithms_BinarySearch

Sep 9th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.55 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int BinarySearch(int* arr, int num, int KEY){
  5.     int S = num / 2;
  6.     while(1){
  7.         if(KEY > arr[S] )   S = (S + num) / 2;
  8.         else if(KEY < arr[S] )  S /= 2;
  9.         else    return S;
  10.     }
  11. }
  12.  
  13. /*
  14. int Search(int* arr, int number, int KEY){
  15.     int i = 0;
  16.     while(i < number){
  17.         if(arr[i] == KEY)   return i;
  18.         i++;
  19.     }
  20.     return -1;
  21. }
  22. */
  23.  
  24. int main(){
  25.     int arr[10] = {1, 10, 24, 36, 47, 50, 55, 61, 73, 89};
  26.     printf("%d", BinarySearch(arr, 10, 36) + 1 );
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement