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
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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!!!

Add a comment
Know the answer?
Add Answer to:
cis 112 (java reqire to use JGRASP) Searching and Sorting Assignment This is a non-object oriented...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using Arrays with Sorting and Searching Algorithms 1) This program has six required outputs and involves...

    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 ...

    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,...

  • Benchmark Searching and Sorting Write a program that has an array of at least 20 strings...

    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...

    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...

  • C++ Sorting and Searching 1. Mark the following statements as true or false. a. A sequential...

    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...

  • Stuck on this computer science assignment Write a program that demonstrates binary searching through an array...

    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...

  • In C only Please! This lab is to write a program that will sort an array...

    In C only Please! This lab is to write a program that will sort an array of structs. Use the functions.h header file with your program. Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like though it is recommended that you use bubble sort, insertion sort, or selection sort...

  • Using Java In this assignment we are working with arrays. You have to ask the user...

    Using Java In this assignment we are working with arrays. You have to ask the user to enter the size of the integer array to be declared and then initialize this array randomly. Then use a menu to select from the following Modify the elements of the array. At any point in the program, if you select this option you are modifying the elements of the array randomly. Print the items in the array, one to a line with their...

  • Using Java programming language Your assignment is to implement a recursive reverse sorting algorithm. It should...

    Using Java programming language Your assignment is to implement a recursive reverse sorting algorithm. It should meet the following requirements: 1. The program shall graphically prompt the user for a file. 2. The program shall read the selected file which will contain 1 integer per line. 3. The program shall sort the values it reads from the file from largest to smallest. 4. The program shall write the values to an output file from largest to smallest in the same...

  • This program is in C++ Write a program that validates charge account numbers read in from...

    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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT