Optimal spritesheet algorithm
===

One thing I've noticed about spritesheets is that their filesize is not entirely consistent. When you have individual images, assuming they are compressed properly, their filesize won't change. But with spritesheets, the filesize of the image can vary a lot depending on how the images are arranged.

As an example, I created this spritesheet:  
![Sheet](http://pokebattlers.net/img/misc/forumstars.png) I tried horizontal, I tried 2x3 in portrait and landscape, I tried all `6! = 720` possible arrangements for the images, and what I found was that the order of the stars made absolutely no difference (I didn't really expect it to, given the image is indexed and so all that changed was the palette being rearranged), but the vertical layout produced a smaller file than any other.

Is there any algorithm that can help determine the optimal arrangement of images in a spritesheet, or is it just a case of brute-force "try everything and find the smallest one"?

Also, why is it that a vertical lineup produces a smaller file than a horizontal one in this and the few other cases I've tried?