0% found this document useful (0 votes)
73 views4 pages

Data Structures and Algorithms Code

The document provides instructions for Assignment 13. It asks students to write code to find an element in a sorted array that has been rotated an unknown number of times. It provides an example input and output. The code provided implements binary search to find the pivot point and then searches either the left or right side depending on where the key would be located. It then asks about the strategy for when left and middle elements are identical, noting that binary search can still be used by comparing to the middle element. The time complexity of binary search is noted as O(log n).

Uploaded by

Pavan Koutharapu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views4 pages

Data Structures and Algorithms Code

The document provides instructions for Assignment 13. It asks students to write code to find an element in a sorted array that has been rotated an unknown number of times. It provides an example input and output. The code provided implements binary search to find the pivot point and then searches either the left or right side depending on where the key would be located. It then asks about the strategy for when left and middle elements are identical, noting that binary search can still be used by comparing to the middle element. The time complexity of binary search is noted as O(log n).

Uploaded by

Pavan Koutharapu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Assignment 13 June 18, 2021

LAB-51+52 VITAP University, Amaravati, AP Full Marks – 10

Duration:1h30mins

Instructions:

a. The programs need to be written in JAVA.


b. Assignments to be submitted only on MS Teams.
c. Submit the solution code and snapshot of the output in a pdf file
accompanied with codes in a .java file.
d. Mention your details, such as, Roll no/regd no, Name and lab slot
(L51+52) on top right corner of the PDF.
e. Mention file name as Main_name.java.

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

lnput: find 5 in {15, 16, 19, 20, 25, 1, 3, 4, 5, 7, 10, 14}

Output: 8 (the index of 5 in the array)

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

LAB-51+52 VITAP University, Amaravati, AP Full Marks – 10


return bsearch(arr, pivot + 1, n - 1, key);
}
static int pivot(int arr[], int low, int high)
{
if (high < low)
return -1;
if (high == low)
return low;
int mid = (low + high) / 2;
if (mid < high && arr[mid] > arr[mid + 1])
return mid;
if (mid > low && arr[mid] < arr[mid - 1])
return (mid - 1);
if (arr[low] >= arr[mid])
return pivot(arr, low, mid - 1);
return pivot(arr, mid + 1, high);
}
static int bsearch(int arr[], int low, int high, int key)
{
if (high < low)
return -1;
int mid = (low + high) / 2;
if (key == arr[mid])
return mid;
if (key > arr[mid])
return bsearch(arr, (mid + 1), high, key);
return bsearch(arr, low, (mid - 1), key);
}
public static void main(String args[])
{
int arr1[] = { 15, 16, 19, 20, 25, 1, 3, 4, 5, 7, 10, 14 };
int n = arr1.length;
int key = 5;
System.out.println(b_search(arr1, n, key)+" ("+"the index of "+
key+" in the array"+")");
}
}

OUTPUT:
Assignment 13 June 18, 2021

LAB-51+52 VITAP University, Amaravati, AP Full Marks – 10

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 ?

=>

If the left and the middle are identical in the array


we can apply the same strategy as in binary search

1. Compare x with the middle element.


2. If x matches with the middle element, we return the mid
index.
3. Else If x is greater than are equal to mid element, then x can
only lie in the right half subarray after the mid element. So we
recur for the right half.
4. Else (x is smaller) recur for the left half.

The time complexity of the binary search algorithm is O(log n).


The best-case time complexity would be O(1)
CODE (FOR REFERENCE PURPOSE):
class bs {
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
return binarySearch(arr, mid + 1, r, x);
}
return -1;
}
public static void main(String args[])
{
bs ob = new bs();
int arr[] = { 2, 2, 2, 3, 4, 2 };
int n = arr.length;
Assignment 13 June 18, 2021

LAB-51+52 VITAP University, Amaravati, AP Full Marks – 10


int x = 2;
int result = ob.binarySearch(arr, 0, n - 1, x);
if (result == -1)
System.out.println("Element not present");
else
System.out.println("Element found at index " + result);
}
}

OUTPUT:

You might also like