This program has six required outputs and involves searching

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. 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. Then display the total number of searches for all five numbers. This is required output #3 of 6. Sort the numbers using a recursive quicksort and then display the sorted array. This is required output #4 of 6. 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. 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 values if found and -1 if not found. The other search method should be a binary search of a sorted array that accents as input the integer value that is to be searched for and returns the index information in the six items above.

Solution

import java.io.*;
import java.lang.*;
import java.util.*;

class Search_Sort
{
public static void main(String args[])
{
int arr[]={23,17,5,90,12,44,38,84,77,3,66,55,1,19,37,88,8,97,25,50,75,61,49};
int flag=0;
Scanner sc= new Scanner(System.in);
//to display unsorted list
for(int i=0;i<arr.length;i++)
System.out.print(\" \"+arr[i]); //part 1
  
//to search element in the list ;part 2
System.out.println(\"enter the key to be searched\");
int key=sc.nextInt();
for(int i=0;i<arr.length;i++)
{
count++;
if(key==a[i])
{
flag=1;
break;
}
  
}
if(flag==1)
System.out.println(\"element found at:\"+i);
  
else
System.out.println(\"-1\");
System.out.println(\"the number of seacrches required: \"+count);
//to sort elements using quick sort
sort(arr);

}
public static void sort(int[] arr)
{
quickSort(arr, 0, arr.length - 1);
}
/** Quick sort function **/
public static void quickSort(int arr[], int low, int high)
{
int i = low, j = high;
int temp;
int pivot = arr[(low + high) / 2];

/** partition **/
while (i <= j)
{
while (arr[i] < pivot)
i++;
while (arr[j] > pivot)
j--;
if (i <= j)
{
/** swap **/
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}

/** recursively sort lower half **/
if (low < j)
quickSort(arr, low, j);
/** recursively sort upper half **/
if (i < high)
quickSort(arr, i, high);
}
}

the code is partially complete

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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site