0

Here is my code and I am confused as to how to swap the value of array at index1 with the value of array at index2

    public static void swapByIndex(int myArray[], int index1, int index2) {
    int position1 = myArray[index1];
    int position2 = myArray[index2];
    
    for(int i = 0; i < myArray.length; i++) {
        if(i == position1) {
            myArray[i] = position2;
        }
        if(i == position2) {
            myArray[i] = position1;
        }
        System.out.print(myArray[i] + " ");
    }
}
apex
  • 37
  • 7
  • https://stackoverflow.com/questions/13766209/effective-swapping-of-elements-of-an-array-in-java – S Henry Oct 03 '20 at 22:34

4 Answers4

0

It's very simple and common way to achieve this:

public static void swapByIndex(int myArray[], int index1, int index2) {
   int temp = myArray[index1];
   myArray[index1] = myArray[index2];
   myArray[index2] = temp;
}

P.S. This solution has an O(1) time complexity but yours has O(n).

Daniel Michalski
  • 545
  • 4
  • 14
  • Thank you for the response, I shouldve stated that I am not sure if i am allowed to use the .toString method so how would i print this as an array and not a string? – apex Oct 03 '20 at 22:46
  • To print an array: ```System.out.println(Arrays.toString(myArray));``` – Daniel Michalski Oct 04 '20 at 17:52
0

The classic way of performing a swap is simply using a temporary int variable to grab the value of one position while its being changed, so you still have it when you want to assign it to the other position

    public static void swapByIndex(int myArray[], int index1, int index2) {
        int temp = myArray[index1];
        myArray[index1] = myArray[index2];
        myArray[index2] = temp;

}
Malthe
  • 75
  • 6
  • Thank you for the response, I shouldve stated that I am not sure if i am allowed to use the .toString method so how would i print this as an array and not a string? – apex Oct 03 '20 at 22:46
0

You don't need to iterate whole array when swapping elements. You can swap them by just taking a third variable and storing value of first index in it. And then, store second index value in first index. After that, store the value from third variable in the second index.

public static void swapByIndex(int myArray[], int index1, int index2) {
    int temp = myArray[index1];
    myArray[index1] = myArray[index2];
    myArray[index2] = temp;
}
Haseeb Ahmad
  • 118
  • 1
  • 13
  • Thank you for the response, I shouldve stated that I am not sure if i am allowed to use the .toString method so how would i print this as an array and not a string? – apex Oct 03 '20 at 22:46
  • @apex that's another problem entirely. See [here](https://stackoverflow.com/a/4778628/133203) for an example (and some other ideas in the other answers at that link). – Federico klez Culloca Oct 03 '20 at 22:54
0

Here is a quick explanation of how the swapping works.

public static void swapByIndex(int myArray[], int index1, int index2) {
    int position1 = myArray[index1];
    int position2 = myArray[index2];

    // At this point, you already know both the indexes and the values.
    // You can just assign the values directly.
    myArray[index1] = position2;
    myArray[index2] = position1;
}

Now, once you have this, you might notice that we only need to store one of the values in a temporary variable. We can reduce it further to the far more common form:

public static void swapByIndex(int myArray[], int index1, int index2) {
    int position1 = myArray[index1];
    
    // Overwrite the value stored at index 1
    myArray[index1] = myArray[index2];

    // Use the stored value to then fill in the second index
    myArray[index2] = position1;
}

This is how we can arrive at the most common version of the swapping function.

public static void swap(int[] array, int a, int b) {
    int tmp = array[a];
    array[a] = array[b];
    array[b] = tmp;
}

While I don't recommend it if you are just starting to learn java, you can also use the generic version that will work for any type of array.

public static <T> void swap(T[] array, int a, int b) {
    T tmp = array[a];
    array[a] = array[b];
    array[b] = tmp;
}
Locke
  • 7,626
  • 2
  • 21
  • 41
  • Thank you for the response, I shouldve stated that I am not sure if i am allowed to use the .toString method so how would i print this as an array and not a string? – apex Oct 03 '20 at 22:49
  • Generally the best way is to use `Arrays.toString(array)`, but if you can't use that then try just making a for loop and printing each element. `for (int i = 0; i < array.length; i++) { System.out.print(" " + array[i] + ","); }` – Locke Oct 03 '20 at 22:53