The Madelung rule tries to establish the order in which electrons fill energy sublevels of atoms. The aufbau principle says that in the ground state of an atom, electrons always occupy the lowest available energy sublevel.
The Madelung rule, summarized in the diagram below, shows the order electrons add to sublevels.
The Madelung Rule is unreliable for elements higher than element 20, calcium. This is because the interactions between nuclei and orbitals and mutual electron-electron repulsions become too complex to be summarized in one simple rule.
For example, The Aufbau Principle and Transition Elements considers why the transition elements of the periodic table's fourth row do not follow the Madelung rule.