如何使用比较器来实现冒泡排序?

发布于 2024-11-01 01:54:59 字数 838 浏览 1 评论 0原文

如何使用比较器实现冒泡排序?

谢谢。

这就是我的比较器的样子:

class ColumnSorter implements Comparator {
  int colIndex;

  ColumnSorter(int colIndex) {
    this.colIndex = colIndex;
  }

  public int compare(Object a, Object b) {
  Vector v1 = (Vector) a;
  Vector v2 = (Vector) b;
  Object o1 = v1.get(colIndex);
  Object o2 = v2.get(colIndex);

  if (o1 instanceof String && ((String) o1).length() == 0) {
    o1 = null;
  }
  if (o2 instanceof String && ((String) o2).length() == 0) {
    o2 = null;
  }

  if (o1 == null && o2 == null) {
    return 0;
  } else if (o1 == null) {
    return 1;
  } else if (o2 == null) {
    return -1;
  } else if (o1 instanceof Comparable) {
      return ((Comparable) o1).compareTo(o2);
  } else {
    return o1.toString().compareTo(o2.toString());
 }
}
}

How can i implement Bubble sort by using Comparator?

Thank you.

This is how my comparator looks:

class ColumnSorter implements Comparator {
  int colIndex;

  ColumnSorter(int colIndex) {
    this.colIndex = colIndex;
  }

  public int compare(Object a, Object b) {
  Vector v1 = (Vector) a;
  Vector v2 = (Vector) b;
  Object o1 = v1.get(colIndex);
  Object o2 = v2.get(colIndex);

  if (o1 instanceof String && ((String) o1).length() == 0) {
    o1 = null;
  }
  if (o2 instanceof String && ((String) o2).length() == 0) {
    o2 = null;
  }

  if (o1 == null && o2 == null) {
    return 0;
  } else if (o1 == null) {
    return 1;
  } else if (o2 == null) {
    return -1;
  } else if (o1 instanceof Comparable) {
      return ((Comparable) o1).compareTo(o2);
  } else {
    return o1.toString().compareTo(o2.toString());
 }
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长安忆 2024-11-08 01:54:59

您可以像 < 一样实现冒泡排序。然后将 < 的使用替换为 someComparator.compare(Object o1, Object o2)

这是“翻译规则”:

if (arr[i] < arr[j]) {
    ...
}

变为

if (someComparator.compare(arr[i], arr[j]) < 0) {
    ...
}

(如果您使用 >,则将使用 > 0 而不是 < 0

)应查阅 Comparator 的文档 了解更多详情。这是第一句话:

比较两个参数的顺序。当第一个参数小于、等于或大于第二个参数时,返回负整数、零或正整数。

You implement bubble sort just as you would with, say <. Then you replace the use of < with someComparator.compare(Object o1, Object o2).

Here's the "translation rule":

if (arr[i] < arr[j]) {
    ...
}

becomes

if (someComparator.compare(arr[i], arr[j]) < 0) {
    ...
}

(If you used >, you would use > 0 instead of < 0.)

You should consult the documentation for Comparator for further details. Here's the first sentence:

Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

月朦胧 2024-11-08 01:54:59

我假设您知道如何编写冒泡排序代码,并且问题是如何使用比较器。

您都必须比较两个项目来决定它们的顺序。

在冒泡排序的每次迭代中, item2 then

你现在可以写:

if( comparator.compare(item1,item2) < 0 ) {

如果你会说:

if item1 >; item2 then

然后你写

if( comparator.compare(item1,item2) > 0 ) {

请注意 <>< /code> 保持不变,并且项目的顺序保持不变。如果您遵守该规则,则比较应该可以正常工作,因此剩下的就是实际的冒泡排序。

I'll assume you know how to code a Bubble Sort and that the problem is how to use a Comparator.

At every iteration in the bubble sort you have to compare two items to decide which order they should be in. Where you would say:

if item1 < item2 then

you now write:

if( comparator.compare(item1,item2) < 0 ) {

If you would say:

if item1 > item2 then

then you write

if( comparator.compare(item1,item2) > 0 ) {

Note that the < and > remain the same and you keep the order of the items the same. If you stick to that rule the comparison should work fine, so all that is left is the actual bubble sort.

魄砕の薆 2024-11-08 01:54:59

最大的问题是你到底为什么要自己实现冒泡排序。冒泡排序是最慢的排序算法之一。

使用 java.util.Collections.sort() 会更快并且为您节省相当多的代码行。 Collections.sort() 有一个采用 Comparator 的方法,或者没有该方法的方法使用对象的自然排序(如果它们实现了 Comparable界面)。

The big question is why on earth you would want to implement a bubble sort by yourself. Bubble sort is one of the slowest sorting algorithms around.

Using the sorting algorithm provided by the JDK in java.util.Collections.sort() would be much faster and save you quite some lines of code. Collections.sort() has a method that takes a Comparator, or one without that uses the natural ordering of objects (if they implement the Comparable interface).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文