What indexes will be examined as the middle element by a binary search for the target value 8 when the search is run on the following input array? Check if the input array is in sorted order. What can you say about the binary search algorithm’s result?
int[] numbers = {6, 5, 8, 19, 7, 35, 22, 11, 9};
int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9};
(Using Java code please)
int[] numbers = {6, 5, 8, 19, 7, 35, 22, 11, 9}; for this array it will not work as it is not in the sorted order
int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9};
here first we will check with as 5 as it is middle element
next as 8>5 we will move right and check with 7
so next 8>7 we will move to right and check with 8
so 8==8 we will return true
public class BinarySearchIterative {
public static void main(String[] args) {
int[] numbers1 = {1, 2, 3, 4, 5, 6,
7, 8, 9};
int[] numbers2= {6, 5, 8, 19, 7,
35, 22, 11, 9};
System.out.println(binarySearch(numbers1, 8));
System.out.println(binarySearch(numbers2, 8));
}
public static int binarySearch(int sortedArray[], int
x)
{
if(!isItSorted(sortedArray))
return -1;
int l = 0, r = sortedArray.length - 1;
while (l <= r) {
int m = l + (r - l) / 2;
System.out.println(sortedArray[m]);
if (sortedArray[m] == x)
return m;
// If x greater, ignore left sub array
if (sortedArray[m] < x)
l = m + 1;
// If x is smaller, ignore right sub array
else
r = m - 1;
}
return -1;
}
private static boolean isItSorted(int[] arr) {
for(int i=0;i<arr.length-1;i++)
{
if(!(arr[i]<arr[i+1]))
return false;
}
return true;
}
}
What indexes will be examined as the middle element by a binary search for the target...
What indexes will be examined as the middle element by a binary search for the target value 8 when the search is run on the following input array? Check if the input array is in sorted order. What can you say about the binary search algorithm’s result? int[] numbers = {6, 5, 8, 19, 7, 35, 22, 11, 9}; int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9};
JAVA question: Suppose we are performing a binary search on a sorted array called numbers initialized as follows: // index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 int[] numbers = {-30, -9, -6, -4, -2, -1, 0, 2, 4, 10, 12, 17, 22, 30}; // search for the value -5 int index = binarySearch(numbers, -5); Write the indexes of the elements that would be examined by the binary search (the mid values...
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.) (a) A string contains several X's followed by several O's. Devise a divide-and-conquer algorithim that finds the number of X's in the string in log2n steps, where n is the length of the string. (b) An array originally contained different numbers in ascending order but may have been subsequently rotated by a few positions. For example, the resulting array may be: 21 34 55 1 2 3 4 5 8 13 Is it possible to adapt the Binary Search...
Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over an array of integer numbers and size n. The function should return the index of the search key if the search key exists and return - 1 if the search key doesn't exist. [10 Points] Q2] Write a C function to implement the selection sort algorithm, to sort an array of float values and size n. The function should sort the array in ascending...
1. Randomized Binary Search Which are true of the randomized Binary Search algorithm? Multiple answers:You can select more than one option A) It uses a Variable-Size Decrease-and-Conquer design technique B) Its average case time complexity is Θ(log n) C) Its worst case time complexity is Θ(n) D) It can be implemented iteratively or recursively E) None of the above 2. Randomized Binary Search: Example Assume you have an array, indexed from 0 to 9, with the numbers 1 4 9...
Write a C function that implements the Liner Search algorithm instead of Binary Search. However, this linear search algorithm returns the indices of the longest sorted subset of numbers in an array of integers of size n. The longest sorted subset of numbers must satisfy three conditions. First, the subset consists of unique numbers (no duplicate); second, all the numbers in this subset is divisible by m, the minimum size of the subset is two elements. In the main method...
IN JAVA please Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Your code will be tested for runtime. Code which does not output a result in logarithmic time (making roughly log(2) N comparisons) will fail the tests. A sample main function is provided so that you may test your code on sample inputs. For testing purposes, the...
The binary search algorithm from Chapter 9 is a very efficient algorithm for searching an ordered list. The algorithm (in pseudocode) is as follows: highIndex - the maximum index of the part of the list being searched lowIndex - the minimum index of the part of the list being searched target -- the item being searched for //look in the middle middleIndex = (highIndex + lowIndex) / 2 if the list element at the middleIndex is the target return the...
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...