Which part causes an infinite loop?  The outermost loop clearly counts from 0 to length - 1.  The first inner loop counts from 0 to 7 (inclusive), by incrementing j.  The second inner loop counts from 7 to 0 (inclusive) by decrementing j.  I did run a quick test to make sure it ran well and I didn't pick up anything looking like an infinite loop.  Can you tell me what you did so I can try to reproduce this?