Question

In: Computer Science

(JAVA) Create a program that takes in 15 numbers in sorted order from the console and...

(JAVA)

Create a program that takes in 15 numbers in sorted order from the console and stores them in a 1D array of size 15.

Next, prompt the user for a number to search for in the array (target).

Then, print the array.

Next, search the array using a linear search – printing out each of the indices (or “indexes”) that are being examined until the algorithm either finds the target or doesn’t.

Then, do the same thing for a binary search.

The program should behave like the samples below:

Sample 1

slot 0: 0

slot 1: 1

slot 2: 2

slot 3: 3

slot 4: 4

slot 5: 5

slot 6: 6

slot 7: 7

slot 8: 8

slot 9: 9

slot 10: 10

slot 11: 11

slot 12: 12

slot 13: 13

slot 14: 14

Enter a target: 15

0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

7 11 13 14

Sample 2

slot 0: 3

slot 1: 11

slot 2: 45

slot 3: 57

slot 4: 81

slot 5: 125

slot 6: 129

slot 7: 311

slot 8: 333

slot 9: 361

slot 10: 402

slot 11: 412

slot 12: 475

slot 13: 499

slot 14: 501

Enter a target: 402

3|11|45|57|81|125|129|311|333|361|402|412|475|499|501|

0 1 2 3 4 5 6 7 8 9 10

7 11 9 10

Solutions

Expert Solution

import java.util.*;
public class Main{
public static void linear_search(int[] a,int t){
for(int i=0;i<15;i++){//traverse the array
if(t==a[i]){
System.out.print(i+" ");//check if element is equal to target
break;
}
else
System.out.print(i+" ");
}
}
public static void binary_search(int[] a,int t){
int l=0,h=14;
while(l<=h){//loop till l is less than or equal to h
int m=l+(h-l)/2;
if(a[m]==t)//compare element and break loop if equal
{
System.out.print(m+" ");
break;
}
if(a[m]<t){//update l if element is less than target
System.out.print(m+" ");
l=m+1;
}
else{
System.out.print(m+ " ");
h=m-1;//update h if element is greater than target
}
}
  
}
public static void main(String []args){
Scanner inp=new Scanner(System.in);
int a[]=new int[15];
for(int i=0;i<15;i++){
System.out.print("slot "+i+":");
a[i]=inp.nextInt();//read array elements from user

}
System.out.print("Enter a target");
int t=inp.nextInt();
for(int i=0;i<15;i++){
System.out.print(a[i]+"|");//print array
}
System.out.println();
linear_search(a,t);//call functions
System.out.println();
binary_search(a,t);
}
}

Screenshots:

The screenshots are attached below for reference.

Please follow them for output.

Please upvote my answer. Thank you.


Related Solutions

write a java program that takes three numbers from the user and print the greatest number...
write a java program that takes three numbers from the user and print the greatest number (using if-statement). sample output: input the first number:35 input the second number:28 input the third number:87 the greatest number:87
Create a program that creates a sorted list from a data file. The program will prompt...
Create a program that creates a sorted list from a data file. The program will prompt the user for the name of the data file. Create a class object called group that contains a First Name, Last Name, and Age. Your main() function should declare an array of up to 20 group objects, and load each line from the input file into an object in the array. The group class should have the following private data elements: first name ,last...
in Java, write a program that takes an input of 4 numbers and splits them into...
in Java, write a program that takes an input of 4 numbers and splits them into 4 separate lines. EXAMPLE: so if input is 1994 the output should be 1 9 9 4
Create a database to hold a collection of numbers to be searched and sorted: 1) Create...
Create a database to hold a collection of numbers to be searched and sorted: 1) Create a main class and a database class 2) Collect 5 random numbers from the user in the main class and store in an array. (not in ascending or descending order) 3) Create an instance of the database class in your main class and store the numbers array in the database using its constructor. 4) In the Database class, create a method that performs a...
Using JAVA: Create a simple numeric code class SimpleCode that takes a set of numbers and...
Using JAVA: Create a simple numeric code class SimpleCode that takes a set of numbers and turns them into words and sentences. The code should use the 0 to represent a space between words and 00 to represent a period at the end of a sentence. The 26 letters of the alphabet should be represented in reverse order. In other words, Z is 26 and A is one. In your final product, only the first letter of a sentence should...
JAVA PROGRAMMING 1)BuildLists: Write a program to create an ArrayList<Integer>. Fill it with numbers from 1...
JAVA PROGRAMMING 1)BuildLists: Write a program to create an ArrayList<Integer>. Fill it with numbers from 1 to 1000. Then remove every even number. Then remove every multiple of 3 remaining Then remove every multiple of 5 Then remove every multiple of 7 Then sum the array, and print.
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1....
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1. Write an abstract Java class called Shape which has only one abstract method named getArea(); 2. Write a Java class called Rectangle which extends Shape and has two data membersnamed width and height.The Rectangle should have all get/set methods, the toString method, and implement the abstract method getArea()it gets from class Shape. 3. Write the driver code tat tests the classes and methods you...
URGENT!!! DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps:...
URGENT!!! DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1. Write an abstract Java class called Shape which has only one abstract method named getArea(); 2. Write a Java class called Rectangle which extends Shape and has two data membersnamed width and height.The Rectangle should have all get/set methods, the toString method, and implement the abstract method getArea()it gets from class Shape. 3. Write the driver code tat tests the classes and methods...
Language is Java Design and write a Java console program to estimate the number of syllables...
Language is Java Design and write a Java console program to estimate the number of syllables in an English word. Assume that the number of syllables is determined by vowels as follows. Each sequence of adjacent vowels (a, e, i, o, u, or y), except for a terminal e, is a syllable. However, the minimum number of syllables in an English word is one. The program should prompt for a word and respond with the estimated number of syllables in...
Write a program which accepts a sequence of comma-separated numbers from console and generate a list...
Write a program which accepts a sequence of comma-separated numbers from console and generate a list and a tuple which contains every number. Suppose the following input is supplied to the program: 34, 67, 55, 33, 12, 98. Then, the output should be: ['34', '67', '55', '33', '12', '98'] and ('34',67', '55', '33', '12', '98').
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT