Concept explainers
Explanation of Solution
Complete program:
//Import required packages
import java.util.*;
//Definition of class Sum
public class Sum
{
//Definition of class main
public static void main(String[] args)
{
//Declare array "a"
int a[] = new int[7];
int value, key;
//Create an object for scanner class
Scanner sc = new Scanner(System.in);
//Get the array values
System.out.println("Enter the array values:");
//For loop to get the values from the user
for (int i = 0; i<a.length; i++)
{
//Get the values
value = sc.nextInt();
//Assign the values to the array
a[i] = value;
}
//Get the key value from the user
System.out.println("Enter key values to be search index:");
key = sc.nextInt();
//Call the method "trinarySearch()"
int result = ternarySearch(a, key, 0, a.length - 1);
System.out.println("Value found at position: " + result);
}
//Definition of method "trinarySearch()"
public static int ternarySearch(int data[], int target,
int front, int end)
{
//Declare the variable "res"
int res;
//Check whether the front is greater than end
if (front > end)
//Assign "-1" to "res"
res = -1;
else {
/*Divide the first half of the array by "3"*/
int firstHalf = (2 * front + end) / 3;
/*Divide the second half of the array by "3"*/
int secondHalf = (front + 2 * end) / 3;
/*Check whether the given number in the first half of the array*/
if (target == data[firstHalf])
;&#x...
Want to see the full answer?
Check out a sample textbook solutionChapter 11 Solutions
Java : Introduction To Prob...-MyProgrammingLab
- Finish the splitOdd10 using recursion to determine if the elements of an array can be split into two groups such that the sum of one group is a non-zero multiple of 10 and the sum of the other group is oddarrow_forwardThere is an error in the following recursive method, which implements a binary search. Find the error and indicate how to fix the code so that the method will work correctly.arrow_forwarduse c++ programming language. Write a recursive function that take input an array; its size, and reverse the arrays without using theother memory allocation. use this function prototype: int *reverse(int *arr, int end,int start){ }arrow_forward
- the language is C++, please take a screenshot of your output. (Recursive binary search) The binary search algorithm given in this chapter is nonrecursive. Write and implement a recursive version of the binary search algorithm. Also, write a version of the sequential search algorithm that can be applied to sorted lists. Add this operation to the class orderedArrayListType for array-based lists. Moreover, write a test program to test your algorithm.arrow_forwardThe following function recursively locates the index of the target value in the array passed in as numbers, returning -1 if the target is not found. Some parts have been removed. fill in the missing code. def find_val(target, numbers, start_index=0): if : _______#base cases return ______ elif :_____ return _____ else:______ #recursive case return ______arrow_forwardWrite a recursive function that find the minimum element in an array of integers.int findMin(int a[], int size);arrow_forward
- Write a RECURSIVE function, without using any loops, that prints the contents of a matrix with 3 columns. The function should take the matrix and the number of rows as arguments. void print_matrix(int arr[][3], int num_rows);arrow_forwardLet A[1...n,1...n] be a 2D array of positive integers with n rows and n columns. Let ArraySum(a,b,c,d) be the sum of all the elements of A[a...b, c...d] (i.e., the submatrix of A with rows a to b and columns c to d). Write down a recursive formula for ArraySum(a,b,c,d) and analyze the time complexity of the corresponding recursive algorithm. You must write a formal recursive formula including the base case and general recursive step.arrow_forwardWhat is the recursive definition that gives the length of the longest bitonic subsequences in an array? It would be in the form lbs(A)....arrow_forward
- Suppose that intArray is an array of integers, and length specifies the number of elements in intArray. Also, suppose that low and high are two integers such that 0 <= low < length, 0 <= high < length, and low < high. That is, low and high are two indices in intArray. Write a recursive definition that reverses the elements in intArray between low and high.arrow_forwardWrite a Program to implement binary search using recursion.arrow_forwardOutline, but do not implement, a recursive solution for sorting an array of numbers. Hint: First sort the subarray without the initial element.arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning