title: Java Bucket Sort on Strings
body: <p>I can't figure out what would be the best way to use Bucket Sort to sort a list of strings that will always be the same length.</p>
  
  <p>An algorithm would look like this:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I'm writing in java and I'm using an arraylist for the main list that stores the unsorted strings. The strings will be five characters long each.</p>
  
  <p>This is what I started. It just abrubdly stops within the second for loop because I don't know  what to do next or if I did the first part right.</p>
  
  <pre><code>ArrayList&lt;String&gt; count = new ArrayList&lt;String&gt;(26);
  
  for (int i = wordlen; i &gt; 0; i--) {
      for (int j = 0; i &lt; myList.size(); i++)
          myList.get(j).charAt(i)
  }
  </code></pre>
  
  <p>Thanks in advance.</p>
  
  <p>EDIT: This is what I have now. I know it doesn't work because if there were more than one strings that started with the same letter than it would blow up, but I think I'm more in the right direction. When I run it, even with words that I put it in to make sure there are no duplicates letters, it freaks out on the first set line: <code>count.set(myList.get(j).charAt(i), myList.get(j));</code> It's says "Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5"</p>
  
  <pre><code> public void BucketSort(int wordlen) {
  
     ArrayList&lt;String&gt; count = new ArrayList&lt;String&gt;(26);
  
       //Make it so count has a size
     for(int p = 0; p &lt; 26; p++)
         count.add(null);
  
      for (int i = wordlen; i &gt; 0; i--) { //for each letter
          for (int j = 0; j &lt; 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 &lt; 26; m++)
         myList.add(count.get(m));
    }
  </code></pre>