Subj : Re: resolving a list of numbers to a range To : comp.programming From : Randy Howard Date : Thu Aug 18 2005 10:26 am Espen Suenson wrote (in article ): > placid wrote: > >> Hi all, >> >> I was just if anyone knows a way to resolve a list of integers say >> {1,2,3,4,5} into a range that is 1-5? > > Here's an algorithm: > > 1) Find the smallest integer in the list, this is A. > 2) Find the largest integer in the list, this is B. > 3) Return the range A-B. > > One way of doing steps 1 and 2 is to sort the list. Then the smallest > integer will be the first element and the largest integer will be the last > element in the list. "It depends", and I know I'm going to regret posting this when short on sleep and not knowing how "list" is stored, but you might find that simply walking through the list, with two variables, such as this pseudo-example is faster than sorting it... low_end = BIGGEST_VALUE_POSSIBLE; high_end = SMALLEST_VALUE_POSSIBLE; while (!end_of_list(list, index)) { if (list[index].value < low_end) low_end = list[index].value; if (list[index].value > high_end) high_end = list[index].value; index++; } assert(low_end < high_end); return range(low_end, high_end); -- Randy Howard (2reply remove FOOBAR) .