java - 通过希尔排序对字符串行进行排序

标签 java sorting

我有一个包含 50 行不同长度和内容的字符串的文本文件。我需要读取文件并按升序排序。排序条件:句子中以字母“a”开头的单词数。

public static void main(String[] args) throws FileNotFoundException {
    String token1 = "";
    Scanner inFile1 = new Scanner(new File("E:\\text.txt"));

    List<String> temps = new LinkedList<String>();
    inFile1.useDelimiter(". ");

    while (inFile1.hasNext()) {
       token1 = inFile1.nextLine();
       temps.add(token1);
    }
    inFile1.close();

    String[] tempsArray = temps.toArray(new String[0]);
    for (int i = 0; i < tempsArray.length; i++) {
       System.out.println(tempsArray[i]);
    }

    int cnt = 0; //number of words in the string line
    for (int i=0; i<tempsArray.length; i++) {
        int k=0; //number of words that start from the letter "а"
        System.out.println("Line № = " + i);
        StringTokenizer st = new StringTokenizer(tempsArray[i]);           
        while (st.hasMoreTokens()) {
            cnt++;
            String s= st.nextToken();
            if (s.charAt(0)=='a') {                    
                k++;               
            }             
        }
        System.out.println("Number of words = " + cnt);
        cnt=0;
        System.out.println("Number of words 'а' = " + k);  
    }       
}

按照 Kau 的建议,我使用 Map。但是 Map 使用唯一的键。但我的 K 可以具有相同的值,而 Map 无法找到合适的字符串元素。我还可以使用哪些其他Сollection

最佳答案

我假设您已经有了 shell Short 对整数数组进行排序的算法。令该方法为shellSort(int[] a)。 您可以做的是创建一个 map ,其中键为 k ,值作为表示线路的字符串。同时,我们将创建一个包含所有 k 的整数数组。然后对 k 值数组调用方法 shellSort。然后从排序后的数组中读回,使用数组元素作为键在映射中查找。获取相应的 map 值(即行)并将它们一一放回 tempsArray 中,最终应以所需的方式对所有行进行排序。 下面是代码(未经测试)只是为了提供一个想法。

public static void main(String[] args) throws FileNotFoundException {
   String token1 = "";
   Scanner inFile1 = new Scanner(new File("E:\\text.txt"));

   List<String> temps = new LinkedList<String>();
   inFile1.useDelimiter(". ");

   while (inFile1.hasNext()) {
     token1 = inFile1.nextLine();
     temps.add(token1);
   }
   inFile1.close();

   String[] tempsArray = temps.toArray(new String[0]);
   for (int i = 0; i < tempsArray.length; i++) {
     System.out.println(tempsArray[i]);
   }

   int cnt = 0; //number of words in the string line
   Map<Integer, List<String>> myMap = new HashMap<Integer, List<String>>();
   int[] countArr = new int[tempsArray.length];
   for (int i=0; i<tempsArray.length; i++) {
       int k=0; //number of words that start from the letter "а"
       System.out.println("Line № = " + i);
       StringTokenizer st = new StringTokenizer(tempsArray[i]);           
       while (st.hasMoreTokens()) {
          cnt++;
          String s= st.nextToken();
          if (s.charAt(0)=='a') {                    
             k++;               
          }             
       }
       countArr[i] = k;
       List<String> listOfLines = myMap.get(k);
       if(listOfLines  == null){
          listOfLines = new ArrayList<String>();
          listOfLines.add(tempsArray[i]);
          myMap.put(k, listOfLines);
       } else{
          listOfLines.add(tempsArray[i]);
       }
       System.out.println("Number of words = " + cnt);
       cnt=0;
       System.out.println("Number of words 'а' = " + k);  
    }
    //Call shellsort here on the array of k values
    shellSort(countArr);
    List<String> sortedListOfLines = new ArrayList<String>();
    for(int i=0; i<countArr.length; i++){
       List<String> lineList = myMap.get(countArr[i]);
       if(lineList != null){
          sortedListOfLines.addAll(lineList);
          lineList = null;
          myMap.put(countArr[i], lineList);
       }
    }       
 }

关于java - 通过希尔排序对字符串行进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22213273/

相关文章:

java - 在java中将对象转换为接口(interface)并返回

mysql - 在mysql中按名称分组之前按日期和时间排序

java - 求最大的行和列,解不一致

c - 3 未经排序的数组中的最大数字,我在哪里缺少逻辑?

java - 通用可比较合并排序算法的 Stackoverflow 错误

java - 将 Delphi 转换为 Java(我的代码是正确的吗?)

java - Java中的getter和setter方法有什么作用,它们与普通方法有什么不同,为什么有时会缺少get方法?

java - 可以从 gmail 发送电子邮件,但不能从 yahoo 发送电子邮件

java - 如何拦截类型或方法的元注释

c# - 为什么 IComparer 要求您定义 IComparer.Compare(Object x, Object y) 而不仅仅是 Compare(Object x, Object y)?