Why do algae and fungi shift to sexual mode of reproduction just before the onset of adverse conditions?

Organisms such as fungi and algae switch to sexual mode of reproduction during adverse conditions because sexual reproduction brings variation into the individuals, some of which might help the individuals to adapt to the changed conditions and survive.This ensures the continuity of species.