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;
}
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;
}
In C++, make the following binary search function work on an array of strings to give...
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. 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)...
Problem: I am trying to implement a binary search function in Visual Studio 2019 using C#. This is my first program in C# and I translated it from Java. In this program, I am to carry out the same 10,000,000 unsuccessful searches for eight different-sized arrays, namely arrays of sizes, 128, 512, 2048, 8192, 32768, 131072, 524288, and 2,097,152. I need to measure each of the three programs and the time it takes to do the 10,000,000 searches for each...
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...
Write a C++ function binsearch that carries out the binary search algorithm on a sorted array of integers. Your function takes as parameters an array of integers (sorted in increasing order), the size of the array, and a target integer to search for. The function returns the index of the target in the array, and returns -1 if the target is not in the array. The file main1.txt on the webpage contains code that generates a specific array of integers...
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 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,...
Having issues using binary search on a pointer array. 1. Write 1000 random ints to file 2. Binary Search to find if number exists in element from file. int main() { ArrayActions action; int count; int userInput; int num = 1000; int array[num]; ofstream myFile ("/Users/chan/Desktop/LANEY_CIS27/Assignemtn2_CIS27/Assignemtn2_CIS27/File.txt"); //Set srand with time to generate unique random numbers srand((unsigned)time(0)); //Format random num up to 999 for(count = 0; count < num; count++) { array[count] = rand() % 1000; } //Condition...
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, 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...