基于字符串的Java桶排序
我不知道使用Bucket Sort对长度始终相同的字符串列表进行排序的最佳方法是什么 算法如下所示:基于字符串的Java桶排序,java,sorting,arraylist,bucket,Java,Sorting,Arraylist,Bucket,我不知道使用Bucket Sort对长度始终相同的字符串列表进行排序的最佳方法是什么 算法如下所示: 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)
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作为存储未排序字符串的主列表。每个字符串的长度为五个字符
这是我开始的。它只是在第二个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)
}
这看起来像是我的家庭作业,所以我不会用代码解决方案来回应 但基本上,你被卡住的地方是设置你的桶。可能您希望bucket是一个
映射图
——也就是说,您希望将每个字母a-Z映射到与该字母匹配的单词列表(针对您当前查看的位置)。那张单子就是你的桶
然后,在完成内部循环之后,从A-Z开始对映射的内容执行另一个循环(提示:
for(char ch='A';ch如果不打算使用map
,则可以使用与所述相同的逻辑,但可以使用一个列表数组。因此,创建List[]bucket=new List[26]
公共无效bucketSort(字符串[]个单词)
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]);
}
}
}
{
int maxlength=0;
对于(int i=0;我在一个小列表中测试了这个;是的,我也必须这样做是为了家庭作业,为了方便你留下评论,这样你就可以理解发生了什么,而不仅仅是复制粘贴它(如果你这样做,你的OV分数将是100%)
公共静态无效排序(字符串[]数组){
if(array.length==0)返回;//空检查
//确定最大长度
int max=0;
for(int i=1;i 对于(int i=0;我是的,我忘了说这是家庭作业,我不需要代码。但这是额外的学分…我们还没有学到任何关于Map的知识,所以肯定有另一种方法可以用ArrayList来实现这一点,但我想这是可行的。我真的不明白Map是如何工作的。我在看Javadoc,但我不太理解它。Map就是A。)名称和值对的et,其中“名称”和“值”可以是任何类型的对象。它们非常有用;假设你想为一组玩家中的每一个保留分数,但你事先不知道玩家是谁或有多少;你可以有一个将玩家链接到整数(该玩家的分数)的地图。但是,当然,您可以使用列表列表来实现这一点;您只需要按照惯例,第一个位置的任何内容都代表“a”,第二个位置代表“B”,以此类推。您无法根据Oracle文档创建参数化类型数组:
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]);
}
}
}
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);
}
}
}