Interchange sort

Description

Sorting by Interchange

Source: The Art of Prolog

Download

Program source code: interchange_sort.pl

Listing

/*
    mysort(Xs,Ys) :- 
	Ys is an ordered permutation of the list of integers Xs.
*/
    mysort(Xs,Ys) :-
	append(As,[X,Y|Bs],Xs),
	X > Y,
	!,
	append(As,[Y,X|Bs],Xs1),
	mysort(Xs1,Ys).
    mysort(Xs,Xs) :-
	ordered(Xs), !.
 
	ordered([]).
	ordered([X]).
	ordered([X,Y|Ys]) :- X =< Y, ordered([Y|Ys]).
 
%  Program 11.5   Interchange sort

Comments

pl/prolog/pllib/interchange_sort.txt · ostatnio zmienione: 2019/06/27 15:50 (edycja zewnętrzna)
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0