PROGRAM CODE
import java.util.Random;
import java.util.Scanner;
/**
* The following program has an array of integers filled with random
numbers between 0 and 99
* It will first ask user for a number to search in array and
perform sequential search
* Next, it will sort the array using Bubble Sort
* Next, it will ask for another number and perform binary search on
that sorted array to find number
*
* @author Anonymous
* @version 1.0
* @since 2020-07-28
*/
public class SearchArray {
public static void main(String[] args) {
// Declaring an integer array of 100 elements
int[] array = new int[100];
// Populating array with random elements between 0 and 99
for (int i=0; i<array.length; i++) {
array[i] = new Random().nextInt(99);
}
// Printing out the numbers in array using print method
print(array);
// Asking user for a number to search in array sequentially
Scanner input = new Scanner(System.in); // object of Scanner class to take input
System.out.print("\nEnter a number to search for in array:
");
int number = input.nextInt();
// Searching Sequentially for that number in array and if found, show it is found
boolean isFound = false; // Variable to determine whether value has found or not
for (int i=0; i<array.length; i++) {
if(number == array[i]){
System.out.println("The number is in the array.");
isFound = true;
break;
}
}
if(!isFound)
System.out.println("The number is not in the array.");
// Calling sort function and then printing array to see it is sorted or not
sort(array, 100);
System.out.println("\nThe sorted array is");
print(array);
// Performing Binary search by calling method of binarySearch
System.out.print("\nEnter a number to search for in array:
");
number = input.nextInt();
binarySearch(array, 100, number);
}
/**
* This method will print the elements of the array
* @param array The array of integers whose elements are to be
printed
*/
public static void print(int[] array) {
for (int i=0; i<array.length; i++) {
System.out.print(array[i] + " ");
// if(i%10 == 0 && i>0) // After every 10 elements
print new line
// System.out.println();
}
System.out.println();
}
/**
* This method will sort the values of the passed array in ascending
order using Bubble Sort
* @param array array of integers
* @param size size of array
*/
public static void sort(int[] array, int size) {
for (int i=0; i<size; i++) {
for (int j=0; j<size-1; j++) {
if(array[j] > array[j+1]) {
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
// Showing local variables with each iteration of algorithm,
uncomment below to see iteration
//
// System.out.println("Iteration #" + j+1 + " of Pass#" +
i+1);
// print(array);
}
}
}
/**
* This method will perform binary search on array
* @param array The array of integers
* @param size The size of array
* @param number The number to be find in array
*/
public static void binarySearch(int[] array,int size, int number) {
int first = array[0];
int end = array[size - 1];
int middle = (first + end) / 2;
boolean isFound = false;
while (first <= end) {
if(array[middle] == number) {
System.out.println("The number is in the array.");
isFound = true;
break;
}
else if(array[middle] < number) {
first = middle + 1; // proceed to next number
}
else{
end = middle - 1;
}
middle = (first + end) / 2;
}
if(!isFound) {
System.out.println("The number is not in the array.");
}
}
}
SAMPLE OUTPUT
-------------------------------------------------------------------------------------------------------------------------------------------------------------
COMMENT DOWN FOR ANY QUESTIONS...........
HIT A THUMBS UP IF YOU DO LIKE IT!!!
cis 112 (java reqire to use JGRASP) Searching and Sorting Assignment This is a non-object oriented...
Using Arrays with Sorting and Searching Algorithms 1) This program has six required outputs and involves searching and sorting an array of integers. Write a java application that initializes an array with the following numbers, in this order: 23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, and 49. Then display the unsorted values. This is required output #1 of 6 for this program. 2) Using a...
Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...
C++ Sorting and Searching 1. Mark the following statements as true or false. a. A sequential search of a list assumes that the list elements are sorted in ascending order. b. A binary search of a list assumes that the list is sorted. 2. Consider the following list: 63 45 32 98 46 57 28 100 Using a sequential search, how many comparisons are required to determine whether the following items are in the list or not? (Recall that comparisons...
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...
Write an object-oriented C++ program (i.e. a class and a main function to use it), using pointer variables, that program that performs specific searching and sorting exercises of an array of integers. This program has six required outputs. Start by initializing an array with the following integers, in this order: 23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, and 49. Your array input may be hardcoded...
Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...
This program is in C++ Write a program that validates charge account numbers read in from the file Charges.txt following this assignment (A23 Data). Use a selection sort function on the array to sort the numbers then use a binary search to validate the account numbers. Print out the sorted list of account numbers so you can see the valid ones. Prompt the user to enter an account number and the validate that number. If the account number entered is...
LANGUAGE = C i. Write a program that takes int numbers from user until user gives a sentinel value (loop terminating condition). Sort the numbers in ascending order using Insertion sort method. Sorting part should be done in different function named insertionSort(). Your code should count the number of swaps required to sort the numbers. Print the sorted list and total number of swaps that was required to sort those numbers. (4 marks) ii. In this part take another number...
Stuck on this computer science assignment Write a program that demonstrates binary searching through an array of strings and finding specific values in an corresponding parallel double array. In all cases your output should exactly match the provided solution.o. Provided files: Assignment.cpp - starter assignment with function prototypes companies.txt - file for program to read. earnings.txt - file for program to read. Input1.txt Input2.txt - Test these inputs out manually, or use stream redirection Input3.txt - These inputs are based...
LANGUAGE: JAVA Sorting an array: 2. Write a program that asks the user for 5 numbers and stores them in an array called data. Then call a method that accepts the array as an argument, sorts the array in ascending order and prints out the original AND sorted array (Hint: The method does not return anything in this case, so it should be set to void).