LP/CLP Quick-Sort



next up previous WS Presentations Main Page
Next: Java Quick-Sort (I) Up: Document: intro Previous: Motivation

LP/CLP Quick-Sort

:- module(qs,[main/1]).
:- use_module(library(random)).

main(N) :-
        initialize(N, List),
        format('Sorting ~d elements....~n',[N]),
        statistics(runtime,_),
        qsort(List,_Result,[]),
        statistics(runtime,[_,T])
        format('Sorted in ~d milliseconds.~n',[T]).

initialize(0, []).
initialize(N, [E|Es]) :-
        N > 0, 
        N1 is N-1,
        random(1, 10000, E),
        initialize(N1, L, U, Es).

qsort([],L,L).
qsort([X|Xs],L,L2):-
        partition(Xs,X,Left,Right),
        qsort(Left,L,[X|L1]),
        qsort(Right,L1,L2).

partition([],_,[],[]).
partition([E|R],C,[E|Left1],Right):- E < C, !,
        partition(R,C,Left1,Right).
partition([E|R],C,Left,[E|Right1]):- E >= C, !,
        partition(R,C,Left,Right1).



Thu Jan 18 11:20:31 MET 1996 <herme@fi.upm.es>-<webmaster@clip.dia.fi.upm.es>