2

バケットソートを使用して、常に同じ長さの文字列のリストを並べ替えるのに最適な方法がわかりません。

アルゴリズムは次のようになります。

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));
  }
4

4 に答える 4

3

これは私には宿題のように見えるので、コードソリューションでは応答しません。

しかし、基本的に、あなたが立ち往生しているビットはあなたのバケツを設定することです。おそらく、バケットをMap<Character, List<String>>-にする必要があります。つまり、各文字A-Zを、その文字に一致する単語のリスト(現在見ている位置)にマップする必要があります。その単語のリストがあなたのバケツです。

次に、そこに到達した内部ループを終了した後、マップのコンテンツを介して別のループを実行し、AZ(ヒント:)から移動しfor ( char ch = 'A'; ch <= 'Z'; ch++ )て、対応するバケットのコンテンツを(空の)リストにダンプします。

于 2010-03-31T01:24:34.450 に答える
1

小さなリストでこれをテストしました。はい、私も宿題のためにこれをしなければなりませんでした、そしてあなたの便宜のためにコメントを残しました。

public static void sort(String[] array) {
        if (array.length == 0) return;  // empty check

        // Determine max length
        int max = 0;
        for (int i = 1; i < array.length; i++) {
            // update max length
            if (max < array[i].length()) max = array[i].length();
        }


        // Initialize buckets
        int bucketCount = 26;   // alphabet
        // buckets in maps
        HashMap<Character, Vector<String>> buckets = new HashMap<Character, Vector<String>>(bucketCount);
        // create the buckets
        char a = 'a';
        for (int i = 0; i <= bucketCount; i++, a++){
            buckets.put(a, new Vector<String>());
        }   

        // assign array values into buckets
        for (int i = 0; i < array.length; i++) {
            // get first letter of word
            String current = array[i];
            char letter = current.toLowerCase().charAt(0);
            buckets.get(letter).add(array[i]);
        }

        // Sort buckets and place back into input array
        int index = 0;  // keeps global array index
        for (char key = 'a'; key <= 'z'; key++) {
            // retrieve the bucker
            Vector<String> bucket = buckets.get(key);

            // do an insertion sort on bucket
            for (int i = 1; i < bucket.size(); i++){
                // i starts as 1, as a list of size 1 is already sorted

                // save the value at the index and remove it
                String temp = bucket.get(i);
                bucket.remove(i);

                // move all values one up, until we find the saved value's location
                int j;
                for(j = i-1; j >= 0 && 
                        bucket.get(j).compareToIgnoreCase(temp) > 0; j--){
                    // to "insert", we need to add and remove
                    bucket.add(j+1, bucket.get(j));
                    bucket.remove(j);
                }
                // place the saved value in the proper location
                bucket.add(j+1, temp);
            }


            // pile the current bucket back into array
            for (int j = 0; j < bucket.size(); j++) {
                array[index++] = bucket.get(j);
            }
        }
    }
于 2016-04-26T23:37:43.363 に答える
0

を使用しない場合は、 @ JacobMMapで説明されているのと同じロジックを使用できますが、代わりにListの配列を使用できます。したがって、を作成します。List<String>[] buckets = new List<String>[26]

于 2010-03-31T02:35:25.543 に答える
0
public void bucketSort(String[] words)
{

    int maxlength=0;
    for(int i=0;i<words.length;i++)
    {
        words[i] = words[i].toUpperCase();
        if(maxlength<words[i].length())
            maxlength = words[i].length();

    }

    for(int j=maxlength-1;j>=0;j--)
    {

        Vector<Vector<String>> map = new Vector<Vector<String>>();
        for(int i=0;i<27;i++)
        {
            map.add(null);
        }
        for(int i=0;i<words.length;i++)//Add words of of length j or greater to map(which is bucket here)
        {

            if(words[i].length()>j)
            {
                int val = (int)words[i].charAt(j) -65;
                if(map.get(val)!= null)
                {
                    map.get(val).add(words[i]);
                }
                else
                {
                    Vector<String> vecot = new Vector<String>();
                    vecot.add(words[i]);
                    map.add(val, vecot);
                }
            }
            else///Add words of of length<j to bucket 0
            {
                if(map.get(0) != null)
                {
                    map.get(0).add(words[i]);
                }
                else
                {
                    Vector<String> vecot = new Vector<String>();
                    vecot.add(words[i]);
                    map.add(0, vecot);
                }

            }
        }
        int count =0;
        for(int i=0;i<map.size();i++)
        {

            if(map.get(i)!=null)
            {
                for(int k=0;k<map.get(i).size();k++)
                {
                 words[count]=map.get(i).get(k); 
                 count++;
                }
            }
        }
        System.out.println("Next set :");
        for(int i=0;i<words.length;i++)
        {
            System.out.println(words[i]);
        }

    }




}
于 2014-04-22T17:50:06.970 に答える