Domains
list = integer*.
Predicates
bubblesort(list,list).
swap(list,list).
printlist(list).
Clauses
bubblesort(InputList,SortList) :-
swap(InputList,List) , ! ,
printlist(List),
bubblesort(List,SortList).
bubblesort(SortList,SortList).
swap([X,Y|List],[Y,X|List]) :- X > Y.
swap([Z|List],[Z|List1]) :- swap(List,List1).
printlist([]) :- nl.
printlist([Head|List]) :-
write(Head, " "),
printlist(List).
Output :
Goal: bubblesort([2,3,1,4],L).
2 1 3 4
1 2 3 4
L=[1,2,3,4]
1 Solution
Goal: bubblesort([2,4,1,3,5,9,6],L).
2 1 4 3 5 9 6
1 2 4 3 5 9 6
1 2 3 4 5 9 6
1 2 3 4 5 6 9
L=[1,2,3,4,5,6,9]
1 Solution