Question: Java language, (Revise selection sort) In the selectSort.java source code, the selectionSort() method repeatedly finds the smallest number in the current array and swaps it
Java language, (Revise selection sort) In the selectSort.java source code, the selectionSort() method repeatedly finds the smallest number in the current array and swaps it with the leftmost element in the unsorted part of the array. Please write another method called selectionSort_Max() by finding the largest number and swapping it with the rightmost element in the unsorted part of the array. Finally, test your method in main() function.
Code:
// selectSort.java // demonstrates selection sort // to run this program: java SelectSortApp //////////////////////////////////////////////////////////////// class ArraySel { private long[] a; // ref to array a private int nElems; // number of data items //-------------------------------------------------------------- public ArraySel(int max) // constructor { a = new long[max]; // create the array nElems = 0; // no items yet } //-------------------------------------------------------------- public void insert(long value) // put element into array { a[nElems] = value; // insert it nElems++; // increment size } //-------------------------------------------------------------- public void display() // displays array contents { for(int j=0; j for(out=0; out arr.insert(77); // insert 10 items arr.insert(99); arr.insert(44); arr.insert(55); arr.insert(22); arr.insert(88); arr.insert(11); arr.insert(00); arr.insert(66); arr.insert(33); arr.display(); // display items arr.selectionSort(); // selection-sort them arr.display(); // display them again } // end main() } // end class SelectSortApp ////////////////////////////////////////////////////////////////
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
