在Java中使用快速排序对文件进行排序
目前我已经遍历了该文件,但我现在正尝试使用快速排序对输出进行排序。我已经创建了一个可用于本地数组的快速排序/分区类,但是我想知道如何将它用于另一个类中的文件。我想做一些事情,比如按人口排序、按城市字母顺序排序和按纬度排序 以下是我正在处理的一些文件:在Java中使用快速排序对文件进行排序,java,arrays,file,arraylist,quicksort,Java,Arrays,File,Arraylist,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
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);
}
}
公共级城市{
字符串国家代码;
字符串城市;
弦区;
国际人口;
双纬度;
双经度;
公共城市(字符串国家代码、字符串城市、字符串地区、整数人口、双纬度、双经度){
this.countrycode=countrycode;
this.city=城市;
这个区域=区域;
这个。人口=人口;
这个。纬度=纬度;
这个经度=经度;
}
公共字符串toString(){
返回this.city+”、“+this.population+”、“+this.latitude+”、“+this.longitude”;
}
}
公共类阅读器{
输入=新输入(“文件:world_cities.txt”);
公共静态城市信息;
公共静态void main(字符串[]args){
//打开文件
输入=新输入(“world_cities.txt”);
试一试{
//将输出写入文件
FileWriter fw=新的FileWriter(“cities_out.txt”);
PrintWriter pw=新的PrintWriter(fw);
内线=0;
//迭代文件中的所有行
而(线<47913){
//读线
字符串cityLine=input.readLine();
//创建数组列表
ArrayList cityList=新的ArrayList(Arrays.asList(cityLine.split(“,”));
//递增计数器
行+=1;
//为对象创建变量
字符串countrycode=cityList.get(0);
字符串city=cityList.get(1);
String region=cityList.get(2);
int population=Integer.parseInt(cityList.get(3));
双纬度=double.parseDouble(cityList.get(4));
double longitude=double.parseDouble(cityList.get(5));
//创建实例
cityInfo=新城(国家代码、城市、地区、人口、纬度、经度);
系统输出打印LN(城市信息);
//将输出打印到文件
印地安号(城市信息);
}
//关闭文件
关闭();
}
//保存到文件时出错时打印什么
捕获(例外e){
System.out.println(“错误!”);
}
//关闭文件
input.close();
}
}
公共类快速入门3{
公共静态void main(字符串[]args){
int[]数组={4,77,98,30,20,50,77,22,49,2};//使用快速排序的本地数组
快速排序(数组,0,array.length-1);
System.out.println(array.toString(array));
}
公共静态void快速排序(int[]a,int-p,int-r)
{
如果(p所以如果我理解正确,你有一种快速排序方法,可以对int
s进行排序,现在你的要求是对City
对象进行排序。我想你可以在互联网上找到很多灵感。首先你需要指定排序顺序。因为你需要不同的排序顺序(按人口、字母顺序等),编写一个Comparator
。在教科书和/或网上搜索指南和示例。你的Comparator需要你的City
类具有get方法,因此也可以添加一些方法。例如,按人口排序的Comparator(最大优先)可能是
Comparator populationComparator=Comparator.comparingInt(City::getPopulation).reversed();
这将要求City
具有getPopulation
方法
接下来,您需要重写快速排序
方法,以接受城市
对象数组和比较器,对于分区()
也是如此因此,如果我理解正确,您有一种快速排序方法,可以对int
s进行排序,现在您的要求是对City
对象进行排序。我认为您可以在互联网上找到很多灵感。首先,您需要指定排序顺序。因为您需要不同的排序顺序(按人口、字母顺序等),编写一个Comparator
。在教科书和/或网上搜索指南和示例。你的Comparator需要你的City
类具有get方法,因此也可以添加一些方法。例如,按人口排序的Comparator(最大优先)可能是
Comparator populationComparator=Comparator.comparingInt(City::getPopulation).reversed();
这将要求City
具有getPopulation
方法
接下来,您需要重写快速排序
方法,以接受城市
对象数组和比较器,对于分区()
也是如此您的排序逻辑是什么?国家、城市、地区?如果您给快速排序方法一个类型为Comparator
或BiPredicate
的附加参数,它可以用于按任意顺序对任意类型的数组进行排序。您的排序逻辑是什么?国家、城市、地区?如果您给快速排序方法一个附加参数类型为比较器
或BiPredicate
的数组,可用于按任意顺序对任意类型的数组进行排序。
Comparator<City> populationComparator = Comparator.comparingInt(City::getPopulation).reversed();