Question

In: Computer Science

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.

Solutions

Expert Solution

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:


Related Solutions

Create a Java Application that implements a Selection sort algorithm to sort an array of 20...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20 unsorted numbers. You should initiate this array yourself and first output the array in its original order, then output the array after it has been sorted by the selection sort algorithm. Create a second Java Application that implements an Insertion sort algorithm to sort the same array. Again, output the array in its original order, then output the array after it has been sorted...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20 unsorted numbers. You should initiate this array yourself and first output the array in its original order, then output the array after it has been sorted by the selection sort algorithm.
Modify the quicksort algorithm such that it uses the last item as the pivot instead of the 1st. Also, sort in descending order, instead of ascending order.
Programming Language : JavaModify the quicksort algorithm such that it uses the last item as the pivot instead of the 1st. Also, sort in descending order, instead of ascending order.NOTE: Do not move the last element into the first element of the array. You must treat the algorithm as if the pivot is actually sitting in the last location of the array.After it has been sorted in descending order, go through all the items in the array and make sure...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top...
Analyzing Selection Sort Algorithm The selection sort algorithm works by first finding the smallest value in...
Analyzing Selection Sort Algorithm The selection sort algorithm works by first finding the smallest value in a list and swapping the first value with it, then finding the second smallest value and swapping the second value with it, continuing until all the values are in order. Implement this algorithm, then determine its growth function, and hence the order of the algorithm. Find how many swaps are made. Use Java Code to create algorithm
* Sort Student array descending based on GPA using MERGE sort. Sorting will be done in...
* Sort Student array descending based on GPA using MERGE sort. Sorting will be done in place. * * @param students array to be sorted, can be empty, but cannot be null */ public static void sortGPA(Student[] students) { // TODO: implement this } Student class: public class Student extends Person { private double GPA; public Student(String lastName, String firstName, double gpa) { super(lastName, firstName); this.GPA = gpa; } public double getGPA() { return GPA; } @Override public boolean equals(Object...
Java Programm please! Design and implement an algorithm using recursion and backtracking to sort an array...
Java Programm please! Design and implement an algorithm using recursion and backtracking to sort an array of integers into ascending order. Consider the given array as input and produce a sorted array as output. Each time you take an integer from the input array, place it at the end of the output array. If the result is unsorted, backtrack.
1a)Use the Lomuto's quicksort algorithm to sort the following list of integers in nondecreasing order for...
1a)Use the Lomuto's quicksort algorithm to sort the following list of integers in nondecreasing order for the first pivot item. Use pointers i and j for swapping items (if necessary) for each step leading to your answer. Note do the problem just for the first pivot item. 123,34,189,56,150,12,9,24 1b)Use the same list of numbers and use the Hoare's quicksort algorithm using pointers l and r and updating the pointers i and j and swapping items. Do the problem just for...
Following is the algorithm of Quicksort for sorting an array of integers in ascending order. Partition(numbers,...
Following is the algorithm of Quicksort for sorting an array of integers in ascending order. Partition(numbers, lowIndex, highIndex) {    midpoint = lowIndex + (highIndex - lowIndex) / 2    pivot = numbers[midpoint]    done = false    while (!done) {       while (numbers[lowIndex] < pivot)          lowIndex++       while (pivot < numbers[highIndex])          highIndex--       if (lowIndex >= highIndex) {          done = true       }       else {          temp = numbers[lowIndex]          numbers[lowIndex] = numbers[highIndex]          numbers[highIndex] = temp                 lowIndex++          highIndex--       }    }    return highIndex } Quicksort(numbers, lowIndex, highIndex) {    if (lowIndex...
Explain why selection sort can be viewed as a divide and conquer algorithm. Compare selection sort...
Explain why selection sort can be viewed as a divide and conquer algorithm. Compare selection sort with insertion sort with respect to performance (consider worst case and best case running times).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT