Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

counting sort java

public static void CountingSort(int A[], int k) {//k is equal to A.length
        int[] B = new int[k + 1];
        int max = A[0];
        for (int i = 1; i < k; i++) {
            if (A[i] > max)
                max = A[i];
        }
        int[] C = new int[max + 1];
        for (int i = 0; i < max; ++i) {
            C[i] = 0;
        }
        for (int i = 0; i < k; i++) {
            C[A[i]]++;
        }
        for (int i = 1; i <= max; i++) {
            C[i] += C[i - 1];
        }
        for (int i = k - 1; i >= 0; i--) {
            B[C[A[i]] - 1] = A[i];
            C[A[i]]--;
        }
        for (int i = 0; i < k; i++) {
            A[i] = B[i];
        }
    }
 
PREVIOUS NEXT
Tagged: #counting #sort #java
ADD COMMENT
Topic
Name
3+5 =