Thursday, February 24, 2011

Bubble sort for prolog

Hello again,
this time I've found a version of bubble sort here. I wanted to provide my version, which is less iterative and, I think, more intuitive. What it does is, simply, bubble until it's sorted:

bubblesort(L1, L2) :- bubblesort2(L1,L2,unsorted),!.
bubblesort2(L,L,sorted).
bubblesort2(L1,L2,unsorted) :- bubble(L1,L3,C), bubblesort2(L3,L2,C).
bubble([],[],sorted).
bubble([X],[X],sorted).
bubble([X,Y|L], [X|L1], C) :- X <= Y, bubble([Y|L],L1,C).
bubble([X,Y|L], [Y|L1], unsorted) :- X > Y, bubble([X|L],L1,_).

Yes, the exam is tomorrow so I will finally stop annoying you readers ;)

2 comments:

pandith13 said...

Good post thanks for share information.
SRI ANNAPOORNESHAWARI ASTROLOGY CENTER.Best Astrologer In Maryland

Astrologer Vivek said...

Our Astrologer Vivek is a great person where he can solve all the problems with all of his astrological tricks and knowledge. He used to do house poojas to clear all the bad vibrations. He makes you feel better from any of the problems. He does all his skills to clear the magical effects. He dictates the slogans for protecting us from all the magical effects. He can help all kind of problems in magical effects by his astrological knowledge.

Black Magic Removal in Sydney