Algorithm for the minimization of discontinuous functions