Java: Write an application that has an array of at least 20 integers. It should call a method that uses the sequential search algorithm to locate one of the values. The method should keep a count of the number of comparisons it makes until it finds the value. Then the program should call another method that uses the binary search algorithm to locate the same value. It should also keep count of the number of comparisons it makes. Display these values on the screen.
a. Create an array of 20 random integers.
b. Use the Bubble Sort Algorithm to sort this array in an ascending order.
c. Make a copy of this sorted array for use in the Binary Search method, using the array copy method called CopyArrayExample.java.
e. Print out the array before starting the search so that you will know which integer you want the two search methods to search for.
f. Run the Sequential Search algorithm and report the number of comparisons.
g. Run the Binary Search algorithm and report the number of
comparisons.
CopyArrayExample.java:
public class CopyArrayExample {
public static void main(String args[]){
int a[]={10,20,30,40,50};
int b[]=new int[a.length];
//copying one array to
another
for(int
i=0;i<a.length;++i){
b[i]=a[i];
}
//printing array
for(int
i=0;i<b.length;++i){
System.out.print(b[i]+" ");
}
}
}
import java.util.Random;
import java.util.Scanner;
public class CopyArrayExample {
// sorting they array in ascending order using bubble
sort
public static void bubbleSort(int array[]) {
int i, j, temp;
int n = array.length;
for (i = 0; i < n - 1; ++i) {
for (j = 0; j < n - 1 - i; ++j) {
if (array[j] > array[j + 1]) {
temp = array[j + 1];
array[j + 1] = array[j];
array[j] = temp;
}
}
}
}
// performs binary search on the array and returns
the number of
// comparisions
public static int binarySearch(int arr[], int l, int
r, int x, int comp) {
if (r >= l) {
comp++;
int mid = l + (r
- l) / 2;
if (arr[mid] ==
x) {
return comp;
}
if (arr[mid]
< x)
return binarySearch(arr, mid + 1, r, x,
comp);
return
binarySearch(arr, l, mid - 1, x, comp);
}
return comp;
}
public static void main(String args[]) {
int a[] = new int[20];
int b[] = new int[20];
Random r = new Random();
// copying one array to
another
for (int i = 0; i < a.length;
++i) {
a[i] =
r.nextInt(1000);
// copying
values into array
b[i] =
a[i];
}
bubbleSort(a);
// printing array
for (int i = 0; i < a.length;
++i) {
System.out.print(a[i] + " ");
}
Scanner sc = new
Scanner(System.in);
// reading element to search in the
array
System.out.println("\nEnter element
to search: ");
int ele = sc.nextInt();
// calling binary and linear
search
System.out.println(binarySearch(a,
0, a.length, ele, 0) + " comparisions required to find " +
ele
+ " using binary search");
System.out.println(linearSearch(b,
ele) + " comparisions required to find " + ele + " using binary
search");
}
// performs linear search on the array and returns
number of comparisons
// required
private static int linearSearch(int[] aA, int aEle)
{
for (int i = 0; i < aA.length;
i++) {
if (aA[i] ==
aEle)
return i + 1;
}
return -1;
}
}
Java: Write an application that has an array of at least 20 integers. It should call...
Benchmark Searching and Sorting Write a program that has an array of at least 20 strings that you will have your user enter. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that uses the binary search algorithm to locate the same value. It should also keep...
Looking for a solution done in a Flowgorithm flowchart please. Design an application that has an array of at least 20 integers. It should call a module that uses the sequential search algorithm to locate one of the values. The module should keep a count of the number of comparisons it makes until it finds the value. Then the program should call another module that uses the binary search algorithm to locate the same value. It should also keep a...
The purpose of this assignment is to familiarize you with sort algorithms. Problem Description is as follows: 8. Search Benchmarks Write a program that has at least 20 250 integers stored in an array in ascending order. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that...
C++ Write a program that uses an arrays of at least 20 string . It should call a function that uses the linear search algorithm to locate one of the name. Read list of name from an input file call "StudentName.txt" The function should keep a count of the numbers of comparisons it makes until it finds the value. The program then should call a function that uses the binary search algorithm to locate the same value. It should also...
The purpose of this assignment is to familiarize you with sort algorithms. Problem Description is as follows: 8. Search Benchmarks Write a program that has at least 20 250 integers stored in an array in ascending order. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that...
in JAVA please thanks. (need answer in a few hours !!)
Exercise #1: Design and implement a program (name it LinearBinarySearch) to implement and test the linear and binary search algorithm discussed in the lecture slides. Define method LinearSearch() to implement linear search of an array of integers. Modify the algorithm implementation to count number of comparisons it takes to find a target value (if exist) in the array. Define method BinarySearch() to implement binary search of an array of...
HOME WORK 9. Sorting Benchmarks Write a program that uses two identical arrays of at least 20 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in ascending order. The function should keep a count of the number of exchanges it makes. The program then should call a function that uses the selection sort algorithm to sort the other array. It should also keep count of the number of exchanges it...
Write a Java application that implements the recursive Binary
Search alogrithm below:
Write a Java application that implements the recursive Binary Search alogrithm below:/** * Performs the standard binary search using two comparisons * per level. This is a driver that calls the recursive method * @return index where item is found or NOT FOUND if not found */public static <AnyType extends Comparable<? super AnyType>> int binarySearch(AnyType [] a, AnyType x) {return binarySearch(a, x, 0, a.length -1);}/** * Hidden recursive...
How to write a recursive method named lessThanKFirst that receives an array of integers a and an integer k and rearranges the integers in a in such a way that all integers that are smaller than k come before any integers that are greater than or equal to k. As an example, suppose that a and k are: int[] a = {35, 12, 57, 28, 49, 100, 61, 73, 92, 27, 39, 83, 52}; int k = 73; Then, the following...
Language = c++ Write a program to find the number of comparisons using the binary search and sequential search algorithms as follows: o Suppose list is an array of 1000 elements. o Use a random number generator to fill the list. o Use the function insertOrd to initially insert all the elements in the list. o You may use the following function to fill the list: void fill(orderedArrayListType& list) { int seed = 47; int multiplier = 2743; ...