Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Write a modified Selection sort algorithm, called maxSelectionSort, whose i

ID: 3862569 • Letter: J

Question

Java

Write a modified Selection sort algorithm, called maxSelectionSort, whose invariants for the outer loop is: After the first iteration, the last number is the maximum of the numbers of the array. After the second iteration, the last two numbers are the maximum two numbers of the array. After the third iteration, the last three numbers are the maximum three numbers of the array. Using these invariants, the maxSelectionSort algorithm should sort the array once the outer loop is complete. Also, indicate the worst-case runtime of your implementation. You can use the method if needed. static void maxSelectionSort (int[] a){//TODO} private static void exch(int[] a, int i, int j){int swap = a[i]; a[i] = a[j]; a[j] = swap;}

Explanation / Answer

package com.java2novice.algos;

public class TwoMaxNumbers {

    public void printTwoMaxNumbers(int[] nums){
        int maxOne = 0;
        int maxTwo = 0;
        int maxThree = 0;
        for(int n:nums){
            if(maxOne < n){
                maxTwo = maxOne;
                maxOne =n;
            } else if(maxTwo < n){
                maxTwo = n;
            }
        }
        System.out.println("First Max Number: "+maxOne);
        System.out.println("Second Max Number: "+maxTwo);
        System.out.println("Second Max Number: "+maxThree);
    }
   
    public static void main(String a[]){
        int num[] = {5,34,78,2,45,1,99,23};
        TwoMaxNumbers tmn = new TwoMaxNumbers();
        tmn.printTwoMaxNumbers(num);
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote