从输入文件中对数组进行 Shellsorting

Shellsorting an array from an input file

完整的项目是从一个文件中获取数据,该文件是一个文本文件,其中包含所有 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
 ....

除非教授。专门说用一个字符串数组来表示国家和一个整数数组来表示利率,@ScaryWombat 的对象数组的想法每个包含一个字符串和一个整数是可行的方法。

也就是说,如果必须的话,您可以仍然使用单独的数组来完成。当您的排序算法需要交换时,请务必交换 line population 条目,而不是只有 population 条目。

解析文件时,需要将解析后的值存储在字典或其他结构中。排序后,打印时,从字典中读取值。

我修改了您的代码以将值存储在字典中,并在我 added/modified 的行中添加了注释。我没有触及你的排序算法,所以你仍然在同一个数组上排序:

public static void main(String[] args) throws IOException {
    String populationString = "";
    String[] line = new String[201];
    int populations[] = new int[201];

    // Have a dictionary that can store the values you parse
    Map<Integer, String> dictionary = new HashMap<Integer, String>();

    Scanner fileIN = new Scanner(new File("F:/CountrySortedAlpha.txt"));

    while (fileIN.hasNext()) {
        for (int i = 0; i < 201; i++) {
            // Parse the whole line, this 29 hard coded seems incorrect
            populationString = fileIN.nextLine().substring(0, 29);
            // Grab both values
            String[] splited = populationString.split("\s+");
            // Country name can have spaces, so take the last elemnt
            populations[i] = Integer.parseInt(splited[splited.length - 1]);
            // Join back values
            String country = populationString.join(" ", splited);
            // Cut off the rate number
            country = country.substring(0, country.lastIndexOf(" "));
            // Store them in your dictionary
            if (dictionary.containsKey(populations[i])) {
                // If multiple countries have same rate, add them to value, and separate with comma
                String value = dictionary.get(populations[i]);
                dictionary.put(populations[i], value + "," + country);
            } else {
                dictionary.put(populations[i], country);
            }
        }
        int j;
        for (int gap = populations.length / 2; gap > 0; gap /= 2) {
            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("Final sorted order: ");
        for (int k = 0; k < populations.length; k++) {
            // Read the value from dictionary
            String value = dictionary.get(populations[k]);
            // For duplicates skip, that entry gets deleted after values were printed
            if (value == null) {
                continue;
            }

            // If multiple countries had the same rate, they were stored as comma separated value
            String[] countries = value.split(",");
            for (String country : countries) {
                // You can print rate, or country, or both
                System.out.println(populations[k] + " " + country);
            }

            // Remove from dictionary, because we already printed all countries with the same rate
            dictionary.remove(populations[k]);
        }
        System.out.println();
    }

    // Don't forget to close the file
    fileIN.close();
}