Question

In: Computer Science

Write a method public static void minMax(int[] arr) that takes an array of unique ints of...

Write a method public static void minMax(int[] arr) that takes an array of unique ints of length at least two as an argument, and swaps the smallest value of the array into the 0th position and swaps the largest value of the array into the last position.

For example, if int[] a = {4, 3, 2, 6, 1, 5}, the method call minMax(a) should modify the array so that it is {1, 3, 2, 5, 4, 6}.

The method should make only one pass through the array.

The method should not return a value. Instead, it should modify the array in place.

Solutions

Expert Solution

public static void minMax(int[] arr) {
    int minIndex = 0, maxIndex = 0;
    for (int i = 0; i < arr.length; i++) {
        if (arr[i] < arr[minIndex]) minIndex = i;
        if (arr[i] > arr[maxIndex]) maxIndex = i;
    }
    int temp = arr[0];
    arr[0] = arr[minIndex];
    arr[minIndex] = temp;

    temp = arr[arr.length - 1];
    arr[arr.length - 1] = arr[maxIndex];
    arr[maxIndex] = temp;
}

import java.util.Arrays;

public class MinMaxTest {

    public static void minMax(int[] arr) {
        int minIndex = 0, maxIndex = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] < arr[minIndex]) minIndex = i;
            if (arr[i] > arr[maxIndex]) maxIndex = i;
        }
        int temp = arr[0];
        arr[0] = arr[minIndex];
        arr[minIndex] = temp;

        temp = arr[arr.length - 1];
        arr[arr.length - 1] = arr[maxIndex];
        arr[maxIndex] = temp;
    }

    public static void main(String[] args) {
        int[] arr = {4, 3, 2, 6, 1, 5};
        System.out.println(Arrays.toString(arr));
        minMax(arr);
        System.out.println(Arrays.toString(arr));
    }
}


Related Solutions

JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c,...
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c, int d, int f) It should print a graph (using asterisks) for each of the letters entered in the reverse order of the parameter list and with a label. In addition, if A and B grades sum is equal or exceeds that of grades C and D and F, the message “Strong class!” should be displayed. For example a method call of: showGradeDistribution(5,7,4,4,3); Would...
C Write a function int sort(int** arr, int n) that takes as input an array of...
C Write a function int sort(int** arr, int n) that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value...
   private static void merge(int arr[], int l, int m, int r) {        //...
   private static void merge(int arr[], int l, int m, int r) {        // Find sizes of two subarrays to be merged        int n1 = m - l + 1;        int n2 = r - m;        /* Create temp arrays */        int L[] = new int[n1];        int R[] = new int[n2];        /* Copy data to temp arrays */        for (int i = 0; i...
In C++ Write a function which takes two parameters: an array of ints and an int...
In C++ Write a function which takes two parameters: an array of ints and an int size of the array and prints every element greater than 5 to the screen. As an example, if the array has the following 10 elements: 2 5 8 9 7 1 0 2 6 3, your function should print out 8 9 7 6. You may assume that the parameters passed to the function are valid. Your function must have the following signature: void...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an argument and returns the sum for every other Int from n down to 1. For example, sumForEachOther(8) should return 20, since 8+6+4+ 2=20.And the call sumForEachOther(9) should return 25 since 9+7+5 + 3+1-=25. Your method must use recursion.
JAVA Given the header of a method public static void m1 (int[ ] max) Write down...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down Java codes to invoke m1 method, declare variables as needed, (Do NOT implement the method)
rite a method with the following header: public static void showGradeDistribution(int a, int b, int c,...
rite a method with the following header: public static void showGradeDistribution(int a, int b, int c, int d, int f) It should print a graph (using asterisks) for each of the letters entered in the reverse order of the parameter list and with a label. In addition, if A and B grades sum is equal or exceeds that of grades C and D and F, the message “Strong class!” should be displayed. For example a method call of: showGradeDistribution(5,7,4,4,3); Would...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};   ...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};        //Complexity Analysis //Instructions: Print the time complexity of method Q1_3 with respect to n=Size of input array. For example, if the complexity of the //algorithm is Big O nlogn, add the following code where specified: System.out.println("O(nlogn)"); //TODO }    public static void Q1_3(int[] array){ int count = 0; for(int i = 0; i < array.length; i++){ for(int j = i; j < array.length;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT