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 (8th Edition)
- Program 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_forwardFast pleasearrow_forward1. Rewrite the Bubble sort to use recursion. 2. Use the time(0) function to determine how many seconds it takes to sort a vector using the recursive method. 3. Use the time(0) function to determine how many seconds it takes to sort a vector using the non-recursive method described in the videos. Run the sort test on vectors containing random integers. You should sort vectors of the following sizes: 100 elements, 1000 elements, 5,000 elements, 10,000 elements and 50,000 elements. Was the recursive method able to work on vectors of each of those sizes? If not, explain why it errored. 4. Create a class that will store a vector of planets (See the Planet assignment). Your class will have functions to add a planet, delete a planet and sort the planets. The class must utilize the insertion sort.arrow_forward
- 1. Rewrite the Bubble sort to use recursion. 2. Use the time(0) function to determine how many seconds it takes to sort a vector using the recursive method. 3. Use the time(0) function to determine how many seconds it takes to sort a vector using the non-recursive method described in the videos. Run the sort test on vectors containing random integers. You should sort vectors of the following sizes: 100 elements, 1000 elements, 5,000 elements, 10,000 elements and 50,000 elements. Was the recursive method able to work on vectors of each of those sizes? If not, explain why it errored.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_forwardWrite Java Program to Sort an array of element by getting values from user Remove duplicate elements from a sorted array Reverse the contents inside an array Search for an element inside the array using linear searcharrow_forward
- write a code in java (using recursion)arrow_forwardWrite a program in any language you know to calculate the GCD for a sequence of numbers, let us say 4 numbers, kept in an array. You need to provide two solutions: 1) Iterative solution 2) Recursive solution. Test your code using the following numbers: 1048,1024,1480, 2640. The GCD of these numbers is 8.arrow_forwardhaskell is the programming languagearrow_forward
- java programarrow_forwardWrite a java program that fills in an array with up to 10 characters and then searches the array for a character entered by the user ( by using Binary Search Implemented by recursion ).arrow_forwardDevelop a Java function that returns the index of a value in a sorted array. It should return -1 if the value does not exist in the array.arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning