Consider an array containing N unique values where for some index i, the values are in increasingorder from index 0 to (i-1), and then again from i to (N-1). An example array is given below.Here i=3, it means the values are in increasing order from index 0 to 2, and then again from 3 to 7.index 0 1 2 3 4 5 6 7value 9 12 15 2 4 5 7 8Given the value of i, propose an algorithm to search a key_value in the array. Complexity of youralgorithm must be less than O(N).a) Present your solution idea as a code/ pseudocode/ flowchart/ step-by-step instructions/logical explanations in short.b) Write the time complexity of your presented solution.c) Show a simulation of the Merge Sort algorithm to organize the whole array in increasingorder.
Question
Consider an array containing N unique values where for some index i, the values are in increasingorder from index 0 to (i-1), and then again from i to (N-1). An example array is given below.Here i=3, it means the values are in increasing order from index 0 to 2, and then again from 3 to 7.index 0 1 2 3 4 5 6 7value 9 12 15 2 4 5 7 8Given the value of i, propose an algorithm to search a key_value in the array. Complexity of youralgorithm must be less than O(N).a) Present your solution idea as a code/ pseudocode/ flowchart/ step-by-step instructions/logical explanations in short.b) Write the time complexity of your presented solution.c) Show a simulation of the Merge Sort algorithm to organize the whole array in increasingorder.
Solution
a) The problem can be solved using a modified version of Binary Search. Here are the step-by-step instructions:
- Start by dividing the array into two sub-arrays at index i. The first sub-array is from index 0 to (i-1) and the second sub-array is from i to (N-1).
- Perform a binary search on both sub-arrays for the key_value.
- If the key_value is found in either of the sub-arrays, return the index. If it's not found, return -1 or a message indicating that the key_value is not in the array.
Pseudocode:
function binarySearch(array, left, right, key_value)
while left <= right
mid = left + (right - left) / 2
if array[mid] == key_value
return mid
else if array[mid] < key_value
left = mid + 1
else
right = mid - 1
return -1
function search(array, i, key_value)
index1 = binarySearch(array, 0, i-1, key_value)
index2 = binarySearch(array, i, N-1, key_value)
if index1 != -1
return index1
else if index2 != -1
return index2
else
return -1
b) The time complexity of the presented solution is O(log N) because we are performing a binary search on the two sub-arrays.
c) Here is a simulation of the Merge Sort algorithm:
- Divide the unsorted list into N sublists, each containing one element (a list of one element is considered sorted).
- Repeatedly merge sublists to produce new sorted sublists until there is only one sublist remaining. This will be the sorted list.
Pseudocode for Merge Sort:
function mergeSort(array)
if length(array) > 1
mid = length(array) / 2
left = array[0, ..., mid-1]
right = array[mid, ..., N-1]
mergeSort(left)
mergeSort(right)
i = j = k = 0
while i < length(left) and j < length(right)
if left[i] < right[j]
array[k] = left[i]
i++
else
array[k] = right[j]
j++
k++
while i < length(left)
array[k] = left[i]
i++
k++
while j < length(right)
array[k] = right[j]
j++
k++
This will sort the entire array in increasing order.
Similar Questions
Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You must write an algorithm with O(log n) runtime complexity. Example 1:Input: nums = [1,3,5,6], target = 5Output: 2Example 2:Input: nums = [1,3,5,6], target = 2Output: 1Example 3:Input: nums = [1,3,5,6], target = 7Output: 4 Constraints:1 <= nums.length <= 104-104 <= nums[i] <= 104nums contains distinct values sorted in ascending order.-104 <= target <= 104
Given an array Arr of N positive integers. Your task is to find the elements whose value is equal to that of its index value ( Consider 1-based indexing ).
Given an unsorted array Arr[] of N integers and a Key which is present in this array. You need to write a program to find the start index( index where the element is first found from left in the array ) and end index( index where the element is first found from right in the array ).If the key does not exist in the array then return -1 for both start and end index in this case.
A step array is an array of integers where each element has a difference of at most k with its neighbor. Given a key x, we need to find the index value of x if multiple elements exist, and return the first occurrence of the key.
Program to demonstrate the use of Binary Search to search a given element in a sorted array in ascending order.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.