My thoughts as an enterprise Java developer.

Thursday, March 09, 2006

Sorting algorithms


http://www.cs.rit.edu/~atk/Java/Sorting/sorting.html
shows four sorting algorithms and animations for each. It shows Bubble, Quick, Odd-Even Transposition, and Shear sorts. The last two are multi-processor sorts so I have 2 questions:

  1. Could Bubble and Quick sort be changed to multi-processor sorts? It seems that Quick Sort could be.

  2. How would they compare in time to the other multi-processor sorts? It would be especially interesting how Quick Sort would compare.

Wednesday, March 01, 2006

Don't return in a finally clause

The code below actually prints "yikes!"
If you return in a finally block then any Throwables that aren't caught (in a catch block that is part of the same try statement as the finally block) will be completely lost. The really bad part about this is that it looks so innocent. I can think of no reason to return in a finally clause. If nothing else, catch Throwable and return in that so it is obvious what is happening. Note this also applies to other things that transfer control (continue, throw, etc).


public class Test {

public static void main(String[] args) {
try {
doSomething();
System.out.println("yikes! don't program like this!");
} catch (RuntimeException e) {
System.out.println("got it.");
}
}

public static void doSomething() {
try {
//Normally you would have code that doesn't explicitly appear
//to throw exceptions so it would be harder to see the problem.
throw new RuntimeException();
} finally {
return;
}
}
}