Subj : Re: resolving a list of numbers to a range To : comp.programming From : rossum Date : Sat Aug 20 2005 12:06 am On Thu, 18 Aug 2005 09:26:41 GMT, Randy Howard wrote: >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; Minor point: low_end = list[0]; high_end = list[0]; index = 1; > >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); rossum The ultimate truth is that there is no ultimate truth .