Question

Java : Modify the selection sort algorithm to sort an array of integers in descending order....

Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field.

Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion

.......

........

sec01/SelectionSortDemo.java

import java.util.Arrays;

/**
This program demonstrates the selection sort algorithm by
sorting an array that is filled with random numbers.
*/
public class SelectionSortDemo
{
public static void main(String[] args)
{
int[] a = ArrayUtil.randomIntArray(20, 100);
System.out.println(Arrays.toString(a));

SelectionSorter.sort(a);

System.out.println(Arrays.toString(a));
}
}

sec01/ArrayUtil.java

import java.util.Random;

/**
This class contains utility methods for array manipulation.
*/
public class ArrayUtil
{
private static Random generator = new Random();

/**
Creates an array filled with random values.
@param length the length of the array
@param n the number of possible random values
@return an array filled with length numbers between
0 and n - 1
*/
public static int[] randomIntArray(int length, int n)
{
int[] a = new int[length];
for (int i = 0; i < a.length; i++)
{
a[i] = generator.nextInt(n);
}
  
return a;
}

/**
Swaps two entries of an array.
@param a the array
@param i the first position to swap
@param j the second position to swap
*/
public static void swap(int[] a, int i, int j)
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}

sec01/SelectionSorter.java

/**
The sort method of this class sorts an array, using the selection
sort algorithm.
*/
public class SelectionSorter
{
/**
Sorts an array, using selection sort.
@param a the array to sort
*/
public static void sort(int[] a)
{
for (int i = 0; i < a.length - 1; i++)
{
int minPos = minimumPosition(a, i);
ArrayUtil.swap(a, minPos, i);
}
}

/**
Finds the smallest element in a tail range of the array.
@param a the array to sort
@param from the first position in a to compare
@return the position of the smallest element in the
range a[from] . . . a[a.length - 1]
*/
private static int minimumPosition(int[] a, int from)
{
int minPos = from;
for (int i = from + 1; i < a.length; i++)
{
if (a[i] < a[minPos]) { minPos = i; }
}
return minPos;
}
}

Extra Note: Let’s program this algorithm, called selection sort. For this program, as well as the other programs in this chapter, we will use a utility method to generate an array with random entries. We place it into a class ArrayUtil so that we don’t have to repeat the code in every example. To show the array, we call the static toString method of the Arrays class in the Java library and print the resulting string (see Section 7.3.4). We also add a method for swapping elements to the ArrayUtil class. (See Section 7.3.8 for details about swapping array elements.)

This algorithm will sort any array of integers. If speed were not an issue, or if there were no better sorting method available, we could stop the discussion of sorting right here. As the next section shows, however, this algorithm, while entirely correct, shows disappointing performance when run on a large data set.

Special Topic 14.2 discusses insertion sort, another simple sorting algorithm.

Homework Answers

Answer #1

Explanation: As you have mentioned please do not already answered solution,we cannot see others answers.I have modified the SelectionSorter.java and in that instead of finding the minimumPosition find the maximumPostition in the array which can be done by changing the method minimumPosition to maximumPostition and then place it at the 0th position in the array, similarly for all the numbers this process is carried out and we get the resulting array in the descending order.The algorithm selection sort works by swapping the largest number at the first position from the array and thus for each iteration this process continues and we get the desired result.If you want some other approach or method please mention in the comments, i will modify the code accordingly.lease upvote if you liked my answer and comment if you need any modification or explanation.

/**
* The sort method of this class sorts an array, using the selection sort
* algorithm.
*/
public class SelectionSorter
{
   /**
   * Sorts an array, using selection sort.
   *
   * @param a the array to sort
   */
   public static void sort(int[] a)
   {
       for (int i = 0; i < a.length - 1; i++)
       {
           int maxPos = maximumPosition(a, i);
           ArrayUtil.swap(a, i, maxPos);
       }
   }

   /**
   * Finds the largest element in a tail range of the array.
   *
   * @param a the array to sort
   * @param from the first position in a to compare
   * @return the position of the largest element in the range a[from] . . .
   * a[a.length - 1]
   */
   private static int maximumPosition(int[] a, int from)
   {
       int maxPos = from;
       for (int i = from + 1; i < a.length; i++)
       {
           if (a[i] > a[maxPos])
           {
               maxPos = i;
           }
       }
       return maxPos;
   }
}

Output:

Know the answer?
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
Similar Questions
Finish the code wherever it says TODO /**    * Node type for this list. Each...
Finish the code wherever it says TODO /**    * Node type for this list. Each node holds a maximum of nodeSize elements in an    * array. Empty slots are null.    */    private class Node {        /**        * Array of actual data elements.        */        // Unchecked warning unavoidable.        public E[] data = (E[]) new Comparable[nodeSize];        /**        * Link to next node.       ...
Implement functions for insertion sort, quicksort, heapsort and merge sort that input an array of integers...
Implement functions for insertion sort, quicksort, heapsort and merge sort that input an array of integers and sort it in-place. Write a program that generates random integer arrays (hint: use seed appropriately to avoid generating same sequences) of lengths 10, 100, 1000, 10,000, 100,000, 1000,000, and then sorts each using each of the sorting functions from (a), and measures the time in nanoseconds. The program will repeat this process 30 times and will compute the average execution time for each...
. A sorting algorithm is stable if the relative order of any two equal entries in...
. A sorting algorithm is stable if the relative order of any two equal entries in the given array stays the same: when two records a[i] and a[j] are equal in content, and i < j, then the algorithm sorts the array in a way that the record originally stored in a[i], still appears to the left of the record originally stored in a[j], when the array is sorted. Which of the algorithms Insertion Sort, Shellsort, Heapsort, and Mergesort (as...
Problem 3 (4 marks). A sorting algorithm is stable if the relative order of any two...
Problem 3 . A sorting algorithm is stable if the relative order of any two equal entries in the given array stays the same: when two records a[i] and a[j] are equal in content, and i < j, then the algorithm sorts the array in a way that the record originally stored in a[i], still appears to the left of the record originally stored in a[j], when the array is sorted. Which of the algorithms Insertion Sort, Shellsort, Heapsort, and...
Problem 3 (4 marks). A sorting algorithm is stable if the relative order of any two...
Problem 3 . A sorting algorithm is stable if the relative order of any two equal entries in the given array stays the same: when two records a[i] and a[j] are equal in content, and i < j, then the algorithm sorts the array in a way that the record originally stored in a[i], still appears to the left of the record originally stored in a[j], when the array is sorted. Which of the algorithms Insertion Sort, Shellsort, Heapsort, and...
8.15 *zyLab: Method Library (Required & Human Graded) This code works but there are some problems...
8.15 *zyLab: Method Library (Required & Human Graded) This code works but there are some problems that need to be corrected. Your task is to complete it to course style and documentation standards CS 200 Style Guide. This project will be human graded. This class contains a set of methods. The main method contains some examples of using the methods. Figure out what each method does and style and document it appropriately. The display method is done for you and...
Do a theta analysis and count the number of computations it performed in each function/method of...
Do a theta analysis and count the number of computations it performed in each function/method of the following code: import java.io.*; import java.util.Scanner; class sort { int a[]; int n; long endTime ; long totalTime; long startTime; static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public sort(int nn) // Constructor { a = new int[nn]; n = nn; endTime= 0; totalTime =0; startTime =0; } public static void main(String args[]) throws IOException { System.out.print("\nEnter number of students: "); int nn =...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length();...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length(); for (int i = 0; i < array.length; i++) { if (array[i].length() < minLength) minLength = array[i].length(); } return minLength; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] strings = new String[50]; for (int i = 0; i < strings.length; i++) { System.out.print("Enter string " + (i + 1) + ": "); strings[i] = in.nextLine(); } System.out.println("Length of smallest...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...