Bubble Baseball

Crap, we adapted the insertion sort because our selection sort required too much moves to get the selected players to their position, but the insertion sort requires an inordinate amount of changes to get the border elements to their position within the sorted area without mixing the already sorted elements. At the end of the day, our selection variant was more efficient with at most 3*amountOfBase moves to sort one element (1 to get the hole alongside with the player, and 2 to get the hole+player in position) while our insertion variant requires at most 3*amountOfPlayers to sort one element (2 to descend the hole and player in position, 1 to get the hole back to its position). That's twice as bad as there is two players per base. It may be possible to improve the insertion sort by moving by more than one element when descending, but it seems uneasy (at least, while not mixing the already sorted elements) and it would probably only ensure that our insertion variant becomes as efficient as our selection variant, not dramatically better.

Se não pudermos tornar o ordenamento mais rápido, podemos torná-lo mais fácil. Se pensar a respeito, parece natural adaptar a ordenção bolha para este problema: o buraco se torna a bolha que se move para cima e para baixo, ordenando um pouco o array a cada passagem. As linhas grandes são simples: "enquanto (while) não estiver ordenado, mova o buraco para baixo até a base 0 (movendo o maior jogador de cada base em cada passo) e então de volta à base maximal (movendo o menor jogador de cada base)". depois de um tempo, estáOrdenado() vai retornar verdadeiro e seu algoritmo vai parar.

This is so easy that we introduce another variant of the problem, with more than two players per base. But actually, that shouldn't block you very long, should it?

Surprisingly, the bubble sort variant requires ways less moves than the other variants. This is astonishing because usually, the bubble sort performs much worse than the others sorts, but it comes from the very good match between its big lines and the baseball universe. It actually happens rather often that a pleasantly written algorithm performs very decently. But this is not an universal rule either, as demonstrated by the naive algorithm of the first exercise, that was nice, simple and wrong ;)