Search code examples
javaalgorithmsortingradix-sort

radix sort break down


this is a simple question, Im trying to figure out radix sort, I was given this code, but cant quit figure out why we need the W, it seems like it uses that for the length of the words given but, I was wondering if the words arent fixed then what?

and why do we need to extend the ascii size?

why do we need to compute frequency counts and compute cumulates?

we technically don't need those two for loops right?

thanks

public static void sort(String[] a, int W) 
{
    int N = a.length;
    int R = 256;   // extend ASCII alphabet size
    String[] aux = new String[N];

    for (int d = W-1; d >= 0; d--) {
        // sort by key-indexed counting on dth character

        // compute frequency counts
        int[] count = new int[R+1];
        for (int i = 0; i < N; i++)
            count[a[i].charAt(d) + 1]++;

        // compute cumulates
        for (int r = 0; r < R; r++)
            count[r+1] += count[r];

        // move data
        for (int i = 0; i < N; i++)
            aux[count[a[i].charAt(d)]++] = a[i];

        // copy back
        for (int i = 0; i < N; i++)
            a[i] = aux[i];
    }
}

Solution

  • but cant quit figure out why we need the W, it seems like it uses that for the length of the words given but, I was wondering if the words arent fixed then what?

    If the strings are longer, then the remaining characters will not take part in the sort. If the strings are shorter, then I believe you'll get an IndexOutOfBoundException

    and why do we need to extend the ascii size?

    As mentioned by Blorgbeard, this refers to the "extended" ASCII set. ASCII is not defined passed 127. The "extended" ASCII includes characters from 0 to 255.

    why do we need to compute frequency counts and compute cumulates?

    This is to sort the strings on the dth character. Actually, the first three parts perform the sort and the last copies back the partially sorted array.

    we technically don't need those two for loops right?

    Technically, you could find another method to sort the strings on the dth character but this seems to do the job quite well.