Question

In: Computer Science

Write a modification of the recursive binary search algorithm that always returns the smallest index whose...

Write a modification of the recursive binary search algorithm that always returns the smallest index whose element matches the search element. Your algorithm should still guarantee logarithmic runtime. Give a brief discussion of the best- and worst-case runtimes for this new algorithm as they compare to the original.

NOTE: You do not have to re-write the entire algorithm. You just need to indicate any changes you would make and show the pseudocode for any portions that are changed.

Example: Given the array [0, 0, 1, 1, 1, 2, 2, 3] and the search element 1, the algorithm should return 2.

Solutions

Expert Solution

Let algorithm be called modifiedBinarySearch(a, i , j , k)

here , 'a' is input array, 'i' is first index of array , 'j' is last index of array and 'k' is search element .

here n = j-i+1

C style pseudoCode:-

int modifiedBinarySearch(a, i , j , k) // it takes time = T(n)

{

if(i==j)// it takes c1 time

{

if (a[i]==k)// it takes c2 time

return i

else

return -1 // means no 'k' element.

}

else

{

m = floor((i+j)/2)// it takes c3 time

if(a[m]==k)// it takes c4 time

{

if(a[m-1]!=k)/* it takes c5 time, this is change in original binary search algorithm . if you element this then rest is binary search */

return m

}

else if(a[m]>=k)

return modifiedBinarySearch(a, i , m-1 , k) // it takes T(n/2)

else

return modifiedBinarySearch(a, m+1 , j , k) // it takes T(n/2)

}

}

Time complexity analysis:-

In best case :-

In best case we will find our required index first attempt itself.so , T(n) = c1 + c3+ c4 + c5

let c1 + c3+ c4 + c5 = c, after all sum of constants is a constant

so , T(n) = c = O(1)

now , we know that in binary search only one statement which takes c5 time is not available.

so , time taken by binary search T(n) = c1 + c4 + c5 = O(1)

so , we can see that order of growth of both algorithms(modifiedBinarySearch and BinarySearch) are same.

In Worst case:-

In worst case , the recursive calls are executed.

so , recursive relation that appears is

T(n) = T(n/2) + c , if n >1

= O(1) , n=1

T(n) = T(n/2) + c

solving above using subsitution we get

T(n) = T(n/22) + c + c

= T(n/23) + c+ c+ c

continuing like this 'd' times we get

= T(n/2d) + c+ c+...d times

it will stop when n/2d =1 so , d = log2n

= T(1) + c.log2n

= O(1) + log2n = O(log2n)

In Binary search same recurrence relation appears so , time taken by binary search in worst case = O(log2n)

so , we can see that order of growth of both algorithms(modifiedBinarySearch and BinarySearch) are same even in worst case.

if we apply algorithm then on given example

modifiedBinarySearch(a, 0 , 7 , 1)

m = 3

a[m] = 1 is true a[m-1]!=1 is not true so , another recursive call occurs

since a[m]==k so ,

modifiedBinarySearch(a, 0 , 2 , 1)

m = 1

a[m] = 1 is not true therefore , another recursive call occurs.

since a[m] < 1 so ,

modifiedBinarySearch(a, 2,2,1)

so , it now returns 2 as answer .


Related Solutions

Write a version of the binary search algorithm that can be used to search a string...
Write a version of the binary search algorithm that can be used to search a string vector object. Also, write a program to test your algorithm. (Use the selection sort algorithm you developed in Programming Exercise 12 to sort the vector.) Your program should prompt the user to input a series of strings, ending the input stream with zzz. The program should then prompt the user to search for a specific string in the list. If the string is found...
Consider the recursive formulation of the Binary Search algorithm. Given a sorted and non-decreasing list of...
Consider the recursive formulation of the Binary Search algorithm. Given a sorted and non-decreasing list of comparable objects L, and a new item x, find the location (index) of x or indicated that x is not in L. 5a) Give a recursive algorithm for binary search. No, while, for, repeat etc statements are allowed. Only if, if else and assignment statements are allowed. 5b) Write a difference equation for the running time of your Binary Search algorithm. Solve your equation...
Binary Search Algorithm a.) Create a Java application that utilizes the "Binary Search Algorithm" presented in...
Binary Search Algorithm a.) Create a Java application that utilizes the "Binary Search Algorithm" presented in chapter 19 (NOT Java's pre-built binarySearch() method from imported Java library) to search for an integer in a random array of size 30 in the range of 0 to 1000 inclusive. You should use Java's random number generator to randomize the numbers in your array. b.) The application's main() method should display unsorted array and sorted array, prompt user for a search key, allow...
(C++ ) ·In “recursive.cpp”, write a recursive function minDoub() which: ·returns the address of the smallest...
(C++ ) ·In “recursive.cpp”, write a recursive function minDoub() which: ·returns the address of the smallest value in the array. If the array is empty, return the “end” pointer ·and takes as parameters: (1)   a pointer to double. The pointer is the address of the start of an array, (2)   the “end” pointer to the address after the array (3)   and the address of the smallest value seen so far ·Write main() to test this function – try a case where the array...
Assume you need to write a Java program that uses a binary search algorithm to search...
Assume you need to write a Java program that uses a binary search algorithm to search a sorted array for a given value. 1. Write a Java pseudocode that uses recursion to accomplish the task. Here is a hint. When you are searching for a particular value in an array, there are two possible outcomes. 1) The value is found and the array index of that value is returned. 2) The value is not found and we return -1. (5...
(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the...
(Binary Tree): Write a recursive implementation of the function, singleParent, that returns the number of the nodes in a binary tree that have only one child. Convert it to an iterative version. in C++
Using Java Write a method that returns the index of the smallest element in an array...
Using Java Write a method that returns the index of the smallest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header:   public static int indexOfSmallestElement (double[] array)
Write a function that takes a valid stringlist and returns the index of the smallest element...
Write a function that takes a valid stringlist and returns the index of the smallest element in the list represented by the stringlist. You may not use split(). Examples: >>> stringlist min index('[123,53,1,8]') # 1 is smallest2 >>> stringlist min index('[1,2,345,0]') # 0 is smallest3 >>> stringlist min index('[5] ') # 5 is smallest0
Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort I need it in Java with...
Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort I need it in Java with comments and I need the input file to be placed with Scanner not BufferedReader Please help I need Class River Class CTRiver and Class Driver Class River describes river’s name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong() that returns true if river is above 30 miles...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns,...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns, as output, the sum of the first n positive odd integers. Your solution should be recursive, and it should not contain any "for" loops or "while" loops.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT