【问题标题】:Modified bubble sort, but not working correctly修改冒泡排序,但不能正常工作
【发布时间】:2014-10-05 05:10:09
【问题描述】:

任务是修改冒泡排序,使其具有双向性。

这意味着“in”索引将首先从左到右携带最大的项目,但是当它到达“out”时,它会反转并从右到左携带最小的项目。

bubbleSort() 方法只是它在示例中呈现的常规方式。

我的代码在 bidiBubbleSort() 方法中。

由于某种原因,当我运行程序时,它给了我一个已排序但不正确的输出。

我用铅笔在一张纸上手动完成每一步,但我不知道我忽略了什么。

未排序:7 6 5 4 3 2 1

排序:6 4 2 1 3 5 7

class ArrayBub
{
   private long[] a;                            // ref to array a
   private int nElems;                          // number of data items
// --------------------------------------------------------------
   public ArrayBub(int max)                     // constructor
   {
      a = new long[max];                        // create the array
      nElems = 0;                               // no items yet
   }
// --------------------------------------------------------------
   public void insert(long value)               // put element into array
   {
      a[nElems] = value;                        // insert it
      nElems++;                                 // increment size
   }
// --------------------------------------------------------------
   public void display()                        // displays array contents
   {
      for(int j=0; j<nElems; j++)               // for each element,
         System.out.print(a[j] + " ");          // display it
      System.out.println("");
   }
// Beginning of my code -------------------------------------------------------
   public void bidiBubbleSort()
   {
      int out, x, y;
      int in = 0;

      for(x=0, out=nElems-1; out>x; out--, x++) // outer loop (backward)
         for(in=x; in<out+1; in++)              // inner loop (forward)
            if( in<out )
               if( a[in] > a[in+1] )            // out of order?
                  swap(in, in+1);               // swap them
               else                             // (in==out)
                  for(y=out-1; y>x; y--)        // reverse 
                     if( a[y] < a[y-1] )
                        swap(y, y-1);
   }
// End of my code -------------------------------------------------------------
   public void bubbleSort()
   {  
      int out, in;

      for(out=nElems-1; out>1; out--)            // outer loop (backward)
         for(in=0; in<out; in++)                 // inner loop (forward)
            if( a[in] > a[in+1] )                // out of order?
               swap(in, in+1);                   // swap them
   }                                             // end bubbleSort()

   private void swap(int one, int two)
   {
      long temp = a[one];
      a[one] = a[two];
      a[two] = temp;
   }
// --------------------------------------------------------------
}                                               // end class ArrayBub

class BubbleSortApp
{
   public static void main(String[] args)
      {
      int maxSize = 100;                        // array size
      ArrayBub arr;                             // reference to array
      arr = new ArrayBub(maxSize);              // create the array

      arr.insert(7);                            // insert 7 items
      arr.insert(6);
      arr.insert(5);
      arr.insert(4);
      arr.insert(3);
      arr.insert(2);
      arr.insert(1);

      arr.display();                            // display items

      arr.bidiBubbleSort();                     // bidirectional bubble sort

      arr.display();                            // display them again
      }                                         // end main()
}                                               // end class BubbleSortApp

【问题讨论】:

    标签: java sorting bubble-sort


    【解决方案1】:

    双向冒泡排序或鸡尾酒排序的主要目的是解决未排序列表末尾的最低值(又名海龟)问题,以便比传统冒泡排序更快地移动到列表开头。

    牢记这一目标,您需要从两端了解双向冒泡排序。即,开始到列表末尾,结束到列表开头

    您当前的实现似乎未能成功地调整传统的冒泡排序。我的建议是忘记冒泡排序和代码的实现,并牢记上述目标 w.r.to 冒泡排序。

    下面是一个提示:

    使用要检查的条件动态决定外部循环的退出 列表是否已排序。不要依赖柜台上的 像传统冒泡排序一样的外循环。

    如果您仍然无法解决它,请查看维基百科文章,然后实施:http://en.wikipedia.org/wiki/Cocktail_sort解决不了的才看!

    【讨论】:

      猜你喜欢
      • 2020-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-30
      • 1970-01-01
      • 2013-04-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多