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: An Introduction to Problem Solving and Programming (7th Edition)
- write in javaarrow_forwardProgram 2 Construct a BinarySearch class that contains recursive binary search static method. Test your recursive binary search on a sorted array holding 16 integers. Display the contents of the array before and after the sort method is called.arrow_forwardWrite this program using javaarrow_forward
- Fast pleasearrow_forwardGiven a sorted array, write a program named as ProblemB.cpp that creates a Binary Search Tree.Hints:•Write a recursive function to do the followings:a) Get the Middle of the array and make it root of the BST.b) Recursively call a function to do the same for left subarray (0 .. mid-1) and right subarray (mid+1..size-1).i) Get the middle of left half and make it left child of the root created in step a.ii) Get the middle of right half and make it right child of the root created in step a.c) return root.arrow_forwardPythonarrow_forward
- Use the recursion to solve the following problems using Java Programming Language 1. Calculate the sum of an array of n integers. 2. Compute Powers, p(x,n)=xn 3. Revers an array of n integers 4. Calculate the length of string (number of characters) 5. Implement the tail recursion for problems 1 and 2. Please test the program by using 2 different input sets!arrow_forwardWrite a recursive method that returns thelargest integer in an array. Write a test program that prompts the user to enter alist of eight integers and displays the largest element.arrow_forwardWrite a recursive function that returns the largest integer in a list. Write a test program that prompts the user to enter a list of integers and displays the largest element.arrow_forward
- Use the recursion to solve the following problems 1. Calculate the sum of an array of n integers. 2. Compute Powers, p(x,n)=xn 3. Revers an array of n integers 4. Calculate the length of string (number of characters) 5. Implement the tail recursion for problems 1 and 2. Please test the program by using 2 different input sets!arrow_forwardFor a recursive binary search, put the following code in the correct order by dragging each line to the correct place. 1 2 A 5 60 7 8 ⠀ :::: if (right >= left) { if (array [mid] == item) return mid; int binarySearch(int array [], int left, int right, int item) { return binarySearch(array, left, mid - 1, item); if (array [mid] > item) return binary Search(array, mid+ 1, right, item); int mid=1+ (right - left) / 2; return -1; }arrow_forwardThe following recursive method get Number Equal searches the array x of 'n integers for occurrences of the integer val. It returns the number of integers in x that are equal to val. For example, if x contains the 9 integers 1, 2, 4, 4, 5, 6, 7, 8, and 9, then getNumberEqual(x, 9, 4) returns the value 2 because 4 occurs twice in x. public static int getNumberEqual(int x[], int n, int val) { if (n< 0) ( return 0; } else { if (x[n-1) == val) { return getNumberEqual(x, n-1, val) +1; } else { return getNumber Equal(x, n-1, val); } // end if ) // end if } // end get Number Equal Demonstrate that this method is recursive by listing the criteria of a recursive solution and stating how the method meets each criterion.arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning