I am writing a program that counts the letter frequency of a file to eventually use to help decode a different file. I am able to correctly sort the array by values (the method call i commented out) but am unable to figure out how to sort the corresponding letters with the values. Any help would be greatly appreciated.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
class CodeBreakerProject {
public static void swap(int[] count, int index1, int index2) {
int temp = count[index1];
count[index1] = count[index2];
count[index2] = temp;
}
public static void selectionSort(int[] count) {
for (int i = 0; i < count.length; i++) {
// find smallest element from i to end
int minIndex = i; // assume 1st element is smallest
for (int j = i; j < count.length; j++) {
if (count[j] > count[minIndex]) {
minIndex = j;
}
}
swap(count, i, minIndex);
}
}
public static void main(String[] args) throws IOException {
File file1 = new File("training.txt");
BufferedReader in = new BufferedReader(new FileReader(file1));
System.out.println("Letter Frequency");
int nextChar;
char ch;
int[] count = new int[26];
while ((nextChar = in.read()) != -1) {
ch = ((char) nextChar);
if (ch >= 'a' && ch <= 'z') { count[ch - 'a']++; }
}
//selectionSort(count); this sorts the values but does not move the letter assignments
for (int i = 0; i < 26; i++) {
System.out.printf("%c = %d\n", i + 'A', count[i]);
}
in.close();
}
}