Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I want to know if someone can suggest how I can remove the initialValue parameter of my method which sorts an array:

public static void bubble(int[] array, int initialValue) {

    int aux;

    for (int i = 1; i < array.length; i++) {
        if (array[i - 1] > array[i]) {
            aux = array[i - 1];
            array[i - 1] = array[i];
            array[i] = aux;
        }
    }

    if (initialValue < array.length) {
        bubble(array, ++initialValue);
    }
}

I call the function using these parameters:

bubble(array, 0);

I want to remove the second parameter because it always starts with 0, and increments every time the method is called.

How can I do that?

I'm using Java, but don't want to use API methods.

share|improve this question
1  
If this is a homework question, add a 'homework' tag to it. Otherwise; the correct answer to "don´t wanna use API methods" is "You may not want to, but you should anyway". –  abuzittin gillifirca Mar 17 '13 at 13:12
2  
The API methods have been highly optimized and fine-tuned over a decade or more, and are all based on sound computer-science principles developed and refined over half a century. They're certainly all going to be way faster than bubble sort. Why would you not want to use them, unless you're doing homework to demonstrate you understand how to implement a particular sorting algorithm? –  GordonM Mar 17 '13 at 13:17
1  
    
yes, it is a homework, its done but i want to improve my code, to take of my second parameter... i want to show this algorithm to run in any languagem –  Diego Macario Mar 17 '13 at 15:57
add comment

closed as off-topic by Jamal Dec 14 '13 at 19:47

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking for code to be written to solve a specific problem are off-topic here as there is no code to review." – Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers

The usual way of doing this is, i.e. hiding internal parameters, is very simple: you make that method private to hide it, and define a public one that doesn't expose the internal parameters.

// your internal method
private static void bubble(int[] array, int initialValue) {
  // ...
}

// the public version that hides the internal parameter(s)
public static void bubble(int[] array) {
  bubble(array, 0);
}
share|improve this answer
add comment

Bubblesort is probably one of the really bad examples to try recursion.
My suggested recursion pattern for bubblesort would be:

function bubblesort (input: some integer array):
  if input is empty:
    return input
  else:
    do one bubble sort run for input
    subarray = bubblesort(unsorted part of input)
    return subarray append sorted part of input

In this way, we will sort the whole array piecewise for each call.

Why does it work? Because every bubble sort run, we will put at least the largest element to the rightmost index.
We know that all elements until the last swap are in unknown state, all after the last swap are sorted.

In Java code:

/** Sorts the given array with a recursive bubblesort. Modifies the given array */
public static int[] bubbleSort(final int[] array) {
    if (array.length == 0)
        return array;

    int lastSwapIndex = 0;
    for (int i = 1; i < array.length; i++) {
        if (array[i - 1] > array[i]) {
            final int temp = array[i - 1];
            array[i - 1] = array[i];
            array[i] = temp;
            lastSwapIndex = i;
        }
    }
    System.arraycopy(bubbleSort(Arrays.copyOfRange(array, 0, lastSwapIndex)), 0, array, 0, lastSwapIndex);
    return array;
}

(To create a non-modifying version should be quite easy for every reader, just create a copy before modifying it)

Nevertheless, I would suggest to use a list. It would look like this:

/** Sorts the given list with a recursive bubblesort. Modifies the given list */
public static List<Integer> bubbleSort(final List<Integer> list) {
    if (list.isEmpty())
        return list;

    int lastSwapIndex = 0;
    for (int i = 1; i < list.size(); i++) {
        if (list.get(i - 1) > list.get(i)) { //slow if no random access list
            final int temp = list.get(i - 1);
            list.set(i - 1, list.get(i));
            list.set(i, temp);
            lastSwapIndex = i;
        }
    }
    bubbleSort(list.subList(0, lastSwapIndex));
    return list;
}

Or if we do not want to modifiy the given list:

/** Sorts the given list with a recursive bubblesort. */
public static List<Integer> bubbleSort(final List<Integer> list) {
    if (list.isEmpty())
        return list;

    int lastSwapIndex = 0;
    for (int i = 1; i < list.size(); i++) {
        if (list.get(i - 1) > list.get(i)) { //slow if no random access list
            final int temp = list.get(i - 1);
            list.set(i - 1, list.get(i));
            list.set(i, temp);
            lastSwapIndex = i;
        }
    }
    final List<Integer> result = bubbleSort(new ArrayList<>(list.subList(0, lastSwapIndex)));
    result.addAll(list.subList(lastSwapIndex, list.size()));
    return result;
}
share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.