Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
4th Edition
ISBN: 9780134787961
Author: Tony Gaddis, Godfrey Muganda
Publisher: PEARSON
Expert Solution & Answer
Book Icon
Chapter 16.3, Problem 16.16CP

Explanation of Solution

The following algorithm is presented on checkpoint 16.15CP and its worst case complexity is discussed as follows,

Pseudocode:

Input: Two integer arrays “a[]” and “b[]” of size “n”.

Size of Input: The size specifies the number “n” of array entries.

Output: The condition is “true” of each element of “a[]” is also an element of “b[]” otherwise, it display “false”.

The following is the pseudocode for given data,

  Boolean Contains =true

  int i=0

  While Contains && i<n do

  Contains=Linearsearch(a[i],b)

  i++;

  End while

  Print Contains

Explanation:

  • In the given code, set the Boolean variable “Contains” is equal to “true”.
  • Execute the while loop if the condition is true,
    • The condition must be “i” less than “n” and the variable “Contains” must be “true”.
    • If it is true, then it calls for Linearsearch() function.
    • Increment the value of “i”.
  • Thus, the while loop is executed until the condition satisfies and then finally it print the Boolean value either “True” or “False”.

Pseudocode for LinearSearch:

Input: An integer “X” and an array “b[]” of size “n”.

Size of input: The size specifies the number “n” of array entries.

Output: It returns true if “X” contains in “b[]”, otherwise “false”.

The following is the pseudocode for linear search,

Boolean LinSearch(int x, int[] b)

Begin

  int k=0;

  Boolean found =false;

  While !found && k<n do

  if(X==b[k])

  found =true;

  else   

  K++

  End If

  End While

Return found

End LinSearch

Explanation:

  • The given code performs the linear search algorithm concept which process to find an item in an array...

Blurred answer
Students have asked these similar questions
Imagine that you have a problem P that you know is N P-complete. For this problem you have two algorithms to solve it. For each algorithm, some problem instances of P run in polynomial time and others run in exponential time (there are lots of heuristic-based algorithms for real N P-complete problems with this behavior). You can’t tell beforehand for any given problem instance whether it will run in polynomial or exponential time on either algorithm. However, you do know that for every problem instance, at least one of the two algorithms will solve it in polynomial time. (a) What should you do? (b) What is the running time of your solution? 564 Chap. 17 Limits to Computation (c) What does it say about the question of P = N P if the conditions described in this problem existed?
I needed the algorithm for G whose length is even. You can read it in the last line of the question. I don't know where the divisible by 5 part came from. Please solve the question again.
Consider the problem of counting, in a given text, the number of substrings that start with an A and end with a B. For example, there are four such substrings in CABAAXBYA.a. Design a brute-force algorithm for this problem and determine its efficiency class.b. Design a more efficient algorithm for this problem with complexity O (n)

Chapter 16 Solutions

Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)

Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education