1. Please write a Divide-and-Conquer Java algorithm solving the following problem:
Given an "almost sorted" array of distinct integers, and an integer x, return the index of x in the array. If the element x is not present in the array, return -1.
"Almost sorted" means the following. Assume you had a sorted array A[0…N], and then split it into two pieces A[0…M] and A[M+1…N], and move the second piece upfront to get the following:
A[M+1]…A[N]A[0]…A[M].
Thus, the "almost sorted" array is either a sorted array, or it consists of two sorted subarrays, such that every element of the first subarray is greater or equal than every element of the second subarray.
For example, the array {3, 17, 28, 935, 1011, -10, 0, 2} is "almost sorted" since it consists of two sorted subarrays: {3, 17, 28, 935, 1011} and {-10, 0, 2} with the property that each element in the first subarray is greater or equal than every element of the second subarray.
Note: One of the subarrays can be empty, i.e., the array might be sorted.
You need to develop an efficient modification of the Binary Search Algorithm, with worst-case running time of ( ) for an array of n elements.
Reminder: In Java, elements of an array of n elements have indexes 0…n-1.
Formally speaking, your input is an array of distinct integers, and the element x to find; your output is: the index of x in the array, or -1 in case x is not there.
With the array above and x=935, the algorithm has to return 3 (the index of the element 935 in the array).
Please develop the following Java function:
public static int FindIndex(int[] arr, int x)
Here arr is the array of distinct integers, x is the element to find.
NOTE: In your algorithm, you do not have to check that the array is "almost sorted". However, you have to check "boundary cases" like an empty array.
import java.util.*;
public class Main
{
static int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;
// If the element is present at the middle itself
if (arr[mid] == x)
return mid;
// If element is smaller than mid, then it can only
// be present in left subarray
if (arr[mid] > x)
{ int rr= binarySearch(arr, l, mid - 1, x);
if(rr==-1)//modified
{
return binarySearch(arr, mid + 1, r, x);
}
return rr;
}
// Else the element can only be present in right
// subarray
int k =binarySearch(arr, mid + 1, r, x);
//modified part
if(k==-1)
{
return binarySearch(arr, l, mid - 1, x);
}
return k;
}
// We reach here when element is not present in array
return -1;
}
public static int FindIndex(int[] arr, int x)
{
if(arr.length==0)return -1;//if array is empty
return binarySearch(arr,0,arr.length-1,x);
}
public static void main(String[] args) {
int a[] = {3, 17, 28, 935, 1011, -10, 0, 2}
;//declaring array
int r = FindIndex(a,935);
System.out.println("index of
935:"+r);
r = FindIndex(a,-10);
System.out.println("index of
-10:"+r);
r = FindIndex(a,0);
System.out.println("index of
0:"+r);
r = FindIndex(a,1);
System.out.println("index of
1:"+r);
r = FindIndex(a,3);
System.out.println("index of
3:"+r);
r = FindIndex(a,2);
System.out.println("index of
2:"+r);
r = FindIndex(a,17);
System.out.println("index of
17:"+r);
}
}
output:
index of 935:3
index of -10:5
index of 0:6
index of 1:-1
index of 3:0
index of 2:7
index of 17:1
//PLS give a thumbs up if you find this helpful, it helps me alot,
thanks.
//if you have any doubts, ask me in the comments
1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array...
the programming language is in java Problem 2 You are given an array A with n distinct elements. Implement an (n log n)-time algorithm that creates an array B where all elements are in range from 0 to n - 1 and where the order of elements is the same as in A. That is, 0 has the same index in B as the smallest element in A, 1 has the same index in B as the second smallest element...
1. Design and write a Divide& Conquer algorithm that, given an array A of n distinct integers which is already sorted into ascending order, will find if there is some i such that Ali] in worst-case 0(log n) time.
JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration skill Problem description: Write the following eight methods and write a main function to test these methods // return the index of the first occurrence of key in arr // if key is not found in arra, return -1 public static int linearSearch(int arr[], int key) // sort the arr from least to largest by using select sort algorithm public stati void selectSort(int arr[])...
Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...
IN JAVA please Given a sorted array 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. Your code will be tested for runtime. Code which does not output a result in logarithmic time (making roughly log(2) N comparisons) will fail the tests. A sample main function is provided so that you may test your code on sample inputs. For testing purposes, the...
Please answer by mathematical language: An array of n elements is almost sorted if and only if every element is at most k spots away from its actual location. Assuming that you can only perform pairwise comparisons, formally prove that any algorithm which can sort almost sorted arrays must have running time Ω(n log k), You may assume that n is a multiple of k.
1.) Write a function called canMakeSum() that takes a sorted array of n integers and determines whether two distinct elements of the array add up to some specified value, "key". If so, return 1. Otherwise, return 0. The function signature is: int canMakeSum(int *array, int n, int key);
In this lab, you get to try your hand with some recursion operating on arrays and subarrays. All of the following methods of the class RecursionProblems must be fully recursive, with no loops allowed at all! (If-else statements are okay.) Because these methods are quite short, this last time there are six methods to write instead of the usual four. (The scoring rule is that you start gaining any points for this lab only at the third method that passes...
1. (16 pts.) Sorted Array Given a sorted array A of n (possibly negative) distinct integers, you want to find out whether there is an index i for which Al = i. Give a divide-and-conquer algorithm that runs in time O(log n). Provide only the main idea and the runtime analysis.
Please Write in Java An array is sorted (in ascending order) if each element of the array is less than or equal to the next element . An array of size 0 or 1 is sorted Compare the first two elements of the array ; if they are out of order, the array is not sorted; otherwise, check the if the rest of the array is sorted. Write a boolean -valued method named isSorted that accepts an integer array , and the number of...