home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!think.com!barmar
- From: barmar@think.com (Barry Margolin)
- Newsgroups: comp.lang.lisp
- Subject: Re: sorting arrays
- Date: 30 Dec 1992 05:12:38 GMT
- Organization: Thinking Machines Corporation, Cambridge MA, USA
- Lines: 26
- Message-ID: <1hrb46INNjgf@early-bird.think.com>
- References: <C01pCC.MFK@acsu.buffalo.edu>
- NNTP-Posting-Host: gandalf.think.com
-
- In article <C01pCC.MFK@acsu.buffalo.edu> lewocz@acsu.buffalo.edu (John S. Lewocz) writes:
- >What would be the most effiecient way to sort a two-dimensional
- >array using an arbitrary column
- >as a key?
-
- I can't vouch for it being "the most efficient way", but here's a
- reasonably simple way that seems like it shouldn't be too inefficient.
- Make a list or vector of displaced arrays, where each elements is a vector
- displaced to a row of the original array. Sort this using :KEY #'(LAMBDA
- (X) (AREF X COLUMN-NUMBER)), and then combine the results into a new 2-D
- array.
-
- Some of the sources of inefficiency in this may be all the references to
- displaced arrays and the copying of the elements into the result. You can
- avoid the former (at the cost of more consing and copying) by copying the
- rows into new vectors at the beginning instead of using displaced arrays.
-
- If you need to avoid all copying, then you'll have to do the sorting in
- place, and there's nothing built into Lisp (Common Lisp, at least) to do
- this, so you'll have to write your own sort routine.
-
- --
- Barry Margolin
- System Manager, Thinking Machines Corp.
-
- barmar@think.com {uunet,harvard}!think!barmar
-