这段代码是Bubble Sort的正确实现吗? [英] Is this code a correct implementation of Bubble Sort?

查看:104
本文介绍了这段代码是Bubble Sort的正确实现吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被用来按照气泡排序算法进行排序. 这个算法正确吗?

I am used following bubble sort algorithm for making sorting . Is this algorithm correct?

for (int a = itemWiseBidderList.size() - 1; a > 1; a--)
{
    for (int j = 0; j < a; j++)
    { 
        if ((itemWiseBidderList.get(j).getRankInInt()) > (itemWiseBidderList.get(j + 1).getRankInInt()))
        {
           Collections.swap(itemWiseBidderList, j, j + 1);
        }
    }
}

推荐答案

如果不是必须进行冒泡排序(通过功课?),那么在Java中实现排序的正确方法是调用

If bubble sorting is not a requirement (by homework?), then the correct way to implement sorting in Java is by calling

Collections.sort(itemWiseBidderList);

如果您的列表项实现了Comparable

If your list items implement Comparable, or

Collections.sort(itemWiseBidderList, new Comparator() {
    public int compare(Object o1, Object o2) {
        // Compare o1, o2 .getRankInInt() here
    }
});

这将比气泡排序快得多.

This will be a lot faster than bubble sorting.

这篇关于这段代码是Bubble Sort的正确实现吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆