2

I am trying to code some pseudocode that was given on an assignment - however the coding portion I'm doing now is not part of the assignment and is just for fun.

One part of the pseudocode says "swap elements of the array". In my code I have to do this in two places. However I was wondering if there's a way to do this by creating only one method, i.e. creating another method that simply swaps them and saves the extra lines of code I used each time I wanted to run it.

The problem is, if I create a brand new method outside of this one, I'll have to send in the array as a parameter and take it out as well, and I'm afraid this will be less efficient (obviously no big deal here but I'm trying to learn for future larger projects).

Here is my code containing the repeated "swap" method lines.

public int[] myAlgorithm(int[] arrayOfInts, int size){
    boolean done = true;
    int j= 0;

    while (j <= n-2){
        if (arrayOfInts[j] > arrayOfInts[j+1]){
            int tempHolder = arrayOfInts[j];
            arrayOfInts[j] = arrayOfInts[j+1];
            arrayOfInts[j+1] = tempHolder;
            done = false;
        }
        j = j + 1;
    }
    j = size - 1;
    while (j >= 1){
        if (arrayOfInts[j] <= arrayOfInts[j-1]){
            int tempHolder = arrayOfInts[j];
            arrayOfInts[j] = arrayOfInts[j+1];
            arrayOfInts[j+1] = tempHolder;
            done = false;
        }
        j--;
    }
    if (!done)
        myAlgorithm(arrayOfInts, size)
    else
        return arrayOfInts;
}
CodyBugstein
  • 21,984
  • 61
  • 207
  • 363

5 Answers5

3

I'll have to send in the array as a parameter and take it out as well

You only need to "send it" and work on the array directly. In practice, passing a parameter to a method is a very cheap operation which, if called often enough, will likely be optimised by the compiler anyway so you should not worry about it too much (unless you prove that it is a performance penalty by profiling your application).

In your case, you could have a method such as:

public void swap(int[] arrayOfInts, int i, int j) {
    int tempHolder = arrayOfInts[i];
    arrayOfInts[i] = arrayOfInts[j];
    arrayOfInts[j] = tempHolder;
}

and you would call it:

swap(arrayOfInts, j, j+1);

Why does it work?

Java passes arguments by value, but in the case of objects (i.e. non-primitive types, including arrays), the value that is passed is the reference to the object. In other words, the argument to the swap method is a reference to the same array as in the calling code, so you can work on that array directly, without having to send it back to the calling method.

Community
  • 1
  • 1
assylias
  • 321,522
  • 82
  • 660
  • 783
1

It takes less time to run if there are duplicated bits of code. It takes more space though.

The problem is that you can't predict how the compiler will optimize your code. You could make a function that takes the array and two indices and swaps the elements at those indices. The compiler might inline the code in the function where you call it. If you don't, the compiler might notice you have duplicated code and create a function if it decides to.

Write the code how you can read it best, especially when the difference in efficiency is so small.

Lee Meador
  • 12,829
  • 2
  • 36
  • 42
1

The problem is, if I create a brand new method outside of this one, I'll have to send in the array as a parameter and take it out as well...

This isn't actually the case. In Java, an array is an object. So when you pass an array to a method, you're really only passing a reference to the array. Any changes you make to the array inside the method are going to be there after the method call. Passing only a reference to the array also means that the method will be very fast because you're not making a copy of the entire array.

So you could write a swap method:

private void swap(int[] arrayOfInts, int i, int j) {
    int temp = arrayOfInts[i];
    arrayOfInts[i] = arrayOfInts[j];
    arrayOfInts[j] = temp;
}
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
1

Arrays, like any other objects of reference types are passed by reference - i.e., the reference to the object is passed by value. That means, the array itself is not copied. Hence, there is no reson to fear "less efficience".

Ingo
  • 36,037
  • 5
  • 53
  • 100
1

In java you can pass the array as a variable and this will only pass a pointer to it and not a whole new copy of the array so you have to pass the array and the the two indexes you want to swap:

 public static void swap(final int[] arr, final int         pos1, final int pos2){
final int temp = arr[pos1];
arr[pos1] = arr[pos2];
arr[pos2] = temp;
}
Greg Oks
  • 2,700
  • 4
  • 35
  • 41