views:

119

answers:

2

I need to implement the bidirectional bubble sort in my code.

In other words in will go from left to right first carrying the largest value.

But when it reaches out, it should reverse and go from right to left carrying the smallest value.

I am advised to to implement another out index in addition the current one.

This is what I have so far - just 2 loops. I am guessing I have to combine them somehow?

    public void bubbleSort() {
    int out, in; // nElems in my case is 4, because I have 4 elements in my array

    for(out=nElems-1; out>1; out--) // outer loop backward
        for(in=out; in>1; in--) // inner loop backward
            if(a[in] < a[in-1])
                swap(in, in-1);

    for(out=0; out<nElems; out++) // outer loop forward
        for(in=0; in<out; in++) // inner loop forward
            if(a[in] > a[in+1])
                swap(in, in+1); 
+1  A: 
    public void bidirectionalBubbleSort()
    {
       int left = 0, right = a.length-1;
       while (left < right)
       {
          for (int pos = left; pos < right; pos++)
          {
             if (a[pos] > a[pos+1])
                swap(pos, pos+1);
          }
          right--;


          for (int pos = right; pos > left; pos--)
          {
             if (a[pos] < a[pos-1])
               swap(pos, pos-1);
          }
          left++;
       }
   }  
jlewis42
This line is causing Array out of bounds exception: if (a[pos] > a[pos+1])
Silence of 2012
my bad, I was decrementing left instead of incrementing it
jlewis42
+1  A: 

I recommend you split the method up for chunks that you can comprehend, like:

public static boolean swap(int[] numbers, int i, int j) {
    int temp = numbers[i];
    numbers[i] = numbers[j];
    numbers[j] = temp;
    return true;
}

static boolean leftSide(int[] numbers, int i, int j) {
    boolean swapped = false;
    for (int k = i; k < j; k++)
        if (numbers[k] > numbers[k + 1])
            swapped = swap(numbers, k, k + 1);
    return swapped;
}

static boolean rightSide(int[] numbers, int i, int j) {
    boolean swapped = false;
    for (int k = j; k > i; k--)
        if (numbers[k] < numbers[k - 1])
            swapped = swap(numbers, k, k - 1);
    return swapped;
}

public static void cocktailSort(int[] numbers) {
    boolean swapped = true;
    int i = -1;
    int j = numbers.length - 1;

    while (i++ < j && swapped)
        if (swapped = leftSide(numbers, i, j))
            swapped = rightSide(numbers, i, j--);
}

And to test it:

public static void main(String[] args) {
    int x[] = new int[] { 2, 6, 3, 7, 8, 3, 7, 5, 4 };
    cocktailSort(x);
    System.out.println(java.util.Arrays.toString(x));
}

Output:

[2, 3, 3, 4, 5, 6, 7, 7, 8]
Margus
If you're gonna return the sorted list, then the original list should remain untouched. Clone it or something, or have the sort method return something else, even nothing. (If it's void, for example, its modifying the original list should be obvious to anyone with a clue.)
cHao
Thats a good point. Code now updated to accommodate that change.
Margus