MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Speeding up hierarchical sorting

  • To: mathgroup at smc.vnet.net
  • Subject: [mg34491] Re: [mg34455] Speeding up hierarchical sorting
  • From: BobHanlon at aol.com
  • Date: Thu, 23 May 2002 03:32:44 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

In a message dated 5/22/02 5:14:18 AM, 
johannes.ludsteck at wiwi.uni-regensburg.de writes:

>I would like to sort a list of lists (of integers)
>hierarchically. For example assume we have a list of
>records with the sex (encoded as 0 = male, 1 = female),
>number of childs and age of persons:
>
>{{0,0,15},{1,2,30},{0,0,10},{0,1,19},{1,3,65},...}.
>
>The task is to sort the dataset with respect to sex and
>then to sort each subset of people with equal sex with
>respect to the number of childs and so on... 
>
>I have programmed a simple (recursive) function doing this.
>HSort[m,si] takes a matrix m containing the records and
>a list si containing the indices specifying the hierarchical
>structure of the sorting procedure, i.e. if si = {1,3,2} then m is
>first sorted with respect to the first elements, then
>with respect to the third element and finally with respect to
>the second element of the records.
>
>Here is the implementation. I have two problems with it: first it is 
>rather slow

lst=Table[{Random[Integer], Random[Integer, {0,3}],
        Random[Integer, {25, 65}]}, {1000}];

colSort[lst_List, ci_Integer] :=
 
    Sort[lst, #1[[ci]]<=#2[[ci]]&];

hSort[lst_List, si_?VectorQ] :=
 
    Fold[colSort[#1, #2]&, lst, Reverse[si]];

Sort[lst] == hSort[lst, Range[3]]//Timing

{0.27 Second, True}


Bob Hanlon
Chantilly, VA  USA


  • Prev by Date: Re: simple program: help, please!
  • Next by Date: RE: generating random number
  • Previous by thread: Speeding up hierarchical sorting
  • Next by thread: RE: Speeding up hierarchical sorting