The Full Counting Sort

  • + 0 comments
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    public class CountingSortStrings {
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int n = scanner.nextInt();
            
            // Initialize the counting array
            List<List<String>> countingArray = new ArrayList<>();
            for (int i = 0; i < 100; i++) {
                countingArray.add(new ArrayList<>());
            }
    
            // Read and sort the input strings
            for (int i = 0; i < n; i++) {
                int num = scanner.nextInt();
                String str = scanner.next();
    
                // If it's in the first half, replace the string with a dash
                if (i < n / 2) {
                    countingArray.get(num).add("-");
                } else {
                    countingArray.get(num).add(str);
                }
            }
    
            // Print the sorted strings
            for (List<String> list : countingArray) {
                for (String str : list) {
                    System.out.print(str + " ");
                }
            }
    
            scanner.close();
        }
    }