冒泡排序不排序 - IntDoublePair

时间:2015-01-09 21:43:04

标签: sorting hadoop

我已经实现了冒泡排序,以便对IntDoublePair进行排序。例如:

[1 0.5]
[1 0.8]
[1 0.67]

排序为:

[1 0.5]
[1 0.67]
[1 0.8]

当我执行代码时,它给了我不按排序顺序排列的数据。我很困惑,我哪里出错了。我需要一些帮助。

private ArrayList<IntDoubleTextPair> sortCollection(ArrayList<IntDoubleTextPair> collection)
    {
        for (int current = 0; current < collection.size(); current++)
        {
            for (int next = 1; next < collection.size(); next++)
            {
                if (collection.get(current).getFirst().get() >= collection.get(next).getFirst().get())
                {
                    if (collection.get(current).getSecond().get() > collection.get(next).getSecond().get())
                    {
                        temp = collection.get(next);

                        collection.set(next, collection.get(current));
                        collection.set(current, temp);
                    }
                }
}
}
return collection;
}

2 个答案:

答案 0 :(得分:0)

我认为你可能有一些错误的逻辑:

  if (collection.get(current).getFirst().get() >= collection.get(next).getFirst().get())
  {
      if (collection.get(current).getSecond().get() > collection.get(next).getSecond().get())
      {
          temp = collection.get(next);
          collection.set(next, collection.get(current));
          collection.set(current, temp);
      }
  }

使用这两个嵌套的if语句,在这个例子中不会发生切换,但它需要:

[2 .1]
[1 .9]

因为即使第一个if语句的计算结果为true,第二个if语句也会失败,因为它只查看第二个数字。也许这会更好:

  if (collection.get(current).getFirst().get() >= collection.get(next).getFirst().get())
  {
      if (collection.get(current).getFirst().get() == collection.get(next).getFirst().get())
      {
          if (collection.get(current).getSecond().get() > collection.get(next).getSecond().get())
          {
          temp = collection.get(next);

          collection.set(next, collection.get(current));
          collection.set(current, temp);
          }
      }

      else
      {
          temp = collection.get(next);

          collection.set(next, collection.get(current));
          collection.set(current, temp);
      }
  }

快乐的编码!如果您有任何问题,请发表评论。

答案 1 :(得分:0)

我认为排序应该至少对你传递的数组起作用(尽管嵌套if条件存在问题),但是在交换操作中传递对temp的引用时可能会出现问题,所以尝试创建一个temp对象而不是传递引用传递值:

private ArrayList<IntDoubleTextPair> sortCollection(ArrayList<IntDoubleTextPair> collection)
    {
        for (int current = 0; current < collection.size(); current++)
        {
            for (int next = 1; next < collection.size(); next++)
            {
                if (collection.get(current).getFirst().get() >= collection.get(next).getFirst().get())
                {

                    if (collection.get(current).getSecond().get() > collection.get(next).getSecond().get())
                    {
                        IntDoubleTextPair temp=  new IntDoubleTextPair();
                        temp.getFirst() = collection.get(next).getFirst();
                        temp.getSecond() = collection.get(next).getSecond();

                        collection.set(next, collection.get(current));
                        collection.set(current, temp);
                    }
                    else if(collection.get(current).getFirst().get() != collection.get(next).getFirst().get()){
                        IntDoubleTextPair temp=  new IntDoubleTextPair();
                        temp.getFirst() = collection.get(next).getFirst();
                        temp.getSecond() = collection.get(next).getSecond();

                        collection.set(next, collection.get(current));
                        collection.set(current, temp);

                        }
                }
}
}
return collection;
}