Question

JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration...

JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration skill Problem description:

Write the following eight methods and write a main function to test these methods

// return the index of the first occurrence of key in arr

// if key is not found in arra, return -1 public static int linearSearch(int arr[], int key)

// sort the arr from least to largest by using select sort algorithm public stati void selectSort(int arr[])

// print out all array elements. 5 elements per line public static void printArray(int arr[])

// assign each element in array with a random number

// between 1 and 100, inclusive public static void initializeArray(int arr[])

// find the range of all array elements

// the range is defined as the difference between the largest and smallest elements public static int range(int arr[])

// find the largest element in array public static int largst(int arr[]) // find the smallest element in array public static int smallest(int arr[])

// find the average value of all elements in array public static double average(int arr[]) Sample pseudo code for main function

1. Declare an int array of size 10

2. Initialize the array by calling initializeArray function

3. Print out the array by calling printArray function

4. Print out the largest and smallest value of the array by using System.out.prinln and calling largest and smalles functions

5. Print out the range of the array

6. Print out the average of the array elements

7. Ask user to enter a search key

8. Call the linearSearch function with array and key. Based on result, print out appropriate message

9. Call the selectSort function to sort the array.

10. Print out array again to see the sorted array

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshot of program code:-

import java.util. Random; import java.util.Scanner; public class ArraySearchSortLargest Smallest { //Function assigns each el

//Function finds the largest element in array public static int largst (int arr[]) int max = arr[0]; for(int i=1; i<10; i++)

//Function finds the average value of all elements in array public static double average (int arr[]) double sum = 0; for(int

//Function returns the index of the first occurrence of key in array. //If key is not found in array, it returns -1. public s

//Prompt & read key to be searched from user System.out.print(Enter a search key: ); int key = scan.nextInt(); //Calling li

Screenshot of output:-

Array elements: 94 87 20 48 50 46 22 28 97 61 Largest element: 97 Smallest element: 20 Range of array: 77 Average of the arra

Program code to copy:-

import java.util.Random;
import java.util.Scanner;

public class ArraySearchSortLargestSmallest {
  
   //Function assigns each element in array with a
   //random number between 1 and 100.
   public static void initializeArray(int arr[])
   {
       Random r = new Random();
  
for (int i = 0; i < 10; i++)
arr[i] = r.nextInt(100)+1;
   }

   //Function prints out all array elements.Five elements per line
   public static void printArray(int arr[])
   {   int lineCounter = 1;
       for (int i = 0; i < 10; i++)
       {
           System.out.print(arr[i]+" ");
           if((lineCounter%5)==0)
           {
               System.out.println();
               lineCounter = 0;
           }
           lineCounter++;
       }
   }
  
   //Function finds the largest element in array
   public static int largst(int arr[])
   {
       int max = arr[0];
       for(int i=1; i<10; i++)
       {
           if(arr[i]>max)
               max = arr[i];
       }
       return max;
   }
  
   //Function finds the smallest element in array
   public static int smallest(int arr[])
   {
       int min = arr[0];
       for(int i=1; i<10; i++)
       {
           if(arr[i]<min)
               min = arr[i];
       }
       return min;
   }

   //Function finds the range of all array elements.
   //The range is defined as the difference between the largest and smallest.
   public static int range(int arr[])
   {
       int large = largst(arr);
       int small = smallest(arr);
       return (large-small);
   }

   //Function finds the average value of all elements in array
   public static double average(int arr[])
   {
       double sum = 0;
       for(int i=0; i<10; i++)
       {
           sum = sum + arr[i];
       }
       return (sum/10);
   }
     
   //Function sorts the array from least to largest by using select sort.
   public static void selectSort(int arr[])
   {
       for(int i=0; i<10; i++)
{
// Find the minimum element
int minIndex = i;
for (int j=i+1; j<10; j++)
if (arr[j] < arr[minIndex])
minIndex = j;

//Swap
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
   }
  
   //Function returns the index of the first occurrence of key in array.
   //If key is not found in array, it returns -1.
   public static int linearSearch(int arr[], int key)
   {
       for(int i=0; i<10; i++)
       {
           if(arr[i]==key)
               return i;
       }
       return -1;
   }
  
   //main() method
   public static void main(String[] args) {
       //Declare an int array of size 10
       int arr[] = new int[10];
       Scanner scan = new Scanner(System.in);
      
       //Initialize the array by calling initializeArray function
       initializeArray(arr);
       //Print out the array by calling printArray function
       System.out.println("Array elements: ");
       printArray(arr);
       //Print out the largest by calling largst function
       System.out.println("Largest element: "+largst(arr));
       //Print out the largest by calling smallest function
       System.out.println("Smallest element: "+smallest(arr));
       //Print out the range of the array by calling range function
       System.out.println("Range of array: "+range(arr));
       //Print out the average of the array elements by calling average function
       System.out.println("Average of the array elements: "+average(arr));
       //Prompt & read key to be searched from user
       System.out.print("Enter a search key: ");
       int key = scan.nextInt();
       //Calling linearSearch function with array and key
       int index = linearSearch(arr, key);
       //Based value returned by linearSearch function, appropriate message is printed
       if(index!=-1)
           System.out.println(key+" is found at index location "+index);
       else
           System.out.println(key+" is not found in an array");
       //Calling selectSort function to sort the array.
       selectSort(arr);
       //Print out sorted array by calling printArray function
       System.out.println("Sorted array: ");
       printArray(arr);

   }

}

Add a comment
Know the answer?
Add Answer to:
JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration...
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
  • Implement the bubble sort algorithm described here: While the array is not sorted For each adjacent...

    Implement the bubble sort algorithm described here: While the array is not sorted For each adjacent pair of elements If the pair is not sorted Swap the elements Use the BubbleSorter class to fill in the code and make it run with the BubbleSorterDemo class. BubbleSorter.java public class BubbleSorter {    /** Sort an integer array using the bubble sort algorithm. @param arr array of integers to sort    */    public static void sort(int[] arr)    { // Your...

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • I need to program 3 and add to program 2 bellows: Add the merge sort and...

    I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array. ____________________>>>>>>>>>>>>>>>>>>>>___________________________ (This is program 2 code that I did : ) ---->>>>>> code bellow from program 2 java program - Algorithms Write a program that randomly generates 100,000 integers into an array....

  • Hello this is my java sorting algorithm program i need all of my errors corrected so...

    Hello this is my java sorting algorithm program i need all of my errors corrected so I can run it. thank you!! import java.util.Scanner;    public class SortingAlogs { public static void main(String[]args){ int array [] = {9,11,15,34,1}; Scanner KB = new Scanner(System.in); int ch; while (true) { System.out.println("1 Bubble sort\n2 Insertion sort\n3 Selection sort\n"); ch = KB.nextInt(); if (ch==1)    bubbleSort(array); if (ch==2)    insertion(array); if (ch==3)    Selection(array); if (ch==4) break;    print(array);    System.out.println(); }    }...

  • must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int...

    must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int [] arr); public static void quickSort(int [] arr); public static void mergeSort(int [] arr); The quick sort and merge sort must be implemented by using recursive thinking. So the students may provide the following private static methods: //merge method //merge two sorted portions of given array arr, namely, from start to middle //and from middle + 1 to end into one sorted portion, namely,...

  • 3. Modify the insertion sort algorithm discussed in class so that it can sort strings. That...

    3. Modify the insertion sort algorithm discussed in class so that it can sort strings. That is, its input will be an array, the type of which is string. The insertion sort algorithm will sort the elements in this array. Finally, its output will be a sorted array. As the solution of this problem, you need to submit the following answer(s): (1). The implementation of your algorithm in C# (20points). Algorithm: At each array-position, it checks the value there against...

  • 6) Assume that we are using quick sort algorithm to sort the following elements in the...

    6) Assume that we are using quick sort algorithm to sort the following elements in the array 26, 15,30,11,8,17 22, 40, 4, 10. Use the first element in the array as pivot. (20 pts.) 1- How total iterations it would take to complete the sorting process? 2- Simulate the entire sorting process. (If you need additional space, complete it at the other side of the paper) public static void quick_sort(intl] a, int left, int right) if (left < right) (...

  • 1.Write code for a Java method, printArray, that takes an int array as parameter and prints...

    1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...

  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

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
ADVERTISEMENT