下面的程序使用归并排序来排列文件中的前 10,000 个单词。我遵循了 Thomas Cormen 在他的《算法导论》第二版中的伪代码。
import java.io.*;
import java.util.*;
public class SortingAnalysis {
public static void merge(String[] A, int p, int q, int r) {
int n1 = q-p+1;
int n2 = r-q;
double infinity = Double.POSITIVE_INFINITY;
int i, j;
String[] L = null;
String[] R = null;
for (i=1; i<=n1; i++) {
L[i] = A[(int) (p+i-1)];
}
for (j=1; j<=n2; j++) {
R[j] = A[(int) (q+j)];
}
L[n1+1] = infinity; //type mismatch: cant convert from double to string
R[n2+1] = infinity; //same as above
i=1;
j=1;
for (int k=(int) p; k<=r; k++) {
int comparison = L[i].compareTo(R[j]);
if (comparison<=0) {
A[k] = L[i];
i++;
}
else {
A[k] = R[j];
j++;
}
}
}
public static void mergeSort(String[] A, int p, int r) {
if (p<r) {
int q = (int) Math.floor((p+r)/2); //I typecasted q here so I can still pass the variables
mergeSort(A, p, q);
mergeSort(A, q+1, r);
merge(A, p, q, r);
}
}
public static void main(String[] args) {
final int NO_OF_WORDS = 10000;
try {
Scanner file = new Scanner(new File(args[0]));
String[] words = new String[NO_OF_WORDS];
int i = 0;
while(file.hasNext() && i < NO_OF_WORDS) {
words[i] = file.next();
i++;
}
long start = System.currentTimeMillis();
mergeSort(words, 0, words.length-1);
long end = System.currentTimeMillis();
System.out.println("Sorted Words: ");
for(int j = 0; j < words.length; j++) {
System.out.println(words[j]);
}
System.out.print("Running time of insertion sort: " + (end - start) + "ms");
}
catch(SecurityException securityException) {
System.err.println("Error");
System.exit(1);
}
catch(FileNotFoundException fileNotFoundException) {
System.err.println("Error");
System.exit(1);
}
}
}
控制台中显示错误提示
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Type mismatch: cannot convert from double to String
Type mismatch: cannot convert from double to String
at SortingAnalysis.merge ... mergeSort and main </code>
我认为这是因为 Math.floor 方法应该是双精度的,但我确实将它类型转换为 int,因此在传递参数时不会有问题。
另外,我认为将字符串分配给无穷大时存在错误。但我只是在遵循 Cormen 的伪代码。这似乎是正确的,因为我自己手动“调试”了代码。但是,当我将其放入代码中时,它不起作用。我哪里错了?我需要你的帮助,伙计们。我是 Java 新手,我仍在缓冲过程中。非常感谢!