views:

92

answers:

1

For example, we have the following array:

char data[]=new char[]{'A','S','O','R','T','I','N','G','E','X','A','M','P','L','E'};

and an index array:

int  a[]=new int[]{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14};

void insitu (char data[], int a[], N)
{
    for (int i=0;i<N;i++)
    {
        char v=data[i];
        int j, int k;
        for (k = i; a[k] != i; k = a[j], a[j]=j)
        {
            j=k;
            data[k]=data[a[k];
        }
        data[k]=v;
        a[k]=k;
    }
}

My question is what j should be initialized to. When I run this code, it asks me to initialize j; what should I do?

+2  A: 

This is a Java implementation of the in-place sort in Sedgewick's Algorithms in C++ (see page):

public class InSitu {
    public static void main(String[] args) {
        int[] a = { 0, 10, 8, 14, 7, 5, 13, 11, 6, 2, 12, 3, 1, 4, 9 };
        char[] data = { 'A', 'S', 'O', 'R', 'T', 'I', 'N', 'G',
            'E', 'X', 'A', 'M', 'P', 'L', 'E' };
        insitu(data, a, a.length);
        System.out.println(java.util.Arrays.toString(a));
        // prints "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]"
        System.out.println(java.util.Arrays.toString(data));
        // prints "[A, A, E, E, G, I, L, M, N, O, P, R, S, T, X]"
    }
    static void insitu(char[] data,int[] a, int N) {
        for (int i = 0; i < N; i++) {
            char v = data[i];
            int j, k;
            for (k=i; a[k] != i; k = a[j], a[j] = j) {
                j = k;
                data[k] = data[a[k]];
            }
            data[k] = v;
            a[k] = k;
        }
    }
}

On array declarations

Please, please, do not make a habit of declaring arrays like this:

int x[];

You should instead put the brackets with the type, rather than with the identifier:

int[] x;

Related questions


On definite assignment

The compiler is smart enough to know when a local variable is definitely assigned, taking into account loop constructs, etc.

The following code compiles:

        int local;
        do {
            local = 0;
        } while (local != 0);

While this doesn't:

        int local;
        while (local != 0) { // doesn't compile!
            local = 0;
        }

Similarly, this compiles:

        for (int local; ; local++) {
            local = 0;
        }

This is because of the semantics of for loop, where the loop body (local = 0;) precedes the loop update (local++) in the control flow, even though it may not look like it in the text.

The specification doesn't allow the compiler to be too smart; for example the following doesn't compile:

    boolean b = false; // or whatever
    int local;
    if (b) {
        local = 0;
    }
    if (!b) {
        local = 1;
    }
    local++; // doesn't compile! 

But this does:

    boolean b = false; // or whatever
    int local;
    if (b) {
        local = 0;
    } else {
        local = 1;
    }
    local++;

See also

polygenelubricants
The java compiler will let you use `j` before it's initialized?
R0MANARMY
Java gives all variables a default value. The declaration *is* the initialization. This is defined in the JLS: http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html#10935 . See simpler explanation here: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/datatypes.html. So in this case, `j` is equal to `0`.
jasonmp85
I don't see anything assigning a value to `j` before this `k = a[j]`, or does that piece get executed after the loop body runs through once?
R0MANARMY
@jasonmp85: Class scope, you're right, method scope I'm pretty sure you're wrong.
R0MANARMY
@R0MANARMY: see latest revision.
polygenelubricants
You're right, my JLS link even says so: "A local variable (§14.4, §14.13) must be explicitly given a value before it is used, by either initialization (§14.4) or assignment (§15.26), in a way that can be verified by the compiler using the rules for definite assignment (§16)." But poly's answer is still valid: `j` is assigned within the loop before the end conditional is checked, so there's no read of an uninitialized variable.
jasonmp85
does it sort data?
Personally, I prefer the third form when writing my infinite loops.
jasonmp85
Very cool, thank you for the explanation.
R0MANARMY
@davit: It sorts `a`, and moves around elements of `data` accordingly.
polygenelubricants
@polygenelubricants data array is sorted?but in book result is different
yes a array is sorted but data array?
@davit: I've updated the answer to match the example in the book.
polygenelubricants
@ polygenelubricants thanks polygenelubricants