为什么一个版本的双向冒泡排序比另一个更快?

时间:2014-04-04 16:26:13

标签: java sorting bidirectional

我有两个用于对n个整数列表进行排序的双向冒泡排序(鸡尾酒排序)的Java实现。

  • 第一个是“香草”鸡尾酒。
  • 第二个在每次传递后递增一个左或右缓冲区,因为在每次传递后,确保一个项目被排序。

奇怪的是,第二个实现要慢得多(对于100k整数,分别为~15和~18秒)。

示例一:(“香草”)

int n = 100000; //length of array, largest element in array

        //create an array with one million random integers
        int[] array = new int[n];
        for (int i = 0; i < array.length; i++) //set each item...
        { 
            array[i] = (int)((Math.random() * n) + 1); //...to a random int 1 to n inclusive
        }

        long startTime = System.currentTimeMillis(); // start timer

        boolean changed = true; //to flag if any changes were made
        int a; // to hold each of two items
        int b;
        long totalPasses = 0; // total passes over the array
        long totalSwaps = 0; // total number of times two elements were swapped

        do
        {
            changed = false;

            if (totalPasses % 2 == 0) // pass from left to right
            {
                for (int i = 0; i < (array.length-1); i++)
                { 
                    a = array[i];
                    b = array[i+1];

                    if (a > b) // if the two selected elements are not in order
                    {
                        array[i] = b; // swap the elements
                        array[i+1] = a;
                        changed = true; // flag that a change has been made
                        totalSwaps++; // update the number of changes made
                    }
                }
            }
            else // pass from right to left
            {
                for (int i = (array.length-1); i > 0; i--)
                { 
                    a = array[i-1];
                    b = array[i];

                    if (a > b) // if the two selected elements are not in order
                    {
                        array[i-1] = b; // swap the elements
                        array[i] = a;
                        changed = true; // flag that a change has been made
                        totalSwaps++; // update the number of changes made
                    }
                }
            } // end sorting for this pass

        totalPasses++;
        } while (changed);

        long endTime = System.currentTimeMillis();

        System.out.println();
        System.out.println("Finished sorting " + n + " integers. (" + (double)(endTime-startTime)/1000 + " seconds to make " + totalSwaps + " swaps over " + totalPasses + " passes)");

示例二:(使用递增缓冲区)

int n = 100000; //length of array, largest element in array

        //create an array with one million random integers
        int[] array = new int[n];
        for (int i = 0; i < array.length; i++) //set each item...
        { 
            array[i] = (int)((Math.random() * n) + 1); //...to a random int 1 to n inclusive
        }

        /*
         * at the end of each left to right pass, increment the right buffer
         * at the end of each right to left pass, increment the left buffer
         */

        long startTime = System.currentTimeMillis(); // start timer

        boolean changed = true; //to flag if any changes were made
        int a; // to hold each of two items
        int b;
        int leftBuffer = 0; // distance from each side that is already sorted
        int rightBuffer = 0;
        long totalPasses = 0; // total passes over the array
        long totalSwaps = 0; // total number of times two elements were swapped

        do // main sorting loop
        {
            changed = false;

            if (totalPasses % 2 == 0) // pass from left to right
            {
                for (int i = leftBuffer; i < ((array.length-1)-rightBuffer); i++)
                { 
                    a = array[i];
                    b = array[i+1];

                    if (a > b) // if the two selected elements are not in order
                    {
                        array[i] = b; // swap the elements
                        array[i+1] = a;
                        changed = true; // flag that a change has been made
                        totalSwaps++; // update the number of changes made
                    }
                }
                rightBuffer++; // increment the cover for
            }
            else // pass from right to left
            {
                for (int i = ((array.length-1)-rightBuffer); i > leftBuffer; i--)
                { 
                    a = array[i-1];
                    b = array[i];

                    if (a > b) // if the two selected elements are not in order
                    {
                        array[i-1] = b; // swap the elements
                        array[i] = a;
                        changed = true; // flag that a change has been made
                        totalSwaps++; // update the number of changes made
                    }
                }
                leftBuffer++;
            } // end sorting for this pass

            totalPasses++;
        } while (changed); // end sorting loop

        long endTime = System.currentTimeMillis();

        System.out.println();
        System.out.println("Finished sorting " + n + " integers. (" + (double)(endTime-startTime)/1000 + " seconds to make " + totalSwaps + " swaps over " + totalPasses + " passes)");

有人可以向这个Java noob解释发生了什么吗?

0 个答案:

没有答案