2

For my method, partition, for my quick sort, it is okay when I run it. However, whenever I upload on the project submit page, the testing returns negative result for the design of this code. which is like

static long partition(DataArray array, long l, long r, Record pivot)
        throws Exception {

    do { // Move bounds inward until they meet
        while (array.get(++l).compareTo(pivot) < 0)
            ;

this line, i get.. three errors...

Error [Checkstyle]: -2 This statement is just a ';' and, therefore, does nothing. You may have added a ';' in an inappropriate location (like after an if or a for loop). See if you are missing code or just delete the extra ';'.

Error [PMD]: -1 (limit exceeded) It is a good idea to always enclose the code in the body of a while loop in braces. It helps to reduce the likelihood of errors.

Error [PMD]: 0 (limit exceeded) The body of this while loop is empty. Make sure you do not have a semicolon (;) after the while by mistake. If you do not need to do anything in the body of the loop you can probably omit the loop entirely.

        while ((r != 0) && ((array.get(--r).compareTo(pivot) > 0)))
            ;

and another three errors here

Error [Checkstyle]: -2 This statement is just a ';' and, therefore, does nothing. You may have added a ';' in an inappropriate location (like after an if or a for loop). See if you are missing code or just delete the extra ';'.

Error [PMD]: 0 (limit exceeded) It is a good idea to always enclose the code in the body of a while loop in braces. It helps to reduce the likelihood of errors.

Error [PMD]: 0 (limit exceeded) The body of this while loop is empty. Make sure you do not have a semicolon (;) after the while by mistake. If you do not need to do anything in the body of the loop you can probably omit the loop entirely.

        swap(array, l, r); // Swap out-of-place values
    }

    while (l < r); // Stop when they cross
    swap(array, l, r); // Reverse last, wasted swap
    return l; // Return first position in right partition
}

I know this testing server is wired, so I get points off from this kind of reason. How can I re-write this code that will perform the same thing?

Jieun Chon
  • 139
  • 1
  • 10

1 Answers1

2

Those Checkstyle & PMD messages are not exactly errors. They show places of likely errors and bad programming style.

They are wrong to some degree in your case:

while ((r != 0) && ((array.get(--r).compareTo(pivot) > 0))) ;

does it's job inside the condition, the ; and omission of {} are intentional because there is no body for the loop. The primary reason for those error messages are cases where you have a body to your loop. Or intended to have one but accidentally added a ; and now you don't.

However, constructs like that, that "abuse" the part that evaluates an expression to do the actual work get complicated to read very quickly. Complicated is not good style.

while (r > 0) {
    T element = array.get(--r);
    if (element.compareTo(pivot) <= 0) {
        break;
    }
}

while being much more verbose, should be the same as before. (r > 0 is more stable than r != 0 so I've changed that.) I guess it will pass the style checks that way.

You can alternatively try to replace the ; with a block with some comment in it (because "empty blocks should be documented" should be another style thing, it is at least in eclipse.) Depends on what those tools consider bad style.

while ((r != 0) && ((array.get(--r).compareTo(pivot) > 0))) { /* empty */ }
zapl
  • 63,179
  • 10
  • 123
  • 154