Java中的双向冒泡排序?

时间:2010-09-30 15:38:29

标签: java algorithm sorting bubble-sort

我需要在我的代码中实现双向冒泡排序。

换句话说,in将从左到右首先带有最大值

但是当它到达out时,它应该反转并从右到左带有最小值

我建议除了当前的之外还要实现另一个out索引。

这是我到目前为止 - 只有2个循环。我猜我必须以某种方式将它们结合起来?

    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); 

5 个答案:

答案 0 :(得分:2)

    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++;
       }
   }  

答案 1 :(得分:1)

我建议您将方法拆分为可以理解的块,例如:

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--);
}

并测试它:

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));
}

输出:

[2, 3, 3, 4, 5, 6, 7, 7, 8]

答案 2 :(得分:0)

    boolean f1 = false, f2 = false;
    outer:
    for (int i=0; i < arr.length-1; i++)
           for (int j=i; j< arr.length - i -1; j++) {

               if(arr[j] >= arr[j+1]){
                   f1 = true;
                   int t = arr[j];
                   arr[j] = arr[j+1];
                   arr[j+1] = t;
               }

               if(arr[arr.length - j -1] <= arr[arr.length - j - 2]){

                   f2 = true;
                   int t = arr[arr.length - j -2];
                   arr[arr.length - j -2] = arr[arr.length - j -1];
                   arr[arr.length -j -1] = t;
               }

               /**
                * @param k: iterator variable thats prints each pass..(optional)
                */
               for (int k:arr)
                   System.out.print(" "+k);
               System.out.println("    "+i);

               //Ultimate break condition
               if(j == arr.length - j -2 && (!f1 && !f2))
                   break outer;


           }

答案 3 :(得分:0)

双向冒泡排序仅使用 2循环&amp; 2个索引变量。

public void bubbleSort(){
    for(int out=0;out<nElems/2;out++){
        boolean forward = true;
        for (int in = out;in !=(forward ? out-1 : out)
                         ;in = forward ? in + 1 : in - 1)
        {
            if (in == nElems - (out + 1))
                forward = false;
            if (a[forward ? in + 1 : in] < a[forward?in:in-1])
                swap(forward ? in + 1 : in - 1, in);
        }
    }
}

答案 4 :(得分:0)

双向冒泡排序,排序变量:array []

//-------------------------------------------//
//biderctioanal bubble sort - coctail sort
public void bidirBubbleSort(){
    for(int i=1; i<length/2; i++){
        for(int j=0; j<length-i; j++)
            if(array[j]>array[j+1])
                swap(j, j+1);
        for(int j=length-i; j>=i; j--)
            if(array[j]<array[j-1])
                swap(j, j-1);
    }
}
//-------------------------------------------//
//swap 2 elements
public void swap(int index1, int index2){
    int temp=array[index1];
    array[index1]=array[index2];
    array[index2]=temp;
}
//-------------------------------------------//

在10_000个随机选择的元素上,标准气泡排序在410毫秒完成,双向气泡排序在319毫秒