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 16 25 36 49 64 81 100. You are searching for the key 100. Your random number generator gives the following numbers in range [0,9] to be used as indexes: 6 3 1 5 7 9 0 0 5 2 8 1 9 6 4 2 7. You consider those random numbers in order, ignoring those that have already been used or are outside the currently relevant range. How many key comparisons (i.e. comparing the search key of 100 against data in the array) will be required until you find the key 100? (Assume that <, =, or > can be determined with with a single key comparison.)
A) 1
B) 2
C) 3
D) 4
E) 5
F) 6
G) 7
H) 8
I) 9
J) 10
Short explanation would be appreciated.
Option A - It uses a Variable-Size Decrease-and-Conquer design technique. After every iteration, the jump size is decreased by 50% and in short 50% of the array is of no use anymore.
Option B - The average search time complexity is O(log n) as compared to linear search where the complexity is O(n).
Option C - The Worst time complexity is also O(log n) and not O(n), it would have been O(n) if the algorithm has to go through every element of the list or array and this won't be happening under any case.
Option D - The algorithm can be implemented both recursively and iteratively, using loops or maybe using recursion.
So, Options A, B, D are correct.
Here I am attaching a screenshot of the code of Randomized Binary Search for easier reference.
The code is well commented and can be easily understood.
Output - Element is present at index 9
In the case given above, we need to find the index of 100, beginning with 6 from the random no. generator. We notice that arr[6] < 100 so we need to consider indices greater than 6.
Indices present next 3,5,1 are ignored.
We go to 7 next, we again see that 100>arr[7], need to search for indices greater than 7 now.
Next coming to 9, we see 100 = arr[9]. we found out a number at index 9.
There were a total of 3 comparisons.
The thing implemented in the code is the better and optimal way of generating random nos in the relevant range.
1. Randomized Binary Search Which are true of the randomized Binary Search algorithm? Multiple answers:You can...
1. What is the worst case time complexity of insertion into a binary search tree with n elements? You should use the most accurate asymptotic notation for your answer. 2. A binary search tree is given in the following. Draw the resulting binary search tree (to the right of the given tree) after deleting the node with key value 8. 10 3. You have a sorted array B with n elements, where n is very large. Array C is obtained...
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};
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)
Consider an ordered array A of size n and the following ternary search algorithm for finding the index i such that A[i] = K. Divide the array into three parts. If A[n/3] > K. the first third of the array is searched recursively, else if A[2n/3] > K then the middle part of the array is searched recursively, else the last thud of the array is searched recursively. Provisions are also made in the algorithm to return n/3 if A[n/3]...
A binary search tree includes n nodes and has an height h. Check all that applies about the space complexity of TREE-MINIMUMX) TREE-MINIMUM () 1 while x. left NIL 2 3 return x x x.left O it is e (lg n) ■ it is 0(h). D it is e (1) ■ It is in place ■ it is Θ (n) A binary search tree includes n nodes and has an height h. Check all that applies about the space complexity...
In regards to binary search tree, can you answer why a BST with N nodes has at least log2N levels and at most N levels. so the runtime complexity is best case 0(logN) and worst case 0(N). Can you explain this with the following numbers in this order? 7,1,64,28,77
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...
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...
Tree & Hash Table & Heap Use the following integer keys 73, 58, 91, 42, 60, 130, 64, 87 to perform the followings: a) Binary Search Tree - Draw a binary search tree - Retrieve the integers keys in post-order - Retrieve the integers keys in pre-order - Draw a binary search tree after node 58 is deleted b) Create a Hash Table using the methods described below. Show the final array after all integer keys are inserted. Assumes that...
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...