2016-11-19 9 views
1

現在、私はファイルを反復処理しましたが、現在はquickSortを使用して出力をソートしようとしています。ローカルアレイで動作するクイックソート/パーティションクラスを作成しましたが、他のクラスのファイルと一緒に使用する方法は不思議です。人口別の並べ替え、アルファベット順の都市別並べ替え、緯度別並べ替えなどを行いたいJavaで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); 
    } 
} 
+0

あなたのソートロジックは何ですか?国、都市、地域? –

+0

quicksortメソッドに 'Comparator'や' BiPredicate'型の引数を追加すると、任意のタイプの配列を任意の順序で並べ替えることができます。 – SpiderPig

答えて

0

私が正しく理解していれば、あなたはint Sを並べ替えることができますクイックソート方法を持っているので、ここで

は、私が働いているファイルの一部がありますあなたの要件はCityのオブジェクトをソートすることです。私はあなたがインターネット上で多くのインスピレーションを見つけることができると思います。まずソート順を指定する必要があります。異なるソート順(人口、アルファベット順など)が必要なので、それを行うComparator<City>と書いてください。指導書や例については、あなたの教科書やネットを検索してください。あなたのコンパレータはメソッドを取得するためにあなたのCityクラスを必要とするので、それらのメソッドもいくつか追加してください。例えば、人口によってソートするコンパレータは、(最大の最初の)これはCitygetPopulationメソッドを持っている必要があります

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

かもしれません。

quicksortメソッドを書き換えて、Cityオブジェクトとコンパレータの配列を受け入れ、Partition()の配列を受け入れる必要があります。 Partitionメソッド内で、a[j]<=xと表示されている場合は、cityComparator.compare(a[j], x) <= 0(コンパレータパラメータがcityComparatorの場合)で置き換えます。 xのタイプと要素を保持するその他の変数をCityに変更する必要があります。

関連する問題