1 answer

Cis 112 (java reqire to use JGRASP) Searching and Sorting Assignment This is a non-object oriented...

Question:

cis 112 (java reqire to use JGRASP)
Searching and Sorting Assignment This is a non-object oriented assignment. This assignment is due at the start of class on Ju
Searching and Sorting Assignment This is a non-object oriented assignment. This assignment is due at the start of class on July 16, Create a program that populates and array with 100 random integers between 0 and 99. Then print out the array. Then prompt the user for a number, and do a sequential search on the unsorted array and return whether or not the number was in the array. Then sort the array using the sort method of your choosing. Make sure you document your method: Print out sufficient local variables for each iteration of the sorting algorithm so that you can see how it is working Print out the contents of the array to show the sort worked. Then prompt the user for another number. The do a binary search on the sorted array to return whether or not the number is in the array Remember to adequately document your program. You can copy code from the Geeks website, but you must add the additional things mentioned above, plus comments!!

Answers

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

SearchArray C:\Program Files\Java\jdk1.8.0_111\bin\java.exe ... 78 79 2 80 11 2 13 22 74 0 24 32 47 54 7 13 37 67 41 42 8 3

-------------------------------------------------------------------------------------------------------------------------------------------------------------


COMMENT DOWN FOR ANY QUESTIONS...........
HIT A THUMBS UP IF YOU DO LIKE IT!!!

.

Similar Solved Questions

1 answer
Case Study, Chapter 14, Perioperative Care April a partial abdominal hysterectomy (ovaries and fallopian tubes remain)....
Case Study, Chapter 14, Perioperative Care April a partial abdominal hysterectomy (ovaries and fallopian tubes remain). The client has an 18- gauge IV site per right forearm (FA) with DSRL at 125 mL/hr infusing per IV infusion pump. West, a 44-year-old female client, arrived to the medical-surgical ...
1 answer
How many ways can 9 keys be arranged on a circular key ring
How many ways can 9 keys be arranged on a circular key ring?...
1 answer
Hi! I'm really not sure how to go about this question, can anyone please help? Thanks!...
Hi! I'm really not sure how to go about this question, can anyone please help? Thanks! In each of these eight pictures, there is a pair of charges. The charge on the left is always a +2Q charge. The charge on the right is either -Q, -3Q, +Q, or +3Q, in some order in pictures A-D and in some orde...
1 answer
On the following pages are two problems. You have a molecular formula, an IR, a 1H...
On the following pages are two problems. You have a molecular formula, an IR, a 1H NMR and a 13C NMR. For each problem: a. (1 pt) Calculate the index of hydrogen deficiency. b. (4 pts) Propose a structure that is consistent with the spectra. c. (1 pt) Indicate the important peaks in the IR and what ...
1 answer
I'm stuck in part (b) and part (c). If you could please explain both I would...
I'm stuck in part (b) and part (c). If you could please explain both I would appreciate it. thank you in advance :) AGID a)Calculate the pH of a buffer solution which is 0.30 M CH3CH2COH and 0.20 M NACH, CH, CO2 b) Calculate the pH after 0.05 M NaOH is added to LOL of the solution in part a. G)...
1 answer
Match the schnitions to the terms Weighted Average Compatibility Principle Costane Principi First-n, Frst-out Net Realizable...
Match the schnitions to the terms Weighted Average Compatibility Principle Costane Principi First-n, Frst-out Net Realizable Control Principle Flexibility Principle Gross Margin Last in Last-out Perpetual Syslem entory Periodic inventory System Match con bons above to the hom bew. Free on board poin...
1 answer
Laser cavity in Q-switching consist of: Pumping source O Partial reflection mirror Electrical source ( Gain...
Laser cavity in Q-switching consist of: Pumping source O Partial reflection mirror Electrical source ( Gain medium O Total reflection mirror Shutter Laser pumping: Flash light. DC battery Gas engine. Laser Fire 0 Water steam RF waves 1 Chemical reaction....
1 answer
The inputs and outputs for Yogi Soft Drinks Inc. are as shown in the table below....
The inputs and outputs for Yogi Soft Drinks Inc. are as shown in the table below. a. If capital costs $30 per unit and labour costs $14 per unit, fill in the blanks in the table. Round your "LRAC" answers to 2 decimal places. INPUTS Capital T Labour Total Cost LRAC Output 100 200 300 400 500...
1 answer
Ned's New Wave Barber Shop specializes in modern unisex haircuts. The only service available at Ned's...
Ned's New Wave Barber Shop specializes in modern unisex haircuts. The only service available at Ned's is a “20-minute” haircut for which the customer is charged $10.The shop has five (5) barbers. (Ned does not work in the shop and, as owner/entrepreneur, he takes no salary.) Each...
1 answer
The Foundational 15 Please provide equation/formula so i know how to solve for. Some screenshot have...
The Foundational 15 Please provide equation/formula so i know how to solve for. Some screenshot have answer in it but could be wrong. PLEASE IGNORE ANSWER IN THE PICTURE. Please provide equation/formula so i know how to solve for. Some screenshot have answer in it but could be wrong. PLEASE IG...
1 answer
We were unable to transcribe this imageWe were unable to transcribe this image(g) the distribution of...
We were unable to transcribe this imageWe were unable to transcribe this image(g) the distribution of Σ c., where ci are some constants satisfying Σ 19-0...
1 answer
Capacitor question LOCULU. SHW: Chapter 18 problems 43-51, 58-61, 99* (99* Take V = 12 volts...
capacitor question LOCULU. SHW: Chapter 18 problems 43-51, 58-61, 99* (99* Take V = 12 volts with C1 = 2 uF and C2 = 6uF.) A. Connect in series: find Ceq, the charge on each and the voltage on each. B. Connect in parallel: find Q on each and Ceq....
1 answer
Locust Systems has the following information for the most recent year of operations. The firm uses...
Locust Systems has the following information for the most recent year of operations. The firm uses a manufacturing overhead rate of 167% of DL cost. Direct materials beginning inventory $23,800 Direct materials ending inventory $29,500 Selling and administrative costs $75,000 Beginn...
1 answer
Classify each characteristic according to whether it describes positive economics or normative economics. Positive economics Normative...
Classify each characteristic according to whether it describes positive economics or normative economics. Positive economics Normative economics Answer Bank describes current economic events involves recommendations or arguments about what should be is concerned with making predictions about how the...
1 answer
Can someone solve this with neat writing!! And a free body diagram as well thank you!...
Can someone solve this with neat writing!! And a free body diagram as well thank you! h - 2 m Oil SG =0.8 m Question 1 (20 points): Calculate the value of FH needed to maintain the gate as shown in Figure 1 where a layer of oil rests on top of water. The width of the gate is b=6 m into the page. ...