Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

public class SelectionSorter { //Returns the index of the largest element in the

ID: 3828592 • Letter: P

Question

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");

}

Explanation / Answer

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;
}


}