Counting Sort 1

Sort by

recency

|

655 Discussions

|

  • + 0 comments

    In java:

            int frequency = 100;
            List<Integer> alternativeSortingList = new ArrayList<>(Collections.nCopies(100,0));
            for (Integer integer : arr) {
                alternativeSortingList.set(integer, alternativeSortingList.get(integer)+1 );
            }
            return alternativeSortingList;
    
  • + 0 comments

    Remember that the dimension of the output is predetermined... "int[100]: a frequency array"

  • + 0 comments

    Java 8

    public static List<Integer> countingSort(List<Integer> arr) {
    // Write your code here
    
        // Integer maxInt = Collections.max(arr);
    
        int[] intArr = new int[100];
    
        for (Integer arg : arr) {
            intArr[arg] = intArr[arg] += 1;
        }
    
        return Arrays.stream(intArr).boxed().collect(Collectors.toList());
    }
    
  • + 0 comments
    ** The Collections.nCopies(100, 0) creates a list with 100 elements, all initialized to 0 **
    

    List count = new ArrayList<>(Collections.nCopies(100, 0)); for (int num : arr) { count.set(num, count.get(num) + 1); } return count; }

  • + 0 comments

    ' def countingSort(arr): # Write your code here count = [0] * 100

    for i in range(len(arr)):
        count[arr[i]] += 1
    return count
    

    '