Java: An Introduction to Problem Solving and Programming (8th Edition)
Java: An Introduction to Problem Solving and Programming (8th Edition)
8th Edition
ISBN: 9780134462035
Author: Walter Savitch
Publisher: PEARSON
bartleby

Concept explainers

Expert Solution & Answer
Book Icon
Chapter 11, Problem 8E

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...

Blurred answer
Students have asked these similar questions
In JAVA Recursive Array Search.  Define and test a recursive method for a sequential search of an integer array. The method should return the index of the searchKey in the array, or return -1 if the searchKey is not found.
If the search element (key) is the middle element in the array of size 10, the recursive method for binary search will be called 10 times More than twice exactly twice only once
The solution must be recursive. inputAndPrintReverse:Inputs integers from the user until the user enters 0, then prints the integers in reverse order. For this method, you may NOT use an array or any type of array structure, in other words, you may not use any structure to store the user input.

Chapter 11 Solutions

Java: An Introduction to Problem Solving and Programming (8th Edition)

Ch. 11.2 - What Java statement will sort the following array,...Ch. 11.2 - How would you change the class MergeSort so that...Ch. 11.2 - How would you change the class MergeSort so that...Ch. 11.2 - If a value in an array of base type int occurs...Ch. 11.3 - Convert the following event handler to use the...Ch. 11 - What output will be produced by the following...Ch. 11 - What output will be produced by the following...Ch. 11 - Write a recursive method that will compute the...Ch. 11 - Write a recursive method that will compute the sum...Ch. 11 - Complete a recursive definition of the following...Ch. 11 - Write a recursive method that will compute the sum...Ch. 11 - Write a recursive method that will find and return...Ch. 11 - Prob. 8ECh. 11 - Write a recursive method that will compute...Ch. 11 - Suppose we want to compute the amount of money in...Ch. 11 - Prob. 11ECh. 11 - Write a recursive method that will count the...Ch. 11 - Write a recursive method that will remove all the...Ch. 11 - Write a recursive method that will duplicate each...Ch. 11 - Write a recursive method that will reverse the...Ch. 11 - Write a static recursive method that returns the...Ch. 11 - Write a static recursive method that returns the...Ch. 11 - One of the most common examples of recursion is an...Ch. 11 - A common example of a recursive formula is one to...Ch. 11 - A palindrome is a string that reads the same...Ch. 11 - A geometric progression is defined as the product...Ch. 11 - The Fibonacci sequence occurs frequently in nature...Ch. 11 - Prob. 4PPCh. 11 - Once upon a time in a kingdom far away, the king...Ch. 11 - There are n people in a room, where n is an...Ch. 11 - Prob. 7PPCh. 11 - Prob. 10PPCh. 11 - Prob. 12PP
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning