2

I'm supposed to make a static method named swap() that scans an array of integers that takes in the min and max and swaps them. So if max has an index of 0 and min index of 3, index[5] = [3] now and index [3] = [5] now. So the array is created by user inputs:

public static int[] readInputs(int arraySize) {
    System.out.print("What length is the array? ");
    arraySize = console.nextInt();
    int[] intArray = new int[arraySize]; 
    for (int i = 0; i <= intArray.length - 1; i++) {
        System.out.print("Enter an integer: ");
        userInput = console.nextInt();
        intArray[i] = userInput;
    }
    return intArray;
        } //end of readInputs()

I am supposed to make another static method that swaps the min and max called swap():

public static int[] swap(int[] intArray){
    for(int index = 0; index < intArray.length; index++){
        if (intArray[index] > max){
            max = intArray[index];
        } 
        if (intArray[index]<min) {
            min = intArray[index];
        }
    }
} //end of swap()

So far, I have it so it finds the min and max, but I'm stuck on how to swap.

6
  • 2
    Instead of saving the max and min, save the index of the maximum and minimum value. Commented Nov 19, 2015 at 13:49
  • @Manu Aren't I already doing that? Or am I just storing the value of the index? Commented Nov 19, 2015 at 13:51
  • @Shayd3 nope, max = intArray[index]; is the actual element. If you want to save the index, max = index; (and of course max is and int) Commented Nov 19, 2015 at 13:52
  • @ryanyuyu ah! Ok. Thank you! Now I just need to figure out how to swap Commented Nov 19, 2015 at 13:53
  • 2
    What if there are couple of MINs or MAXs in the array? Commented Nov 19, 2015 at 13:54

2 Answers 2

2

Just store the indices of the minimum and the maximum.

Code

public static int[] swap(int[] array) {
    int minIndex = 0, maxIndex = 0;
    for (int i = 1; i < array.length; ++i) {
        if (array[i] < array[minIndex])
            minIndex = i;
        if (array[i] > array[maxIndex])
            maxIndex = i;
    }
    int t;
    if (maxIndex != minIndex) {
        t = array[minIndex];
        array[minIndex] = array[maxIndex];
        array[maxIndex] = t;
    }
    return array;
}

public static void main(String[] args) {
    for(int i : swap(new int[]{1,4,5,8,2,10}))
        System.out.println(i);
}

Output

10
4
5
8
2
1
Sign up to request clarification or add additional context in comments.

5 Comments

What if there are 2 tens or 2 ones in your example?
@cricket_007 the first encountered minimum and maximum will be swapped when the same elements are encountered.
Sure, I understand that, but based on the problem description, shouldn't all mins and maxes be swapped?
You can start your loop at 1.
@KlitosKyriacou thanks for the pointer. I have edited the code ;)
0

You almost had it! You just have to save the index of max and min value and then swap the values!

public static int[] swap(int[] intArray){
    int max = intArray[0], min = intArray[0], maxIndex = 0, minIndex = 0;
    for(int index = 1; index < intArray.length; index++){
        if (intArray[index] > max){
            max = intArray[index];
            maxIndex = index;
        } 
        if (intArray[index]<min) {
            min = intArray[index];
            minIndex = index;
        }
    }
    intArray[maxIndex] = min;
    intArray[minIndex] = max;

    return intArray;
} //end of swap()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.