caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: John Max Skaller <skaller@ozemail.com.au>
To: Mattias Waldau <mattias.waldau@abc.se>
Cc: caml-list@inria.fr
Subject: Re: sorting of list of vectors (array with one dimension)
Date: Thu, 30 Nov 2000 18:37:51 +1100	[thread overview]
Message-ID: <3A2603CF.698A368D@ozemail.com.au> (raw)
In-Reply-To: <HDEEKOMJILGEIHIMAPCDIEBPDLAA.mattias.waldau@abc.se>

Mattias Waldau wrote:
> 
> I have a list of vectors, I would like to sort according to the first
> vector, and move the corresponding data in the other vectors.
> 
> Is there a function for this (for example, a sort function, where I add a
> function that is called each time data is swapped) or do I have to write a
> quicksort in ocaml.

Indirection is the solution to everything :-)

Make a vector of indexes, initially 0,1,2,3 ... n-1,
and sort them with a 'compare' routine which looks at your first vector.

When finished, reorganise all the vectors in the new order indicated
by the indices. You can now move the element that should be in slot 0
there,
saving the old contents of slot 0, 
and rippling through the first cycle until the location for the saved
data is freed. The problem is to find the next cycle. The easiest way I
can think
of is to flag which slots have been fixed, and find any one that has
not,
if any, and start the ripple again from there. I don't see, immediately,
how to do this in less than linear time. Anyone?

Example: sorting "helowrd"
                  0123456

leads to "dehlorw"
          6102354

so we have to move: 6->0, 1->1, 0->2, 2->3, 3->4, 5->5, 4->6.
The cyclic decomposition is:

	(60234)(1)(5)



-- 
John (Max) Skaller, mailto:skaller@maxtal.com.au
10/1 Toxteth Rd Glebe NSW 2037 Australia voice: 61-2-9660-0850
checkout Vyper http://Vyper.sourceforge.net
download Interscript http://Interscript.sourceforge.net



  reply	other threads:[~2000-11-30  7:57 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-11-21  8:23 Jocelyn Serot
2000-11-29 17:36 ` sorting of list of vectors (array with one dimension) Mattias Waldau
2000-11-30  7:37   ` John Max Skaller [this message]
2000-11-30 11:47     ` Sven LUTHER
2000-11-30 12:21       ` John Max Skaller
2000-12-01 18:44         ` Mattias Waldau
2000-12-02 16:00           ` John Max Skaller
2000-11-30 11:33   ` Judicael Courant

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3A2603CF.698A368D@ozemail.com.au \
    --to=skaller@ozemail.com.au \
    --cc=caml-list@inria.fr \
    --cc=mattias.waldau@abc.se \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).