Write a Java program that will
Now take that code and alter it to have two lists.
Both lists randomize between 1 and 50.
While traversing one list, use the linear search to compare each element of list 1 to list 2.
Display the element from the first list and whether it matched the element from the second list.
Perform a binary search with the same list within the same program.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// Main.java
public class Main {
// method to generate an array of given size, initialized with random
// numbers between 1 and 50
static int[] generateRandomList(int size) {
int arr[] = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = (int) (Math.random() * 50) + 1;
}
return arr;
}
// method to perform linear search on an array for a given element, returns
// index if found, else -1
static int linearSearch(int element, int[] array) {
for (int i = 0; i < array.length; i++) {
if (array[i] == element) {
// found,
return i;
}
}
// not found
return -1;
}
// method to print elements of an array space separated
static void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
}
// method to sort an array
static void sortArray(int[] array) {
// using bubble sort algorithm
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length - 1; j++) {
// comparing element at index j to element at index j+1
if (array[j] > array[j + 1]) {
// swapping elements at j and j+1
int t = array[j];
array[j] = array[j + 1];
array[j + 1] = t;
}
}
}
}
// method to perform binary search on an array for an element
static int binarySearch(int element, int[] array) {
int low = 0, high = array.length - 1, mid;
// looping as long as low is less than or equal to high
while (low <= high) {
// finding middle index
mid = (low + high) / 2;
// comparing item at middle with target element
if (array[mid] == element) {
// found, returning index
return mid;
} else if (array[mid] > element) {
// will search left half on next iteration
high = mid - 1;
} else {
// will search right half on next iteration
low = mid + 1;
}
}
return -1; // not found
}
public static void main(String[] args) {
// generating an array of 10 elements
int arr1[] = generateRandomList(10);
// generating a search item
int search_item = (int) (Math.random() * 50) + 1;
// flag denoting if item is found
boolean found = false;
System.out.println("list1: ");
// displaying elements before searching
printArray(arr1);
// sequentially searching for search_item
System.out.println("Searching for " + search_item);
for (int i = 0; i < arr1.length; i++) {
// displaying current element and index
System.out.println("element: " + arr1[i] + ", index: " + i);
if (arr1[i] == search_item) {
// found, setting flag to true and exiting loop
found = true;
break;
}
}
// displaying if element is found or not
if (found) {
System.out.println("Found!");
} else {
System.out.println("Not found");
}
// generating another array of same size (part 2)
int arr2[] = generateRandomList(10);
// displaying elements
System.out.println("\nlist2: ");
printArray(arr2);
System.out
.println("Searching if each item in list1 is present in list2");
// looping through each item in list1, checking if it is present in
// list2
for (int i = 0; i < arr1.length; i++) {
// using linearSearch method to search for arr1[i] in arr2
int index = linearSearch(arr1[i], arr2);
if (index == -1) {
System.out.println(arr1[i] + " is not present in list2");
} else {
System.out.println(arr1[i] + " is present at index " + index
+ " in list2");
}
}
// now before we perform binary search, the array needs to be sorted. so
// sorting arr1
sortArray(arr1);
//displaying sorted list
System.out.println("\nlist1 sorted: ");
printArray(arr1);
//performing binary search and displaying results
System.out.println("Searching for " + search_item + " in list1");
int index = binarySearch(search_item, arr1);
if (index == -1) {
System.out.println(search_item + " is not present in list1");
} else {
System.out.println(search_item + " is present at index " + index
+ " in list1");
}
}
}
/*OUTPUT*/
list1:
48 24 16 8 44 9 43 45 34 43
Searching for 8
element: 48, index: 0
element: 24, index: 1
element: 16, index: 2
element: 8, index: 3
Found!
list2:
14 14 11 39 28 33 16 36 32 21
Searching if each item in list1 is present in list2
48 is not present in list2
24 is not present in list2
16 is present at index 6 in list2
8 is not present in list2
44 is not present in list2
9 is not present in list2
43 is not present in list2
45 is not present in list2
34 is not present in list2
43 is not present in list2
list1 sorted:
8 9 16 24 34 43 43 44 45 48
Searching for 8 in list1
8 is present at index 0 in list1
Write a Java program that will create a random list (array) randomize a search item to be...
Program with generic merge sort and binary search
method help. The programming language I'm using is Java.
This program should show understanding generic merge sort methods and generic binary search methods in java. The execution should include at least 5 found items including one from the first three items in the sorted array and one from the last three items in the sorted array as well as at least two items not found Create a generic merge sort method that...
(Recursive Binary Search) Write a recursive method recursiveBinarySearch to perform a binary search of an array. The method should receive the search key, starting index and ending index as arguments. If the search key is found, return its index in the array. If the search key is not found, return –1. (NOTE: Complete the recursiveBinarySearch method in the BinaryArray class). java
Write a C++ function binsearch that carries out the binary search algorithm on a sorted array of integers. Your function takes as parameters an array of integers (sorted in increasing order), the size of the array, and a target integer to search for. The function returns the index of the target in the array, and returns -1 if the target is not in the array. The file main1.txt on the webpage contains code that generates a specific array of integers...
Unit 4 Assignment 5: Coding Project using C# Binary Search Scenario Use the same integer array named partNumbers that you used for task 3. Sort the array in ascending order. 1065, 1095, 1075, 1055, 1056, 1090, 1098, 1088, 1097, and 1078. Java: use Array.sort() C#: use Array.Sort() PHP: use sort() Write code that asks the user to enter two part numbers. For C#, use console input. Implement a binary tree search function called binarySearch() to search the array for the...
Please complete this code for java
Lab Write a program as follows: Create an array with 10 elements. The array should contain numbers generated randomly between 1 and 100 Ask the user to enter any number - Search the array to see if the user entered number is in the array If the user-entered number is in the array, print a 'match found' message. Also print at which index the match was found, else print a 'match not found' message...
Write Java program to compare time consumed by linear search and binary search to search for non-exit element in arrays of length 1000, 100000,500000,1000000. Hints: - Use Random class, method nextInt(a.length) to generate random numbers. - Select an element that is greater than a.length. - Use Arrays.sort(a) to sort the array before using binarySearch.
Write a Java program that creates and manipulates a directory of names, telephone numbers. The following information will be stored for each person in the directory: - Name (Last, First) - Home telephone number You should keep the entire collection ordered by key value (the combination of last and first names). Your program should be able to perform the following basic functions: - Search and display the contents of a particular entry - Display the entire directory - Delete an...
Write a Java program with a single-dimension array that holds 11 integer numbers and sort the array using a bubble sort. Next, identify the median value of the 11 integers. Here are the steps your program must accomplish. algorithm (either flowchart or pseudocode) that you will use to write the program Step 1. Create an Place the algorithm in a Word document. 6. Ste the Step 2. Code the program in Eclipse and ensure the following steps are accomplished. 1....
team, create a Java program that adds or removes entries from a linked list based on user input. You can use the index of the entry as the key to identify the record for addition or deletion. The program can be as simple as you like but must add an entry to the list based on the user input and also must delete the proper item in the list based on the index entered by the user Display your list...
Please write a Java program that does the following: Create an array of 100 integers. Store 100 random integers (between 1 and 100) in the array. Print out the elements of the array. Sort the array in ascending order. Print out the sorted array. Prompt the user to enter a number between 1 and 100. Search the array for that number and then display "Found" or "Not Found" message. Display each number from 1 to 100 and the number of...