Question

In C++, make the following binary search function work on an array of strings to give...

In C++, make the following binary search function work on an array of strings to give you the index location of the string you want in the array. It currently works on integers only.

int binarySearch(int arr[], int firstIndex, int lastIndex, int target){

int index;

if (firstIndex>lastIndex)

index = -1;

else {

int mid = firstIndex + (lastIndex - firstIndex) / 2;

if (target == arr[mid])

index = mid;

else if (target < arr[mid])

index = binarySearch(arr, firstIndex, mid - 1, target);

else

index = binarySearch(arr, mid + 1, lastIndex, target);

}

      return index;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
int binarySearch(string arr[], int firstIndex, int lastIndex, string target){
    int index;
    if (firstIndex>lastIndex)
        index = -1;
    else {    
        int mid = firstIndex + (lastIndex - firstIndex) / 2;
        if (target == arr[mid])
            index = mid;
        else if (target < arr[mid])
            index = binarySearch(arr, firstIndex, mid - 1, target);
        else
            index = binarySearch(arr, mid + 1, lastIndex, target);
    }
    return index;
}
Add a comment
Know the answer?
Add Answer to:
In C++, make the following binary search function work on an array of strings to give...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Complete the binary search function. def binary Search (array, first, last, key): if last >= first:...

    Complete the binary search function. def binary Search (array, first, last, key): if last >= first: mid = int(first + (last - first)/2) if array[mid] return mid key: elif array[mid] > key: ##Statement 1 else: ##Statement 2 else: return -1 Statement 1: return binarySearch(array, last, mid+1, key) Statement 2: return binarySearch(array, mid-1, first, key) Statement 1: return binarySearch(array, first, mid+1, key) Statement 2: return binarySearch(array, mid-1, last, key) Statement 1: return binarySearch(array, mid-1, first, key) Statement 2: return binary Search(array,...

  • 5. (40 Points) Write a complete Java class named ArrayMethods that implements the methods listed below....

    5. (40 Points) Write a complete Java class named ArrayMethods that implements the methods listed below. All the methods accept the following parameters: Parameters: intar An array of int values. int firstIndex The index of the first element to include in the sum. int lastIndex The index of the last element to include in the sum. Please note that all methods must verify the validity of first Index and lastIndex. public static int sum(int[] arr, int first Index, int lastIndex)...

  • Given the binary search function, answer following question: int binarySearch(int a[], int size, int target, int...

    Given the binary search function, answer following question: int binarySearch(int a[], int size, int target, int low, int high) { while (low <= high) { int mid = (low + high) / 2; if (a[mid] == target) return mid; else if (a[mid] < target) low = mid + 1; else high = mid 1: } return -1; } 1) If array a[] = {4, 5, 18, 25, 66, 70, 78}, size = 7, target = 71, low = 0, high...

  • Searching/sorting tasks and efficiency analysis - Binary Search Search for the character S using the binary...

    Searching/sorting tasks and efficiency analysis - Binary Search Search for the character S using the binary search algorithm on the following array of characters: A E G K M O R S Z. For each iteration of binary search use a table similar to the table below to list: (a) the left index and (b) the right index of the array that denote the region of the array that is still being searched, (c) the middle point of the array,...

  • Given the following code for a binary search, how many times this method have to be...

    Given the following code for a binary search, how many times this method have to be executed in order to find the number 5? A) 1 time B) 2 times C) 3 times D) 4 times E) more than 5 times public class BinarySearch{ public static void main(String []args){ if (right >= left) { int middle = left + (right - left) / 2; if (arr[middle] == num) return middle; if (arr[mid] > num) return binarySearch(arr, left, middle - 1,...

  • Write a Java application that implements the recursive Binary Search alogrithm below: Write a Java application...

    Write a Java application that implements the recursive Binary Search alogrithm below: Write a Java application that implements the recursive Binary Search alogrithm below:/** * Performs the standard binary search using two comparisons * per level. This is a driver that calls the recursive method * @return index where item is found or NOT FOUND if not found */public static <AnyType extends Comparable<? super AnyType>> int binarySearch(AnyType [] a, AnyType x) {return binarySearch(a, x, 0, a.length -1);}/** * Hidden recursive...

  • without coding Give the Big O run-time of the following algorithms. Binary Search: def binary-search (arr,...

    without coding Give the Big O run-time of the following algorithms. Binary Search: def binary-search (arr, low, high, x): # Check base case if low > high : return None else: mid = (high + low) // 2 element arr[mid] == X: if element return mid elif element > X: return binary-search(arr, low, mid 1, x) else: return binary_search(arr, mid + 1, high, x) Selection Sort: def selection_sort (arr): for i in range (len(arr)): smallest index = i smallest value...

  • You are making a .h file. Implement a recursive binary search function. bSearch passes in an...

    You are making a .h file. Implement a recursive binary search function. bSearch passes in an array of integers, the size of the array, and the value the function is searching for. The function should return the index where found or -1 if not found. You will want to implement a recursive helper function that passes in the array, the value to be located, and the beginning and ending of the range of values to search within. Use the provided...

  • Java The following questions ask about tracing a binary search. To trace the binary search, list...

    Java The following questions ask about tracing a binary search. To trace the binary search, list the value of first, last, and mid for each pass through the loop or method. Use one of these methods for your trace. public static int binarySearchIterative(int[] numbers, int target) { boolean found = false; int first = 0; int last = numbers.length - 1; while (first <= last && !found) { int mid = (first + last) / 2; if (numbers[mid] == target)...

  • 6 (10 points Remember the recursive Searching algorithm Binary Search. Write a recursive method to search...

    6 (10 points Remember the recursive Searching algorithm Binary Search. Write a recursive method to search for a target character in the array and return the index location if found or -1 if it is not found. 7 a5 points 17 points cach) Write the code to create a GUI based class Temperature Converter which inherits from JFrame and implements the ActionListerner interface. public static int binary Search(char target, char( theValues, int firstIndex, int lastindex) Example: Clicked "F to C"...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT