Re: sorting a linked list



Hi, Garvin:

I've just joined this news group, and saw your article. Question for you
and the other members of the newsgroup: would it not be more efficient to
go with a direct approach, something like:

% sort_list/2 identifies the head of the sorted list and sorts
% remaining elements using sort_list/3.
sort_list([],[]).
sort_list(List, [e(null,X) | Sorted]) :-
select(e(null,X), List, S1),
sort_list(X, S1, Sorted).

% sort_list/3 selects the list element that has the first
% parameter in the element's first slot, then sorts the
% remaining elements
sort_list(_,[],[]).
sort_list(X, List, [e(X,Y) | Sorted]) :-
select(e(X,Y), List, S1),
sort_list(Y, S1, Sorted).

% sample list provided in the first post.
sample_list([e(c,d),e(null,a),e(b,c),e(a,b)]).

In trying this out with the sample list that you provided, I'm thinking
that this approach is deterministic without relying on cuts or existential
checks (\=) in the body of the clauses, and is fairly efficient with
regard to choice-points and back-tracking. Of course, I haven't really
tried it with a poorly-formed list (no start element, broken links, etc.).

Cheers,
Roby

On Mon, 10 Nov 2008 04:55:37 -0800, garvin wrote:

forget it - now i have the solution. sometimes it helps just writing
down the problem. ;)

sort_pairs(PairList, OrderedList) :-
remove_member(PairList, (null, Next), RestList),
sort_pairs__(RestList, Next, OrderedList).

sort_pairs__([], Elem, [Elem]).
sort_pairs__([(null,Next)], _, [Next]). sort_pairs__(PairList, Elem,
[Elem|OrderedList]) :-
Elem \= null,
PairList \= [],
remove_member(PairList, (Elem,Next) , RestList), sort_pairs__
(RestList,
Next, OrderedList).


regards,
garvin

.



Relevant Pages

  • Re: Paul Holland re CB
    ... not wanting to share CB's private information with me or the newsgroup ... in that regard he is 'equal opportunity' ... any law enforcement office would need only a scant amount of time to ... find who and where he is - he's not hiding from anyone. ...
    (alt.med.fibromyalgia)
  • Occasionally, suppers found at coastal videos, unless theyre western.
    ... You let_'s the multiple attempt and regard it before its cult. ... Where did Yvette hold in spite of all the arms? ...
    (sci.crypt)
  • Re: Marcus in his natural environment
    ... experience that you (and many others in this newsgroup) have, ... You have no possible way of knowing about the life experiences of anonymous ... myself from this group altogether and joining brer_dudley on his back ... I love these sorts of "I'm strongly considering taking my ball and going ...
    (rec.music.dylan)
  • Re: Giving an application a window icon in a sensible way
    ... posted it neglected to mention that fact; the n00b naturally just ... newsgroup. ... That sounded to me like a reference to a specific thread. ... I put my reply with regard to quoted text. ...
    (comp.lang.java.programmer)
  • Re: Need help. Sorting on save in multi-sheet workbook.
    ... > individual sheets and sorts them when the file is saved. ... > Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, ... You have posted this message to the wrong newsgroup. ... The access in this groups name refers to Microsoft Access, ...
    (microsoft.public.access.forms)