简体   繁体   English

效率不高的存储桶排序?

[英]Inefficient bucket sort?

I'm trying to use the bucket sort algorithm to sort strings. 我正在尝试使用存储桶排序算法对字符串进行排序。 The task says the runtime should be about 0.05 seconds, but mine takes over 9. Why is mine taking so long, and how can I make it faster. 任务说运行时间应该是0.05秒左右,但是我的要花9秒钟。为什么我的要花这么长时间,以及我如何才能使其更快呢。 It has about 90000 names in the file. 文件中大约有90000个名称。 Am I even doing the bucket sort properly? 我什至在正确地分类存储桶吗?

public static void bucketSortByLength() {
      String[] bucket = new String[14];
      String[] insideBucket;
      int index = 0;
      for(int i = 0; i <= 13; i++)
        bucket[i] = "";
      for(int i = 0; i < numNames; i++)
        bucket[names[i].length()] += names[i] + " ";
      for(int i = 0; i <= 13; i++){
        insideBucket = bucket[i].split("\\s+");
        for(String s : insideBucket)
          names[index++] = s;
      }
    }

Your code is likely slow because it's doing something that can be really inefficient in Java: String values are immutable, so concatenation creates a whole new string each time. 您的代码可能很慢,因为它执行的操作实际上在Java中效率很低: String值是不可变的,因此串联每次都会创建一个全新的字符串。

You should use a data type that supports efficient appends for your buckets. 您应该使用支持存储桶高效附加的数据类型。 Two options that come to mind are StringBuilder and LinkedList<String> . 我想到的两个选项是StringBuilderLinkedList<String> (The latter would have been better when I last used Java everyday, and probably still is.) (当我上次每天使用Java时,后者会更好,现在可能仍然如此。)

As others have stated, this is inefficient because of the string concatenation. 正如其他人所述,由于字符串连接,这效率很低。

A good rule of thumb: 一个好的经验法则:
Whenever you find your self concatenating strings inside a loop, it's best to use a StringBuilder . 每当您在循环中找到自连接字符串时,最好使用StringBuilder
See: When to use StringBuilder in Java 请参阅: 何时在Java中使用StringBuilder

This code should give you the performance you are expecting: 此代码应为您提供预期的性能:

public static void bucketSortByLength() {
    StringBuilder[] bucket = new StringBuilder[14];
    int index = 0;
    for(int i = 0; i <= 13; i++)
        bucket[i] = new StringBuilder();
    for(int i = 0; i < numNames; i++)
        bucket[names[i].length()].append(names[i]).append(' ');

    for(int i = 0; i <= 13; i++){
        String[] insideBucket = bucket[i].toString().split("\\s+");
        for(String s : insideBucket)
            names[index++] = s;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM