Question

In: Computer Science

Q2 - 15 pts) Given a minimum unimodal array of integers, run the binary search algorithm...

Q2 - 15 pts) Given a minimum unimodal array of integers, run the binary search algorithm to find the minimum element. You need to show the initial and the iteration-level values of the left index, right index and middle index as well as your decisions to reduce the search space in each iteration.

0

1

2

3

4

5

6

7

8

9

33

32

27

26

24

22

21

8

7

3

IN C++ PLEASE

Solutions

Expert Solution

ANSWER:

CONSIDERING THE CONDITIONS AND REQUIREMENTS FROM THE QUESTION

HERE GIVEN REQUIREMENT :

0

1

2

3

4

5

6

7

8

9

33

32

27

26

24

22

21

8

7

3

Python Code:

def findMin(arr, low, high): # Function to find minimum element
  
while (low < high):
mid = low + (high - low) // 2;
  
if (arr[mid] == arr[high]):
high -= 1;
elif (arr[mid] > arr[high]):
low = mid + 1;
else:
high = mid;
return arr[high];
  

if _name_ == '_main_': # Driver code
  
arr = [33, 32, 27, 26, 24, 22, 21, 8, 7, 3];
n = len(arr);
print("The minimum element is : ", findMin(arr, 0, n - 1));

Output :

The minimum element is : 3

Iterations:

33 32 27 26 24 22 21 8 7 3 - Array Elements

0 1 2 3 4 5 6 7 8 9 - Index

Iteration 1:

low = 0 high = 9

while( 0<9 ) //True

mid = 0 + (9-0) //2 = 4

if( a[4]==[9] )

24 == 3 //False

elif( a[4]>a[9] )

24>3 //True

low = 4+1 = 5

Iteration 2:

while( 5<9 ) //True

mid = 5 + (9-5) //2 = 7

if( a[7]==[9] )

8==3 //False

elif( a[7]>a[9] )

8>3 //True

low = 7+1= 8

Iteration 3:

while(8<9) //True

mid=8+(9-8)//2=8

if(a[8]==[9])

7==3 //False

elif(a[8]>a[9])

7>3 //True

low=8+1=9

Iteration 4:

while(9>9) // False

return a[9]

return 3

NOTE : PLEASE UPVOTE ITS VERY MUCH NECESSARY FOR ME A LOT. PLZZ....


Related Solutions

Q2 - 15 pts) Given a minimum unimodal array of integers, run the binary search algorithm...
Q2 - 15 pts) Given a minimum unimodal array of integers, run the binary search algorithm to find the minimum element. You need to show the initial and the iteration-level values of the left index, right index and middle index as well as your decisions to reduce the search space in each iteration. 0 1 2 3 4 5 6 7 8 9 33 32 27 26 24 22 21 8 7 3
Given a minimum unimodal array of integers, run the binary search algorithm to find the minimum...
Given a minimum unimodal array of integers, run the binary search algorithm to find the minimum element. You need to show the initial and the iteration-level values of the left index, right index and middle index as well as your decisions to reduce the search space in each iteration. 42 39 2 6 9 16 20 28 31 34
Given an array storing integers ordered by value, modify the binary search routine to return the...
Given an array storing integers ordered by value, modify the binary search routine to return the position of the integer with the greatest value less than K when K itself does not appear in the array. Return ERROR if the least value in the array is greater than K.
Implement a recursive binary search on an array of 8-byte integers in LEGV8
Implement a recursive binary search on an array of 8-byte integers in LEGV8
Binary Search. Write a MIPS assembly program to perform a binary search on A[10], which is an array of 10 positive integers.
Binary Search. Write a MIPS assembly program to perform a binary search on A[10], which is an array of 10 positive integers. Your program should have a main routine that does the following:(a) Prompt the user to enter all the 10 integers in the array.(b) Prompt the user to enter the number to be searched.(c) Reads the integer values and makes sure it is a positive integer.(d) Prints the index of the integer. If the input is not available in...
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...
(a) Consider the general k-ary search algorithm (generalization of binary search) which splits a sorted array...
(a) Consider the general k-ary search algorithm (generalization of binary search) which splits a sorted array of size n into k subsets each of size n/k and recursively searches only one of these k subsets. Which one of these k subsets should be searched is decided by making (k − 1) comparisons, each of which can be done in constant time. Clearly, the recurrence relation for this k-ary search algorithm can be written as, T(n) = T(n/k) + (k −...
Given an array of foods, create a binary search tree. Then, make a copy of that...
Given an array of foods, create a binary search tree. Then, make a copy of that BST and balance it. Language is C++. Vectors are not allowed. The balance function definition: void balance(BST treeObj); and then when writing the function: void BST::balance(BST treeObj) where BST is a class. The function will be called in main like: originalTree.balance(balancedTree);
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.
// This program demonstrates a Binary Search, which search for a value // in an array,...
// This program demonstrates a Binary Search, which search for a value // in an array, assuming that the array is sorted in descending order. // You have to modify the function binarySearch() to search for a value // in an array that is sorted in ascending order. // NOTES: // Uncomment line 34 and comment line 32. You don't have to edit anything // else in the main(), just in the binarySearch() function. // EXAMPLES (using the array sorted...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT