Permutation sort

Description

Permutation sort

Source: The Art of Prolog

Download

Program source code: permutation_sort.pl

Listing

/*			       
	sort(Xs,Ys) :- 
		The list Ys is an ordered permutation of the list Xs.
*/
 
	sort(Xs,Ys) :- permutation(Xs,Ys), ordered(Ys).
 
	permutation(Xs,[Z|Zs]) :- select(Z,Xs,Ys), permutation(Ys,Zs).
	permutation([],[]).
 
	ordered([]).
	ordered([X]).
	ordered([X,Y|Ys]) :- X =< Y, ordered([Y|Ys]).
 
	select(X,[X|Xs],Xs).
	select(X,[Y|Ys],[Y|Zs]) :- select(X,Ys,Zs).
 
%	Program 3.20 Permutation sort

Comments

pl/prolog/pllib/permutation_sort.txt · ostatnio zmienione: 2017/07/17 08:08 (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