Minimum devide algorithm