完整的项目是从一个文件中获取数据,该文件是一个文本文件,其中包含所有 201 个国家/地区的列表以及它们按字母顺序排列的各自互联网使用率。这是一个例子
Afghanistan 7
Albania 63
Algeria 20
Andorra 97
Angola 23
...
有了这个,我们必须以数字方式(特别是)对数据进行 Shellsort。我已经成功地做到了这一点,但我只输出了一个百分比列表,因为我还需要列出的国家。这是我的代码:
import java.io.*;
import java.util.*;
public class InternetUsers {
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
String populationString = "";
String[] line = new String[201];
int populations[] = new int[201];
Scanner fileIN = new Scanner(new File("F:/CountrySortedAlpha.txt"));
while(fileIN.hasNext()){
for(int i = 0; i < 201; i++){
populationString = fileIN.nextLine().substring(26, 29);
populations[i] = Integer.parseInt(populationString.trim());
}
int j;
for(int gap = populations.length / 2; gap > 0; gap /= 2){
for (int k = 0; k < populations.length; k++){
}
for (int t = gap; t < populations.length; t++){
int tmp = populations[t];
for(j = t; j >= gap && (tmp < populations[j - gap]); j -= gap){
populations[j] = populations[j - gap];
}
populations[j] = tmp;
}
}
System.out.println("\nFinal sorted order: ");
for(int k = 0; k < populations.length; k++){
System.out.print(populations[k]);
System.out.println("");
}
System.out.println();
}
}
}
所以我的问题是我该如何输出这些国家呢?我需要完全重做我的排序方式吗?这是我的示例输出:
Final sorted order:
1
1
2
2
2
2
2
3
....