Data Structures and Algorithms Code
Data Structures and Algorithms Code
Duration:1h30mins
Instructions:
Q. Given a sorted array of n integers that has been rotated an unknown number of times,
write code to find an element in the array. You may assume that the array was originally
sorted in increasing order.
EXAMPLE
CODE:
class assign13
{
static int b_search(int arr[], int n, int key)
{
int pivot = pivot(arr, 0, n - 1);
if (pivot == -1)
return bsearch(arr, 0, n - 1, key);
if (arr[pivot] == key)
return pivot;
if (arr[0] <= key)
return bsearch(arr, 0, pivot - 1, key);
Assignment 13 June 18, 2021
OUTPUT:
Assignment 13 June 18, 2021
Q. The tricky condition is if the left and the middle are identical, as in the example array
{2, 2, 2, 3, 4, 2}. What strategy can be employed in binary search approach ? What will
be the time complexity ?
=>
OUTPUT: