Algorithm helps explain sex in evolution