【问题标题】:Sorting by key and value按键和值排序
【发布时间】:2014-10-25 12:15:37
【问题描述】:

我实现了快速排序,我想先按键排序,然后按值排序。我有重复的键和值,所以我需要它们主要按键排序,次要按值排序。例如以下是正确排序的:

cat 533
dog 251
dog 533
dog 1021
ferret 31
ferret 477
zebra 398

我正在从通过命令行指定的文件中读取数据。我考虑过使用哈希表,但我读到无法对哈希表进行排序。所以我的问题是如何使用我的快速排序实现先按键排序,然后按值排序?现在我只从文件中读取文本而不是整数。到目前为止,这是我的代码:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;

public class Quicksort
{
    //  static Hashtable<String, Integer> hash;
    static ArrayList<String> records;

    public static void main(String[] args) throws IOException
    {
        //  hash = new Hashtable<String, Integer>();
        records = new ArrayList<String>();
        if (0 < args.length)
        {
            readFile(args[0]);
        }
        else
        {
            System.err.println("Invalid arguments count:" + args.length);
            System.exit(0);
        }
        ArrayList<String> s = new ArrayList<String>();

        quickSort(records, 0, records.size() - 1);
        for (String k : records)
            System.out.println(k);

    }

    public static void readFile(String inFile) throws IOException
    {
        BufferedReader br = new BufferedReader(new FileReader(inFile));
        try
        {
            String line = br.readLine();

            while (line != null)
            {
                //System.out.println(line);
                String[] split = line.split("\\s+");
                records.add(split[0]);
                //  hash.put(split[0], Integer.parseInt(split[1]));

                line = br.readLine();
            }
        }
        finally
        {

            br.close();
        }
    }

    public static <T> void display(T[] a)
    {
        for (T b : a)
            System.out.println(b);
    }

    public static <T extends Comparable> void quickSort(ArrayList<T> a, int wall, int pivotIndex)
    {
        if (wall < pivotIndex)
        {
            int index = partition(a, wall, pivotIndex);
            quickSort(a, wall, index - 1);
            quickSort(a, index + 1, pivotIndex);
        }
    }

    public static <T extends Comparable<T>> int partition(ArrayList<T> a, int wall, int pivotIndex)
    {
        T currentPivot = a.get(pivotIndex);
        int leftOfWall = wall - 1;

        for (int j = wall; j <= pivotIndex - 1; j++)
        {
            if (a.get(j).compareTo(currentPivot) <= 0)
                exchange(a, ++leftOfWall, j);

        }

        exchange(a, leftOfWall + 1, pivotIndex);
        return leftOfWall + 1;
    }

    public static <T> void exchange(ArrayList<T> a, int b, int c)
    {
        T swap = a.get(b);
        a.set(b, a.get(c));
        a.set(c, swap);

    }
}

【问题讨论】:

    标签: java sorting quicksort key-value


    【解决方案1】:

    其中一个选项是创建一个新类:

    public class Animal implements Comparabe<Animal> {
        private String type;
        private int number;
        public Animal(String type, int number) {
            this.type = type;
            this.number = number;
        }
    
        public int compareTo(Animal other) {
           //sorting logic here, first sort strings if they equals sort by integers.
        }
    }
    

    然后只列出动物static ArrayList&lt;Animal&gt; records;

    【讨论】:

      【解决方案2】:

      声明一个实现 Comparable 的记录类:

      class Record implements Comparable<Record>
      {
      
      String key;
      int value;
      
      int compareTo(Record o1, Record o2)
      {
      int val = o1.key.compareTo(o2.key);
      if (val != 0)
      return val;
      
      return o1.value.compareTo(o2.value);
      
      }
      

      现在将记录读入此类的实例并运行排序

      【讨论】:

        猜你喜欢
        • 2023-03-09
        • 2020-12-23
        • 2023-03-20
        • 1970-01-01
        • 2019-04-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-10
        相关资源
        最近更新 更多