What it should do is form the groups by banner, then put the biggest group on one team. Next, it should fill the other team with banner groups, starting from second largest, until it has more players than the first team. Then the same procedure on the first team again, and so on, until all banner groups are distributed.
Then it should calculate the player difference between those two opposing clan groups, multiply it by 2 and fill the weaker team with randomers, until this limit is reached, then distribute the rest of the randomers evenly.
So if 10 greys are in one team, and 5 HRE and 3 STR in the other, and 12 randomers on the server, the grey team would receive another 5 random players and the HRE team 7, making it an 15 vs 15.