Please code in java /** * Just a container, really, that serves as a place to ho
ID: 3828472 • Letter: P
Question
Please code in java
/**
* Just a container, really, that serves as a place to house these public
* methods:
*
* static Integer[] selectSort( Integer[] incoming );
*
* static Integer[] selectSortReverse( Integer[] incoming );
*
* static int findMin( int fromIndex, Integer[] arrayOfInts );
*
* static int findMax( int fromIndex, Integer[] arrayOfInts );
*
* static void swap( int fromPos, int toPos, Integer[] arrayOfInts);
*
* Note: these MUST be public in order for you to pass any of the tests.
*
*
*
*/
public class SelectionSorter {
/**
* Returns a sorted (from largest to smallest) copy of the incoming array of
* Integers.
*
* @param incoming
* @return
*/
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;
*
* @param fromIndex
* @param arrayOfInts
* @return
*/
public static int findMin(int fromIndex, Integer[] arrayOfInts) {
throw new UnsupportedOperationException("you must implement this method");
}
/**
* Returns the index of the largest element in the arrayOfIntegers,
* beginning from the fromIndex.
*
* @param fromIndex
* @param arrayOfIntegers
* @return
*/
public static int findMax(int fromIndex, Integer[] arrayOfIntegers) {
throw new UnsupportedOperationException("you must implement this method");
}
/**
* Swaps the contents in the toPos with the fromPos; note, this method does
* not copy the array, but operates on the arrayOfInts directly.
*
* @param fromPos
* @param toPos
* @param arrayOfInts
*/
public static void swap(int fromPos, int toPos, Integer[] arrayOfInts) {
throw new UnsupportedOperationException("you must implement this method");
}
/**
* Performs the standard "insertion sort" on a shallow copy of the incoming
* array.
*
* @param incoming
* @return
*/
public static Integer[] insertionSort(Integer[] incoming) {
throw new UnsupportedOperationException("you must implement this method");
}
}
Explanation / Answer
Hi,
Please see below the classe to sort.
Please comment for any quries/feedbacks.
Thanks,
Anita
SelectionSorter.java
public class SelectionSorter {
/**
* Returns a sorted (from largest to smallest) copy of the incoming array of
* Integers.
*
* @param incoming
* @return
*/
public static Integer[] selectSortReverse(Integer[] incoming) {
int max;
for (int i = 0; i < incoming.length; i++)
{
// Assume first element is max
max = i;
for (int j = i + 1; j < incoming.length; j++)
{
if (incoming[j] > incoming[max])
{
max = j;
}
}
if (max != i)
{
int temp = incoming[i];
incoming[i] = incoming[max];
incoming[max] = temp;
}
}
return incoming;
}
/**
* Return the index of the smallest element in the arrayOfInts, beginning
* the search fromIndex;
*
* @param fromIndex
* @param arrayOfInts
* @return
*/
public static int findMin(int fromIndex, Integer[] arrayOfInts) {
int smallnum =arrayOfInts[fromIndex];
for(int i=fromIndex;i<arrayOfInts.length ;i++){
if(arrayOfInts[i]< smallnum){
smallnum = arrayOfInts[i];
}
}
System.out.println("Small num from index "+fromIndex+"is "+smallnum);
return smallnum;
}
/**
* Returns the index of the largest element in the arrayOfIntegers,
* beginning from the fromIndex.
*
* @param fromIndex
* @param arrayOfIntegers
* @return
*/
public static int findMax(int fromIndex, Integer[] arrayOfIntegers) {
int largenum =arrayOfIntegers[fromIndex];
for(int i=fromIndex;i<arrayOfIntegers.length ;i++){
if(arrayOfIntegers[i] > largenum){
largenum = arrayOfIntegers[i];
}
}
System.out.println("Large num from index "+fromIndex+"is "+largenum);
return largenum;
}
/**
* Swaps the contents in the toPos with the fromPos; note, this method does
* not copy the array, but operates on the arrayOfInts directly.
*
* @param fromPos
* @param toPos
* @param arrayOfInts
*/
public static void swap(int fromPos, int toPos, Integer[] arrayOfInts) {
int temp =arrayOfInts[toPos];
arrayOfInts[toPos] = arrayOfInts[fromPos];
arrayOfInts[fromPos] = temp;
System.out.println("Swapped!");
}
/**
* Performs the standard "insertion sort" on a shallow copy of the incoming
* array.
*
* @param incoming
* @return
*/
public static Integer[] insertionSort(Integer[] incoming) {
int n = incoming.length;
for (int j = 1; j < n; j++) {
int key = incoming[j];
int i = j-1;
while ( (i > -1) && ( incoming [i] > key ) ) {
incoming [i+1] = incoming [i];
i--;
}
incoming[i+1] = key;
}
return incoming;
}
/**
* print the the incoming array.
*
* @param incoming
* @return
*/
private static void printNumbers(Integer[] input) {
for (int i = 0; i < input.length; i++) {
System.out.print(input[i] + ", ");
}
System.out.println(" ");
}
//main method o test
public static void main(String [] args){
//Creating int array
Integer[] input = { 4, 2, 9, 6, 23, 12, 34, 0, 1 };
//Creating SelectionSorter object
SelectionSorter sorter= new SelectionSorter();
//Calling printArray
sorter.printNumbers(input);
//To find the maximimu
sorter.findMax(0, input);
//To find Minimum
sorter.findMin(0, input);
//Insertion Sort
System.out.println("Insertion Sort: ");
sorter.insertionSort(input);
//Calling printArray
sorter.printNumbers(input);
//Swap
sorter.swap(1, 8, input);
//Calling printArray
sorter.printNumbers(input);
//Reverse
System.out.println("Select Sort Reverse: ");
sorter.selectSortReverse(input);
//Calling printArray
sorter.printNumbers(input);
}
}
Sample output:
4, 2, 9, 6, 23, 12, 34, 0, 1,
Large num from index 0is 34
Small num from index 0is 0
Insertion Sort:
0, 1, 2, 4, 6, 9, 12, 23, 34,
Swapped!
0, 34, 2, 4, 6, 9, 12, 23, 1,
Select Sort Reverse:
34, 23, 12, 9, 6, 4, 2, 1, 0,