In: Computer Science
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.
Code:
SelectionSorter.java:
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, maxPos, i);
}
}
/**
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;
}
}
Screenshot:
ArrayUtil.java:
import java.util.Random;
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;
}
}
Screenshot :
SelectionSortDemo.java:
import java.util.Arrays;
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));
}
}
Screenshot:
Screenshot of the output: