int main(void){ int key = 0; int list[10] = {1, 3,4,5,6,7,8,9,12, 20}; int size = ((int)sizeof(list)/ sizeof(int)); int pos; printList(list ,size); //test1 key=3; pos=recursiveBinarySearch(list, 0, size-1, key); showResult(pos, key); //test2 key=20; pos=recursiveBinarySearch(list, 0, size-1, key); showResult(pos,key); //test3 key=10; pos=recursiveBinarySearch(list, 0, size-1, key); showResult(pos,key); //test4 key=-1; pos=recursiveBinarySearch(list, 0, size-1, key); showResult(pos,key); //test5 int list1[1]={2}; key=1; pos=recursiveBinarySearch(list1, 0, 0, key); showResult(pos,key); //test6 int list2[2]={1,3}; key=1; pos=recursiveBinarySearch(list2, 0, 1, key); showResult(pos,key); //test7 key=3; pos=recursiveBinarySearch(list, 0, 1, key); showResult(pos,key); //test8 key=0; pos=recursiveBinarySearch(list, 0, 1, key); showResult(pos,key); //test9 key=4; pos=recursiveBinarySearch(list, 0, 1, key); showResult(pos,key); //test10 key=2; pos=recursiveBinarySearch(list, 0, 1, key); showResult(pos,key); return 0; }
/* * Part of Cosmos by OpenGenus Foundation */ int recursiveBinarySearch(int arr[], int l, int r, int x) { if (r >= l) { int mid = l + (r - l)/2; if (arr[mid] == x) return mid; if (arr[mid] > x) return recursiveBinarySearch(arr, l, mid-1, x); return recursiveBinarySearch(arr, mid+1, r, x); } return -1; }
int main(void) { int arr[] = {1, 2, 3, 5}; int size = sizeof(arr)/ sizeof(arr[0]); int find = 3; printf("Position of %d is %d\n", find, recursiveBinarySearch(arr, 0, size-1, find)); printf("Position of %d is %d\n", find, binarySearch(arr, 0, size-1, find)); return 0; }
int recursiveBinarySearch(int list_of_numbers[], int desired_number, int low_number, int high_number) { int mid = (low_number + high_number) / 2; if (low_number > high_number) { return -1; } else if (list_of_numbers[mid] == desired_number) { return mid; } else if (list_of_numbers[mid] < desired_number) { return recursiveBinarySearch(list_of_numbers, desired_number, mid + 1, high_number); } else { return recursiveBinarySearch(list_of_numbers, desired_number, low_number, mid - 1); } }
int recursiveBinarySearch(int list[], int left, int right, int key){ int middle; if(left>right) return -1; else{ middle = left + (right - left)/2; if(key == list[middle]) return middle; else if(key <list[middle]) right = middle -1; else left = middle +1; return recursiveBinarySearch(list, left, right, key); } return -1; }
int readInput(FILE *input_file, char file_name[], int values[], int desired_number) { int count = 0; int value = 0; int array_size; input_file = fopen(file_name, "r"); if (feof(input_file) != 0) { printf("Error: can't open file\n"); return -1; } else { printf("File opened successfully\n"); } //Get the first line of the file, which gives the size of the array while (fscanf(input_file, "%d", &value) != EOF) { if (count == 0)//The first value is being read { array_size = value - 1;//So that the later ones start at the correct index } else { values[count - 1] = value; } count++; } fclose(input_file); int delta; clock_t t1, t2; t1 = clock(); int recursive_result = recursiveBinarySearch(values, desired_number, 0, array_size); t2 = clock(); delta = t2 - t1; int delta1; clock_t t3, t4; t3 = clock(); int iterative_result = iterativeBinarySearch(values, desired_number, array_size); t4 = clock(); delta1 = t3 - t4; displayOutput(values, array_size, recursive_result, delta, delta1, iterative_result); return 0; }