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 sequential search of the unsorted array, determine and report the relative (i.e. 0,1, 2, 3, 4, etc.) positions of the following numbers in the array (or -1 if not found), and the number of searches required to locate the numbers: 25, 30, 50, 75, and 92. This is required output #2 of 6.
3) Then display the total number of searches for all five numbers. This is required output #3 of 6.
4) Sort the numbers using a recursive quicksort and then display the sorted array. This is required output #4 of 6.
5) Using a binary search of the sorted array, determine and report the 1-relative positions of the following numbers in the array (or -1 if not found), and 2-the number of searches required to locate the numbers: 25, 30, 50, 75, and 92. This is required output #5 of 6.
6) Finally, display the total number of searches for all five numbers. This is required output #6 of 6.
(There are six required sets of output as numbered in the above paragraphs.) Create an object-oriented solution for this assignment. Create a class called SArray (Search Array) that accepts the initial array through the constructor. The class should have a recursive quick sort sorting method that will sort the array. The class should have two methods for searching. One that does a sequential search that accepts as input the integer value that is to be searched for and returns the index in array where the search value is if found, and -1 if not found. The other search method should be a binary search of a sorted array that accepts as input the integer value that is to be searched for and returns the index in the array where the search value is if found, and -1 if not found. Create a toString method to display the contents of the array. The driver/main class could not only pass in the initial array values but call various methods to perform the searches, sorting, and array contents display thus enabling you to display the information in the six items above. Include additional comments as necessary and maintain consistent indentation.
//java program for searching and sorting
import java.util.*;
class SearchingSorting
{
int array[];
int size;
SearchingSorting()
{
array=null;
size=0;
}
SearchingSorting(int a[])
{
size=a.length;
array=new int[size]; //getting
length of a[] passed
for(int i=0;i<size;i++)
//storing each parametered a[] to array[]
array[i]=a[i];
}
void showArray() //method for showing unsorted
array
{
for(int i=0;i<size;i++) //repeat
upto size and display each element
System.out.print("
"+array[i]);
System.out.println();//new line break
}
int linearSearch() //method to unsorted array
search
{
int item;
Scanner s=new
Scanner(System.in);
System.out.println("Enter element
to search : ");
item=s.nextInt();
for(int i=0;i<size;i++)
if(array[i]==item) //if
found return relative index value
return i;
return -1; //if not found return -1
}
int binarySearch()
{
int item,beg,end,mid;
Scanner s=new Scanner(System.in);
//scanner object for reading values
System.out.println("Enter element
to search : ");
item=s.nextInt(); //input item to
search
beg=0;end=size-1;
mid=(beg+end)/2; //calculation of
mid location
while(beg<=end &&
array[mid]!=item) //repeat loop until element found
{
if(item>array[mid]) //if right partition
beg=mid+1;
else
if(item<array[mid])//if left partition
end=mid-1;
mid=(beg+end)/2; //new mid
calculation
}
if(array[mid]==item)
return mid;
else
return -1;
}
void sort()
{
quickSort(array,0,size-1);
//calling recursive function
}
private void quickSort(int arr[],int low,int
high)
{
int i=low,j=high; //getting i,
j
int temp; // for swapping
int
pivot=arr[(low+high)/2];//getting pivot element
//partioning #1
while(i<=j)
{
while(arr[i]<pivot) //searching for greater element than
pivot
i++;
while(arr[j]>pivot)//searching for smaller element than
pivot
j--;
if(i<=j)
{
//swapping
process
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
i++;
j--;
}
}
if(low<j) //recursive
call1
quickSort(arr,low,j);
if(i<high)//recursive
call2
quickSort(arr,i,high);
}
public static void main(String args[])
{
int scount=0;
int a[]={23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55,
1, 19, 37, 88, 8, 97, 25, 50, 75, 61, 49}; //array initialize
SearchingSorting s1=new SearchingSorting(a);
System.out.println("Unsorted array (output #1) :
");
s1.showArray();
System.out.println("Linear Searching item position
(output #2): "+s1.linearSearch());
for(int i=1;i<=5;i++)
scount+=s1.linearSearch(); //counting 05 elements
linear searches
System.out.println("Linear Searching Total no.of
searches (output #3): "+scount);
scount=0; //reset search count
s1.sort();
System.out.println("Sorted array (output #4):
");
s1.showArray();
System.out.println("Binary Searching item position
(output #5): "+s1.binarySearch());
for(int i=1;i<=5;i++)
scount+=s1.binarySearch(); //counting 05 elements
binary searches
System.out.println("Binary Searching Total no.of
searches (output #6): "+scount);
}
}
//output
Using Arrays with Sorting and Searching Algorithms 1) This program has six required outputs and involves...
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...
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 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...
Comparison of Sorting Algorithms You are required to implement all the sorting algorithms (Bubble, Selection, Insertion, Quick, Merge). Take help from lecture slides and welb . You will then create arrays of different sizes as instructed below, test each algorithm on each array and record the execution times of each individual algorithm on each array. . You will report these execution times in a table and then write a report explaining the execution times of the sorting algorithms according to...
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...
1. a. Using C++, represent the following graph using adjacency matrix, and implement depth first searching (DFS) by stack (define it with class) to traverse the graph. 6 7 2 4 b. If starting with node 2, when node 7 is printed, what numbers are in the stack (for DFS)? Please draw the stack step by step to show how the numbers are pushed into and popped out of it. 2. a. Given a set of integer numbers as int...
The files must be called Proper coding conventions required the first letter of the class start with a capital letter and the first letter of each additional word start with a capital letter. Only submit the .java file needed to make the program run. Do not submit the .class file or any other file. 5% Style Components Include properly formatted prologue, comments, indenting, and other style elements as shown in Chapter 2 starting page 64 and Appendix 5 page 881-892....
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,...
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...
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...
This lab is to give you more experience with C++ Searching and Sorting Arrays Given a file with data for names and marks you will read them into two arrays You will then display the data, do a linear search and report if found, sort the data, do a binary search. Be sure to test for found and not found in your main program. Read Data Write a function that reads in data from a file using the prototype below....