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

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...
(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
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.
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an...
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an array. Use number of data N at least more than 10. The function Binary Search should return the index of the search key V.
Make a Binary search program for C# and write algorithm and explain it in easy words...
Make a Binary search program for C# and write algorithm and explain it in easy words also show output and input
Create a List object that uses the binary search algorithm to search for the string "A"....
Create a List object that uses the binary search algorithm to search for the string "A". Display a message box indicating whether the value was found. Language: C#
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version)...
Write an algorithm in pseudocode for the binary search method using a while loop (iterative version) The recursive ternarySearch method returns true or false depending if the element was found or not. The ternarySearch method works in a similar manner to a binary search except it uses two mid values that “divide” the array into three portions. So, it needs to consider three recursive scenarios: See sample run: Accounts are: [0] 5658845 [1] 8080152 [2] 1005231 [3] 4520125 [4] 4562555...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT