Objectives
- Search through an array
- Explain the differences between linear and binary searching
int linsearch(int k, int arr[], int len) { for (int i=0; i<len; i++) { if (k == arr[i]) return i; } return -1; }
int binsearch(int k, int arr[], int len) { int hi = len - 1; int lo = 0; int middle; while (hi >= lo) { middle = (hi + lo) / 2; if (arr[middle] == k) return middle; else if (arr[middle] > k) hi = middle - 1; else lo = middle + 1; } return -1; }