tags:

views:

85

answers:

2

I have question about hoare partition method. Here is code and also pseudo-code. Please if something is wrong, correct pseudo code.

HOARE-PARTITION ( A, p, r)
 1 x ← A[ p]
 2 i ← p−1
 3 j ← r +1
 4 while TRUE
 5        do repeat j ← j − 1
 6        until A[ j ] ≤ x

 7        do repeat i ← i + 1
 8        until A[i] ≥ x

 9        if i < j
10             then exchange A[i] ↔ A[ j ]
11             else return j

and my code

public class Hoare {
    public static  int partition(int a[],int p,int r) {
        int x = a[p];
        int i = p-1;
        int j = r+1;

        while (true) {    
            do
            {
                j=j-1;
            }  while(a[j]>=x);

            do
            {
                i=i+1;
            } while(a[i]<=x);

            if (i<j) {
                int t=a[i];
                a[i]=a[j];
                a[j]=t;
            } else {    
                return j;
            }
        }        
    }

    public static void main(String[]args) {
        int a[]=new int[]{13,19,9,5,12,8,7,4,11,2,6,21};
        partition(a,0,a.length-1);
    }
}

And mistake is this:

error: Class names, 'Hoare', are only accepted if annotation
processing is explicitly requested
1 error

Any ideas?

+1  A: 

You seem to have translated:

do repeat stmt
until A[ j ] ≤ x;

into

do {
    stmt;
} while (A[j] >= x);

This is not correct. The comparison which is the opposite of ≤ would be >, not ≥. Therefore, your comparison when translating from "until" to "while" would use > such as while (A[j] > x);.

The other errors in your possible homework are similar and I encourage you to think each translation through carefully on your own.

Heath Hunnicutt
+1  A: 

After re-formatting your question, I now see what you asked originally. The answer to your question is found in the Java Documentation

In your case, you need to use javac Hoare.java to compile.

Class names, 'HelloWorldApp', are only accepted if annotation processing is explicitly requested

If you receive this error, you forgot to include the .java suffix when compiling the program. Remember, the command is javac HelloWorldApp.java not javac HelloWorldApp.
Heath Hunnicutt