如何使用比较器来实现冒泡排序?
如何使用比较器实现冒泡排序?
谢谢。
这就是我的比较器的样子:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以像
<
一样实现冒泡排序。然后将<
的使用替换为someComparator.compare(Object o1, Object o2)
。这是“翻译规则”:
变为
(如果您使用
>
,则将使用> 0
而不是< 0
。)应查阅 Comparator 的文档 了解更多详情。这是第一句话:
You implement bubble sort just as you would with, say
<
. Then you replace the use of<
withsomeComparator.compare(Object o1, Object o2)
.Here's the "translation rule":
becomes
(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:我假设您知道如何编写冒泡排序代码,并且问题是如何使用比较器。
您都必须比较两个项目来决定它们的顺序。
在冒泡排序的每次迭代中, 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.最大的问题是你到底为什么要自己实现冒泡排序。冒泡排序是最慢的排序算法之一。
使用
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 aComparator
, or one without that uses the natural ordering of objects (if they implement theComparable
interface).