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

Java 为什么双向冒泡排序的一个版本比另一个更快?,java,sorting,bidirectional,Java,Sorting,Bidirectional,我有两个双向冒泡排序(鸡尾酒排序)的Java实现,用于对n个整数的列表进行排序 第一种是香草鸡尾酒 第二种方法是在每次传递之后增加一个左缓冲区或右缓冲区,因为在每次左缓冲区或右缓冲区之后,保证只对一个项目进行排序 奇怪的是,第二个实现要慢得多(对于100k整数,分别为15秒和18秒) 样品一:(“香草”) int n=100000//数组长度,数组中的最大元素 //创建一个包含一百万个随机整数的数组 int[]数组=新的int[n]; 对于(int i=0;i0;i--) { a=阵列[i

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

  • 第一种是香草鸡尾酒
  • 第二种方法是在每次传递之后增加一个左缓冲区或右缓冲区,因为在每次左缓冲区或右缓冲区之后,保证只对一个项目进行排序
奇怪的是,第二个实现要慢得多(对于100k整数,分别为15秒和18秒)

样品一:(“香草”)

int n=100000//数组长度,数组中的最大元素
//创建一个包含一百万个随机整数的数组
int[]数组=新的int[n];
对于(int i=0;ib)//如果所选的两个元素顺序不正确
{
数组[i]=b;//交换元素
数组[i+1]=a;
changed=true;//标记已进行更改
totalSwaps++;//更新所做更改的数量
}
}
}
else//从右向左传递
{
对于(inti=(array.length-1);i>0;i--)
{ 
a=阵列[i-1];
b=数组[i];
if(a>b)//如果所选的两个元素顺序不正确
{
数组[i-1]=b;//交换元素
数组[i]=a;
changed=true;//标记已进行更改
totalSwaps++;//更新所做更改的数量
}
}
}//结束此过程的排序
TotalPass++;
}同时(改变);
long-endTime=System.currentTimeMillis();
System.out.println();
System.out.println(“完成了对“+n+”整数的排序。(“+(双精度)(结束时间开始时间)/1000+”秒以进行“+totalSwaps+”交换“+TotalPass+”传递)”);
示例二:(带递增缓冲区)

int n=100000//数组长度,数组中的最大元素
//创建一个包含一百万个随机整数的数组
int[]数组=新的int[n];
对于(int i=0;ib)//如果所选的两个元素顺序不正确
{
数组[i]=b;//交换元素
数组[i+1]=a;
changed=true;//标记已进行更改
totalSwaps++;//更新所做更改的数量
}
}
rightBuffer++;//增加
}
else//从右向左传递
{
对于(inti=((array.length-1)-rightBuffer;i>leftBuffer;i--)
{ 
a=阵列[i-1];
b=数组[i];
if(a>b)//如果所选的两个元素顺序不正确
{
数组[i-1]=b;//交换元素
数组[i]=a;
changed=true;//标记已进行更改
totalSwaps++;//更新所做更改的数量
}
}
leftBuffer++;
}//结束此过程的排序
TotalPass++;
}while(已更改);//末端分拣回路
long-endTime=System.currentTimeMillis();
System.out.println();
System.out.println(“完成了对“+n+”整数的排序。(“+(双精度)(结束时间开始时间)/1000+”秒以进行“+totalSwaps+”交换“+TotalPass+”传递)”);

有人能向这个Java noob解释一下发生了什么吗?

猜测合法吗。。。。第一个循环中的for循环取决于循环中不变的值。也就是说,编译器知道在运行之前将进行多少次循环。在东南部
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)");