implemented median based grouping