Search code examples
javaalgorithmsortingbubble-sort

Bidirectional Bubble Sort


enter image description here

I have to solve the following problem. So initially i had a bubble sort and now i modified it to make it bidirectional. The following is my solution.

public void bubbleSort() {
    int temp;
    int out;
    int outNew = 0;
    int in;

    for (out = nElems - 1; out > outNew; out--) {

        for (in = 0; in < out; in++) {

            if (a[in] > a[in + 1]) {
                temp = a[in + 1];
                a[in + 1] = a[in];
                a[in] = temp;
            }
        }

        for (int j = in - 1; j > outNew; j--) {

            if (a[j] < a[j - 1]) {
                temp = a[j];
                a[j] = a[j - 1];
                a[j - 1] = temp;
            }

        }

        outNew++;

    }

}

When i call my bubble sort to sort few random numbers in an array i created it seems to be sorting fine. My question is rather to all you developers whether my solution is satisfying the question posted above and also what could i have done differently to make this solution more effective (if possible) . I am sorry if this is a little open question , i am usually on here looking for hints and suggestions rather than code as it helps me learn better. I appreciate all answers and am open to any suggestions.


Solution

  • Your first inner loop seems a bit inefficient, since your array will be partially sorted at both ends. After the first round (one time incrementing the index, one time decreasing it) the first and the last element will already be correct, hence no need to start at index 0 (and the task/exercise requires that btw).

    The following ASCII art demonstrates, on which indices the algorithm should operate on at the example of a array with 9 elements (including the indices reached with in+1 and j-1; all indices between the | should be considered):

    position:   0    1    2    3    4    5    6    7    8
    ------------------------------------------------------------
              |                   ->                       |
              |                   <-                 |
                   |              ->                 |
                   |              <-             |
                        |         ->             |
                        |         <-        |
                             |    ->        |
                             |    <-   |
    

    But what your algorithm does is:

    position:   0    1    2    3    4    5    6    7    8
    ------------------------------------------------------------
              |                     ->                     |
              |                     <-                     |
              |                     ->                |
                   |                <-                |
              |                     ->           |
                        |           <-           |
              |                     ->      |
                             |      <-      |
    

    You'll have to fix the the initial index of the first inner for loop.