【问题标题】:Bubble sort too long?冒泡排序太长?
【发布时间】:2016-02-09 16:23:56
【问题描述】:

这是我执行冒泡排序的代码:

public static void bubbleSortByLength() {
    boolean flag = true;
    String temp;
    for (int i = numNames - 1; i > 0; i--) {
        flag = false;
        for (int j = 0; j < i - 1; j++) {
            if (names[j].length() > names[j + 1].length()) {
                temp = names[j];
                names[j] = names[j + 1];
                names[j + 1] = temp;
                flag = true;
            }
        }
        if (!flag)
            return;
    }
}

与我见过的其他冒泡排序迭代相比,它相当慢。有谁知道我怎样才能让它更快?

【问题讨论】:

  • 无论你怎么尝试,冒泡排序总是 O(n^2)。
  • 对大型数据集使用合并排序或快速排序。
  • 与我见过的其他冒泡排序迭代相比,它相当慢 然后使用其中之一。你在问什么究竟?
  • 那么我想我的问题应该是:这是否像冒泡排序一样有效?这是正确的冒泡排序吗?

标签: java bubble-sort


【解决方案1】:

我建议的第一件事是您创建自定义Comparator 以围绕您的排序实现。鉴于您的 String 长度的主要标准,我会使用类似

static class StringLengthComparator implements Comparator<String> {
    @Override
    public int compare(String o1, String o2) {
        if (o1.length() == o2.length()) {
            return o1.compareTo(o2);
        }
        return Integer.compare(o1.length(), o2.length());
    }
}

接下来我要推荐的是基于更好的排序算法来实现一些东西。 Quicksort 和 Mergesort 通常是 O(nlog n)(尽管 Quicksort 通常对排序输入有一个病态的情况,导致 O(n^2) 行为)。接下来,为了更容易阅读,我建议您将swap 提取到一个方法中。喜欢,

static void swap(String[] arr, int a, int b) {
    if (a == b) {
        return;
    }
    String t = arr[a];
    arr[a] = arr[b];
    arr[b] = t;
}

然后你可以使用上面的Comparator 和swap 方法来实现一个冒泡排序

public static void bubbleSortByLength(String[] names) {
    int numNames = names.length;
    StringLengthComparator c = new StringLengthComparator();
    for (int i = 0; i < numNames - 1; i++) {
        boolean swappedFlag = false;
        for (int j = 1; j < numNames - i; j++) {
            if (c.compare(names[j - 1], names[j]) > 0) {
                swappedFlag = true;
                swap(names, j - 1, j);
            }
        }
        if (!swappedFlag) {
            break;
        }
    }
}

【讨论】:

    【解决方案2】:

    **高级更快的冒泡排序**

            /*Advanced BUBBLE SORT with ONE PASS*/
    /*Authored by :: Brooks Tare  AAU*/
    
    public class Bubble {
    
        public int[] bubble(int b[]){ 
        int temp=0; 
    
        for(int i=0;i<b.length-1;i++){
    
                if(b[i]>b[i+1] ){
                    ///swap(b[i],b[i+1]);
    
                    temp=b[i];
                    b[i]=b[i+1];
                    b[i+1]=temp;
    
        /*Checking if there is any number(s) greater than 
          the current number. If there is swap them.*/
                    while(i>0){
    
    
                        if(b[i]<b[i-1]){
                        ///swap(b[i]<b[i-1])
                            int temp1;
                            temp1=b[i];
                            b[i]=b[i-1];
                            b[i-1]=temp1;
                            i--;
                        }
                        else if(b[i]>b[i-1]){i--;}
                    }
                }
                else{continue;}
    
            }
    
    
    
            return b;
        }
    ///the following is a function to display the Array 
            public void see(int []a){
                for(int j=0;j<a.length;j++){
                    System.out.print(a[j]+",");
                }
            }
    
    
    
        public static void main(String []args){
            ///You can change the Array to your preference.. u can even make it dynamic 
    
            int b[]={5,1,4,2,0}; 
            int v[]=new int[100]; 
            Bubble br=new Bubble();
            v=br.bubble(b);
            br.see(v);
    
            }
        }
    

    【讨论】:

    • 虽然实现可能会提供有用的信息,但请考虑添加解释以使答案更加独立。
    猜你喜欢
    • 1970-01-01
    • 2013-10-09
    • 2011-03-17
    • 2015-09-12
    • 1970-01-01
    • 2014-03-26
    • 2015-03-06
    • 2018-04-18
    • 2018-11-13
    相关资源
    最近更新 更多