//#################################################################### // This program is an implementation of binary search. // First an array is filled with random 4-digit integers, then bubble // sort is used to sort the array. A number is randomly picked from // the array and binar search is used to search that number. //#################################################################### #include #include int binary_search(int val, int arr[], int size) { int left = 0, right = size-1, middle; int iter=0; do { middle = (left + right) / 2; printf("##### Iteration %2d: left= %d middle= %d right= %d\n", ++iter, left, middle, right); if(arr[middle] < val) left = middle + 1; else if(arr[middle] > val) right = middle - 1; else return middle; } while(left <= right); // (left < right) won't work return -1; } #define SIZE 10 void bubble_sort(int arr[], int size); int main() { int a[SIZE], i, waldo, res; srand(1); // seed. // fill the array with random 4-digit numbers for(i=0; ii; --j) { if (arr[j] < arr[j-1]) { temp = arr[j]; arr[j] = arr[j-1]; arr[j-1] = temp; } } } }