Question

I am now working on radix sort that implements count sort. I think I have for the most part understood and followed the pseudo code, but I am getting an array out of bounds error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 12
    at RunRadixSort.radixSort(RunRadixSort.java:47)
    at RunRadixSort.main(RunRadixSort.java:15)

My Code

import java.text.DecimalFormat;
    import java.util.Arrays;


   import java.text.DecimalFormat;
import java.util.Arrays;


public class RunRadixSort {


    public static void main(String[] args) {

        int[] sortNumbers = {4,5,6,2,3,7,2,1,23,5,13};
        int[] sorted = new int[sortNumbers.length];
        DecimalFormat df = new DecimalFormat("#.########");
        int max = getMax(sortNumbers);
        long timeStart = System.nanoTime();
        sorted = radixSort(sortNumbers, max);
        long timeEnd = System.nanoTime();
        long elapsedTime = timeEnd - timeStart;
        double time = (double)elapsedTime / 1000000;
        System.out.println(Arrays.toString(sorted));
        System.out.println("\nTotal Execution Time: " + df.format(time)+ " miliseconds");
    }

    public static int getMax(int[] A){
        int max = A[0];
        for(int i = 1; i < A.length; i++){
            if(A[i] > max){
                max = A[i];
            }
        }
        return max;
    }

    public static int[] radixSort(int[] A, int d){
        int[] B = new int[A.length];
        int[] C = new int[d + 1];
        for(int i = 0; i < d; i++){
            for(int k = 0; k < A.length; k++){
                C[A[k]]++;
            }
            int total = 0;
            for(int l = 0; l < d; l++){
                int temp = C[l];
                C[l] = total;
                total += temp;
            }
            for(int m = 0; m < A.length; m++){
                B[C[A[m]]] = A[m];
                C[A[m]]++;
            }
        }
        return B;
    }
}
Was it helpful?

Solution

You are not incrementing j. This could be a typo:

 for(int j = 0; j < d; i++){

try this:

for(int j = 0; j < d; j++){

OTHER TIPS

In the line for(int j = 0; j < d; i++){

it should be for(int j = 0; j < d; j++){

Also, the line C[A[k]] = C[A[k]] + 1; When k=8 and A[K]=23 you're going to get an ArrayOutOfBoundsException because when C[] is declared it is declared as an array of length 23 which you can only indexed from 0 to 22 inclusive. The actual range of values should include 0 through the max.

So, you need to either declare C[] as int[] C = new int[d+1]; or store to values in C[A[k]-1] = C[A[k]-1] + 1;, depending on whether you consider zero an acceptable value in the input array sortNumbers.

The question and code changed however. This block of code sums the values and increasingly adds them into Array C.

  for(int l = 0; l < d; l++){
            int temp = C[l];
            C[l] = total;
            total += temp;
        }

Then the next block

 for(int m = 0; m < A.length; m++){
            B[C[A[m]]] = A[m];
            C[A[m]]++;
        }

Uses the value of entries in array C as indexes in array B however B is the same size as array A.

Are you sure C should hold the total of the values rather than the total of the occurrences of the values?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top