iFlash
iFlash

Reputation: 15

String lines sorting by Shell Sort

I have a text file of 50 string lines of varying length and content. I need to read the file and sort in ascending order. Sorting condition: the number of words in a sentence that start from the letter "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);  
    }       
}

I use Map as Kau advise me. But Map use unique keys. But my K can have same values and Map can't find an appropriate string element. What other Сollection сan I use?

Upvotes: 1

Views: 604

Answers (1)

kau
kau

Reputation: 372

I am assuming you already have the algorithm for shell short to sort an array of integers. Let the method be shellSort(int[] a). What you can do is create a map with key as k and value as the string representing the line. At the same time we'll create an array of integers that holds all k . Then call the method shellSort on the array of k values. Then read back from the sorted array, look in the map using the array elements as keys. Fetch the corresponding map values (which are the lines) and put them back one by one in tempsArray which should finally have all the lines sorted in the desired way. Below is the code (untested) just to give an idea.

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);
       }
    }       
 }

Upvotes: 1

Related Questions