Compare every pair of intervals. If they overlap, merge them and repeat.
With intervals, there are pairs. After each merge, you might need to recheck.
Worst case: operations. Sorting first makes this much simpler.
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
The trap
Compare every pair of intervals. If they overlap, merge them and repeat.
With intervals, there are pairs. After each merge, you might need to recheck.
Worst case: operations. Sorting first makes this much simpler.