C语言Matrix编程题——[Arrays]D. Liang 6.14 Binary Search

[Arrays]D. Liang 6.14 Binary Search

Description:

Implement the following function:

/* 
Elements in array are in decreasing order.
size is the size of array. 
If key found, return the index, else return -1 
*/
int binarySearch(int array[], int key, int size)

For example,
array[] = {9, 7, 5}
binarySerach(array, 9, 3) returns 0, binarySerach(array, 0, 3) returns -1

Programme:

//Date:2020/4/24
//Author:Kamenrider Justice
int binarySearch(int array[], int key, int size)
{
   int i;
   for(i=0;i<size;i++)
   {
      if(key==array[i])
      {
         return i;
      }
   }
   return -1;
}

Django 入门到实战:打造热门博客系统

你可能感兴趣的:(C语言Matrix)