Search:

Submit Article

# ProLog Program of Bubble Sort

Posted By: Milind Mishra     Category: Artificial Intelligence     Views: 19555

## Code for ProLog Program of Bubble Sort in Artificial Intelligence

```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(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

```
Share:

Didn't find what you were looking for? Find more on ProLog Program of Bubble Sort Or get search suggestion and latest updates.

 Milind Mishra author of ProLog Program of Bubble Sort is from India. View All Articles

• Comment should be atleast 30 Characters.
 Mehwish Sheraz from United Kingdom Comment on: Feb 28 I have used a similar code, however its only valid for numbers whereas I want to also use it for letters. What can I change in my code?`swap([X,Y|List],[Y,X|List]) :- X > Y. swap([Z|List],[Z|List1]) :- swap(List,List1). printlist([]) :- nl. printlist([List|_]) :- write(" ",List), printlist(List)`