#include <stdio.h>
      #include <stdlib.h>
      
      int binarySearch(int *A, int n, int key) {
      	int p = 0, q, r = (n-1);
      	while (p < r) {
      		q = (p + r) / 2;
      		if (key == *(A + q)) {
      			return q;
      		}
      		else if (key > *(A + q)) {
      			p = q + 1;
      		}
      		else {
      			r = q - 1;
      		}
      	}
      	return -1;
      }
      
      C language logo

      Binary Search

      Codiga's C Recipes

      Binary Search Implementation

      0 Comments

        Add Comment

        Log in to add a comment

        Codiga - All rights reserved 2022.