【问题标题】:Sorting through file using QuickSort in Java在 Java 中使用 QuickSort 对文件进行排序
【发布时间】:2016-11-19 17:01:37
【问题描述】:

目前我已经遍历了该文件,但我现在正在尝试使用 quickSort 对输出进行排序。我创建了一个可以与本地数组一起使用的快速排序/分区类,但我想知道如何将它与其他类中的文件一起使用。我想做一些事情,比如按人口排序、按城市字母排序和按纬度排序。

这是我正在处理的一些文件:

ad,Andorra La Vella,07,20430,42.5,1.5166667
ad,Canillo,02,3292,42.5666667,1.6
ad,Encamp,03,11224,42.5333333,1.5833333
ad,La Massana,04,7211,42.55,1.5166667
ad,Les Escaldes,08,15854,42.5,1.5333333
ad,Ordino,05,2553,42.55,1.5333333
ad,Sant Julia De Loria,06,8020,42.4666667,1.5
ae,Abu Dhabi,01,603687,24.4666667,54.3666667
ae,Dubai,03,1137376,25.2522222,55.28

我的代码:

public class City {
    String countrycode;
    String city;
    String region;
    int population;
    double latitude;
    double longitude;

    public City(String countrycode, String city, String region, int population, double latitude, double longitude) {
        this.countrycode = countrycode;
        this.city = city;
        this.region = region;
        this.population = population;
        this.latitude = latitude;
        this.longitude = longitude;
    }

    public String toString() {
        return this.city + "," + this.population + "," + this.latitude + "," + this.longitude;
    }
}

public class Reader {

    In input = new In("file:world_cities.txt");
    public static City cityInfo;

    public static void main(String[] args) {
        // open file
        In input = new In("world_cities.txt");

        try {
            // write output to file
            FileWriter fw = new FileWriter("cities_out.txt");
            PrintWriter pw = new PrintWriter(fw);

            int line = 0;

            // iterate through all lines in the file
            while (line < 47913) {

                // read line
                String cityLine = input.readLine();

                // create array list
                ArrayList<String> cityList = new ArrayList<String>(Arrays.asList(cityLine.split(",")));

                // increase counter
                line += 1;

                // create variables for the object
                String countrycode = cityList.get(0);
                String city = cityList.get(1);
                String region = cityList.get(2);
                int population = Integer.parseInt(cityList.get(3));
                double latitude = Double.parseDouble(cityList.get(4));
                double longitude = Double.parseDouble(cityList.get(5));
                // create instance
                cityInfo = new City(countrycode, city, region, population, latitude, longitude);
                System.out.println(cityInfo);

                // print output to file
                pw.println(cityInfo);
            }

            // close the file
            pw.close();
        }

        // what is printed when there is an error when saving to file
        catch (Exception e) {
            System.out.println("ERROR!");
        }

        // close the file
        input.close();
    }
}

public class QuickSort3 {
    public static void main(String[]args) {
        int[] array = {4, 77, 98, 30, 20, 50, 77, 22, 49, 2}; // local array that works with the quicksort
        quickSort(array,0,array.length - 1);
        System.out.println(Arrays.toString(array));
    }

    public static void quickSort(int[] a, int p, int r)
    {
        if(p<r)
        {
            int q = Partition(a, p,r);
            quickSort(a, p, q-1);
            quickSort(a, q+1, r);
        }
    }

    private static int Partition(int[] a, int p, int r)
    {
        int x = a[r];

        int i = p-1;
        int temp=0;

        for(int j=p; j<r; j++)
        {
            if(a[j]<=x)
            {
                i++;
                temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }
        }
        temp = a[i+1];
        a[i+1] = a[r];
        a[r] = temp;
        return (i+1);
    }
}

【问题讨论】:

  • 你的排序逻辑是什么?国家、城市、地区?
  • 如果你给你的快速排序方法一个额外的ComparatorBiPredicate类型的参数,它可以用来按照你想要的任何顺序对任意类型的数组进行排序。

标签: java arrays file arraylist quicksort


【解决方案1】:

所以如果我理解正确,您有一个可以对ints 进行排序的快速排序方法,现在您的要求是对City 对象进行排序。我想你可以在互联网上找到很多灵感。首先,您需要指定排序顺序。由于您需要不同的排序顺序(按人口、字母顺序等),请写一个 Comparator&lt;City&gt; 来做到这一点。在您的教科书和/或网络上搜索指导和示例。你的比较器需要你的City 类来获得方法,所以也添加一些方法。例如,按人口(最大优先)排序的比较器可能是

Comparator<City> populationComparator = Comparator.comparingInt(City::getPopulation).reversed();

这将要求City 有一个getPopulation 方法。

接下来,您需要重写 quicksort 方法以接受 City 对象和比较器的数组,Partition() 也是如此。在Partition 方法中,它显示a[j]&lt;=x,替换为cityComparator.compare(a[j], x) &lt;= 0(如果您的比较器参数称为cityComparator)。您需要将x 和其他包含元素的变量的类型更改为City

【讨论】:

    猜你喜欢
    • 2020-05-17
    • 2012-11-02
    • 1970-01-01
    • 1970-01-01
    • 2013-03-16
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多