Write a Java program to implement sequential search. Create a String array in main method and initialize it with 5 different names. Now take any name from the user, find whether that name is present in that array or not, and print the position of that name in the array if found. Write a search method to sequentially search the name from the array. Invoke the search method from the main method. The array and the name to be searched should be passed into the search method as parameters. Sample input & output: [Let’s assume that the 5 names are Diana, Becker, Ann, Eve, and Cory] Enter the name: Cory The name is found at 5 th position. Enter the name: Donald The name is not found. Now write a Java program to implement binary search for the above problem. [Hint: You can use equals() and compareTo() methods of String class.]
Write a Java
and initialize it with 5 different names. Now take any name from the user, find whether that
name is present in that array or not, and print the position of that name in the array if found.
Write a search method to sequentially search the name from the array. Invoke the search
method from the main method. The array and the name to be searched should be passed into
the search method as parameters.
Sample input & output:
[Let’s assume that the 5 names are Diana, Becker, Ann, Eve, and Cory]
Enter the name: Cory
The name is found at 5
th position.
Enter the name: Donald
The name is not found.
Now write a Java program to implement binary search for the above problem.
[Hint: You can use equals() and compareTo() methods of String class.]
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 3 images