Logo 
Search:

Artificial Intelligence Articles

Submit Article
Home » Articles » Artificial Intelligence » ProLogRSS Feeds

ProLog Program of Bubble Sort

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

Program to sort using Bubble Sort.

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

  
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
Milind Mishra author of ProLog Program of Bubble Sort is from India.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
Anita Jain from United States Comment on: Jun 21
why is it so?plz suggest me what to do for the given problem

Anita Jain from United States Comment on: Jun 21
I've written the same code for bubble sort but it is not giving me the correct output,the list entered is given as output.

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)

View All Comments