Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int binarySearch(int * searchArr, int size, int search, int * numComparisons){
- // If the size of the list is 1, just perform the comparison!
- printArray(searchArr, size);
- if(size == 1) {
- if(searchArr[0] == search){
- return 0;
- } else {
- return -1;
- }
- } else if(size == 0){
- return -1;
- } else {
- if(searchArr[(size / 2)-1] >= search){
- (*numComparisons)++;
- return binarySearch(searchArr, (size / 2), search, numComparisons);
- } else {
- return binarySearch(searchArr + (size / 2), size - (size / 2), search, numComparisons) + (size / 2);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement