[Date Prev] [Date Next] [Thread Prev] [Thread Next] Date Index Thread Index Search archive:
Date:Mon Sep 2 22:58:05 1993 
Subject:Re: Insertion sort in Pop? 
From:Steve Knight 
Volume-ID:930903.01 

Ruvan asks:
>       Does anyone have code for this - or what is the best way to do such a
> sort from prolog? (My problem is that I have random numbers (say) coming in
> 'asynchronously' which I need to place in order so that I can 'pop' off the 
> highest from the 'top' of the 'stack' at any time... while numbers still keep 
> coming in).

There is simply no "best" answer -- it all depends on what constraints
apply to the arrival and departures.  However, I do have a couple
of helpful remarks :-

(1) Don't use Prolog's sort because it eliminates duplicates.

(2) If the numbers of arrivals - departures is fairly small (e.g. 10), 
    just use Pop11's syssort routine and keep them in a list.  This sorting 
    algorithm takes advantages of pre-existing runs.  It is lazier than
    writing your own.   But that's the point.

(3) If the number of arrivals - departures is not large (e.g. < 100) ,
    then write the obvious insertion routine.  It's about 6 lines
    of Pop code or 3 Prolog clauses.

(4) If the number is in the region of 100-5000, implement the queue
    as a binary tree.  It is still very simple but degrades smoothly.

(4) If there is often a large number of pending entries (e.g. > 5000), 
    then these dumb techniques look a bit pedestrian.  If you don't
    need real-time-like response it becomes appropriate to write
    adaptive store algorithms.  If you do need real-time response then
    you must exploit structure in the problem.

Steve