Earendil
8th October 2006, 05:39 PM
I'm working on a project for my class (school class), and I'm on the absolute final method I need to code, and I don't know how to do it.
Here's the situation: I've read in a Bush speech, and counted the number of times each word occurs, and sorted those.
As of now, I have two arrays, one of Strings and one of ints, consisting of all the words, and the number of times they occured. They're sorted from occurence max to min.
So, it outputs the following.
the: 216
and: 165
.
.
.
Casablanca: 1
around: 1
Zarqawi: 1
My problem is that I need to organize all words whose occurence are the same alphabetically.
So all words who occur once need to be in alphabetical order (as you can see they're not).
I just need to figure out how to figure out which words need to be sorted, and sort those. I don't know either how to figure out which words have the same count, and then how to sort those. The code below was the best I could come up with, and it failed. I tried to figure out which indices for count were equal, then sorting all the elements between those two indices for the words array.
Any help would be super, either corrections to my code, or suggesting a new algorithm.
public static String[] countOrgan(String[] words, int [] count) {
int high, low, min;
String temp;
for(int i=1;i<count.length-1;i++) {
if (count[i]==count[i-1]&&count[i]!=count[i+1]){
low = i;
do {
if(count[i]==count[i-1])
i++;
else break;
} while(true);
high = i-1;
i--;
}
else continue;
for(int j=low; j<high-1;j++) {
min = j;
for(int k=j+1;k<high;k++) {
if (words[k].compareTo(words[min])<0)
min = k;
}
temp = words[j];
words[j] = words[min];
words[min] = temp;
}
}
return words;
}
Here's the situation: I've read in a Bush speech, and counted the number of times each word occurs, and sorted those.
As of now, I have two arrays, one of Strings and one of ints, consisting of all the words, and the number of times they occured. They're sorted from occurence max to min.
So, it outputs the following.
the: 216
and: 165
.
.
.
Casablanca: 1
around: 1
Zarqawi: 1
My problem is that I need to organize all words whose occurence are the same alphabetically.
So all words who occur once need to be in alphabetical order (as you can see they're not).
I just need to figure out how to figure out which words need to be sorted, and sort those. I don't know either how to figure out which words have the same count, and then how to sort those. The code below was the best I could come up with, and it failed. I tried to figure out which indices for count were equal, then sorting all the elements between those two indices for the words array.
Any help would be super, either corrections to my code, or suggesting a new algorithm.
public static String[] countOrgan(String[] words, int [] count) {
int high, low, min;
String temp;
for(int i=1;i<count.length-1;i++) {
if (count[i]==count[i-1]&&count[i]!=count[i+1]){
low = i;
do {
if(count[i]==count[i-1])
i++;
else break;
} while(true);
high = i-1;
i--;
}
else continue;
for(int j=low; j<high-1;j++) {
min = j;
for(int k=j+1;k<high;k++) {
if (words[k].compareTo(words[min])<0)
min = k;
}
temp = words[j];
words[j] = words[min];
words[min] = temp;
}
}
return words;
}