Counting Sort 2

  • + 1 comment

    The problem description should be more clear about what a counting sort algorithm actually is. I see alot of people here sorting in all sorts of different ways, when they are intended to use a specific algorithm, counting sort.

    • + 3 comments
      static int[] countingSort(int[] arr) {
              int n = arr.length;
              int k = arr[0];
              for(int i=1;i<n;i++) {
                  if(arr[i]>k) {
                      k = arr[i];
                  }
              }
              int count[] = new int[k+1];
              for(int i=0;i<n;i++) {
                  ++count[arr[i]];
              }
              for(int i=1;i<k+1;i++) {
                  count[i] = count[i]+count[i-1];
              }
              int b[] = new int[n];
              for(int i=n-1;i>=0;i--) {
                  b[--count[arr[i]]] = arr[i];
              }
              for(int i=0;i<n;i++) {
                  arr[i] = b[i];
              }
              return arr;
          }
      
      • + 1 comment
        [deleted]
        • + 0 comments

          here is problem solution in java python c++ c and javascript programming. https://programs.programmingoneonone.com/2021/04/hackerrank-counting-sort-2-solution.html

      • + 0 comments

        Is it a solution from Jenny's lecture?