Question

public class SelectionSorter { //Returns the index of the largest element in the arrayOfIntegers, beginning from...

public class SelectionSorter {

//Returns the index of the largest element in the arrayOfIntegers, beginning from the fromIndex.

public static Integer[] selectSort(Integer[] incoming) {

       Integer[] ret = new Integer[incoming.length];

for (int i = 0; i < incoming.length; i++) {

           ret[i] = incoming[i];

       }

       int temp = 0;

       for (int i = 0; i < ret.length - 1; i++) {

           if (ret[i] > ret[i + 1]) {

               temp = ret[i];

               ret[i] = ret[i + 1];

               ret[i + 1] = temp;

               i = -1;

           }

       }

       return ret;

   }

//Returns a sorted (from smallest to largest) copy of the incoming array of Integers. Uses the classic selection sort

//algorithm.

public static Integer[] selectSortReverse(Integer[] incoming) {

   throw new UnsupportedOperationException("you must implement this method");

   }

//Return the index of the smallest element in the arrayOfInts, beginning the search fromIndex;

public static int findMin(int fromIndex, Integer[] arrayOfInts) {

       throw new UnsupportedOperationException("you must implement this method");

   }

//Performs the standard "insertion sort" on a shallow copy of the incoming array.

public static Integer[] insertionSort(Integer[] incoming) {

throw new UnsupportedOperationException("you must implement this method");

}

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

Here I have implemented a find min,

insertion sort in increasing order

As provided selectSort is sorting in increasing order and asked selectSortReverse was asking in increasing order, didn't implemented anything just called method and return. If you need largest to smallest, comment and I will update.

//Program in Java please
public class SelectionSorter {

     //Returns the index of the largest element in the arrayOfIntegers, beginning from the fromIndex.
    public static Integer[] selectSort(Integer[] incoming) {
           Integer[] ret = new Integer[incoming.length];
    for (int i = 0; i < incoming.length; i++) {
               ret[i] = incoming[i];
           }
           int temp = 0;
           for (int i = 0; i < ret.length - 1; i++) {
               if (ret[i] > ret[i + 1]) {
                   temp = ret[i];
                   ret[i] = ret[i + 1];
                   ret[i + 1] = temp;
                   i = -1;
               }
           }
           return ret;
       }
  
//Returns a sorted (from smallest to largest) copy of the incoming array of Integers. Uses the classic selection sort
//algorithm.
public static Integer[] selectSortReverse(Integer[] incoming) {
      Integer[] ret = new Integer[incoming.length];
      ret = selectSort(incoming);
    
      for(int i = 0, j = ret.length-1; i < j; i++, j--)
      {
          int temp = ret[i];
          ret[i] = ret[j];
        
          ret[j] = temp;
      }
    
      return ret;
     }
  
//Return the index of the smallest element in the arrayOfInts, beginning the search fromIndex;
    public static int findMin(int fromIndex, Integer[] arrayOfInts) {
        Integer min = arrayOfInts[arrayOfInts.length-1];
        int index = arrayOfInts.length-1;
        for(int i = arrayOfInts.length-1; i >= fromIndex; i--)
        {
            if(min > arrayOfInts[i])
            {
                min = arrayOfInts[i];
                index = i;
            }
        }
        return index;
       }
  
//Performs the standard "insertion sort" on a shallow copy of the incoming array.
public static Integer[] insertionSort(Integer[] incoming) {
    Integer[] ret = new Integer[incoming.length];
    for (int i = 0; i < incoming.length; i++) {
               ret[i] = incoming[i];
           }
    int i, key, j;
  
    for (i = 1; i < ret.length; i++)
    {
        key = ret[i];
        j = i-1;

        while (j >= 0 && ret[j] > key)
        {
            ret[j+1] = ret[j];
            j = j-1;
        }
        ret[j+1] = key;
    }
    return ret;
}


}

Add a comment
Know the answer?
Add Answer to:
public class SelectionSorter { //Returns the index of the largest element in the arrayOfIntegers, beginning from...
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
  • COMPLETE BigMerge public class BigMerge {       /*    * Returns j such that a[j][index[j]]...

    COMPLETE BigMerge public class BigMerge {       /*    * Returns j such that a[j][index[j]] is the minimum    * of the set S={a[i][index[i]] for every i such that index[i]<a[i].length}    * If the set S is empty, returns -1    * Runs in time a.length.    *    * NOTE: normally this would be private, but leave it    * public so we can test it separately from your merge.    */    public static int argMin(int [][]...

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

  • 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[])...

  • You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement...

    You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement this method return new int[] {}; }    There is a utility method provided for you with the following signature. You may use this method to convert a list of integers into an array. public static int[] convertIntegers(List<Integer> integers) Provided code import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class MatrixSearch { // This method converts a list of integers to an array...

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

  • Draw a flowchart for this program public class InsertionSort {     public static void main(String a[]) {...

    Draw a flowchart for this program public class InsertionSort {     public static void main(String a[]) {         int[] array = {7, 1, 3, 2, 42, 76, 9};         insertionSort(array);     }     public static int[] insertionSort(int[] input) {         int temp;         for (int i = 1; i < input.length; i++) {             for (int j = i; j > 0; j--) {                 if (input[j] < input[j - 1]) {                     temp = input[j];                     input[j] = input[j - 1];                     input[j - 1] = temp;                 }             }             display(input, i);...

  • In the USIntsArrayList Class, implement the USIntsArrayListInterface Interface (which is really just the UnSortedInts Class without...

    In the USIntsArrayList Class, implement the USIntsArrayListInterface Interface (which is really just the UnSortedInts Class without the logic. All you are doing in this class is providing the same functionality as UnSortedInts, but using an ArrayList to hold the data. Document appropriately Thank you, package arrayalgorithms; import java.util.ArrayList; /** * Title UnSorted Ints stored in an Array List * Description: Implement all the functionality of the UnSortedInts * class using an ArrayList * @author Khalil Tantouri */ public class USIntsArrayList...

  • I have this Java program I need Help plz package ads.set2.select; public class Selector { public...

    I have this Java program I need Help plz package ads.set2.select; public class Selector { public static void main(String[] args) {   int index = 3;   int[] numbers = { 23, 4, 33, 11, 99,24 };   int results = select(numbers, index);   System.out.println("Element at index: " + index + " in sorted array is " + results);   // error case   select(numbers, 9); } /** * Returns the number that would be at index {@code index} in a sorted version * of the...

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

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

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