バケットソートを使用して、常に同じ長さの文字列のリストを並べ替えるのに最適な方法がわかりません。
アルゴリズムは次のようになります。
For the last character position down to the first:
For each word in the list:
Place the word into the appropriate bucket by current character
For each of the 26 buckets(arraylists)
Copy every word back to the list
私はJavaで記述しており、ソートされていない文字列を格納するメインリストにarraylistを使用しています。文字列はそれぞれ5文字の長さになります。
これが私が始めたものです。次に何をすべきかわからないため、または最初の部分を正しく実行したかどうかがわからないため、2番目のforループ内で突然停止します。
ArrayList<String> count = new ArrayList<String>(26);
for (int i = wordlen; i > 0; i--) {
for (int j = 0; i < myList.size(); i++)
myList.get(j).charAt(i)
}
前もって感謝します。
編集:これは私が今持っているものです。同じ文字で始まる文字列が爆発するよりも複数ある場合は機能しないことはわかっていますが、私はもっと正しい方向に進んでいると思います。私がそれを実行すると、重複する文字がないことを確認するためにそれを入れた単語でさえ、それは最初の設定行でびっくりします:count.set(myList.get(j).charAt(i), myList.get(j));
それは「スレッド「メイン」の例外」と言いますjava.lang.StringIndexOutOfBoundsException:文字列インデックスが範囲:5 "
public void BucketSort(int wordlen) {
ArrayList<String> count = new ArrayList<String>(26);
//Make it so count has a size
for(int p = 0; p < 26; p++)
count.add(null);
for (int i = wordlen; i > 0; i--) { //for each letter
for (int j = 0; j < myList.size(); j++) //for each word
//Add the word to count based on the letter
count.add((int)myList.get(j).charAt(i) - 65, myList.get(j));
}
//Clear the main list so there aren't a bunch of unsorted words leftover
myList.clear();
//Add the words back in to the list based on their order in count
for (int m = 0; m < 26; m++)
myList.add(count.get(m));
}