package bench;

class BidirectionalBubbleSortAlgorithm
    extends SortAlgorithm
{
    public BidirectionalBubbleSortAlgorithm()
    {
        super("Bidirectional Bubble Sort");
    }

    public final void performTest()
    {
        int j;
        int limit = a.length;
        int st = -1;
        //int _a[] = a;

        while(st < limit)
        {
                boolean flipped = false;
            st++;
                limit--;

                for (j = st; j < limit; j++)
                {
                        if (a[j] > a[j + 1])
                        {
                            int T = a[j];

                            a[j] = a[j + 1];
                            a[j + 1] = T;
                            flipped = true;
                        }
                }

                if(!flipped)
                {
                        return;
                }

                for(j = limit; --j >= st;)
                {
                        if(a[j] > a[j + 1])
                        {
                            int T = a[j];

                            a[j] = a[j + 1];
                            a[j + 1] = T;
                            flipped = true;
                        }
                }

                if(!flipped)
                {
                        return;
            }
            }
    }
}
