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 ;)

6 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

panditramdial said...

Pandit Ramdial Ji Famous Astrologer in Hamilton is specialized in all terms in astrology. His practice in astrology has reached many targeted issues to be resolved successfully. He advocates that many people pray to God to illuminate their life.

Famous Astrologer in Hamilton

DurgaAnugarha said...

Very good article,Thank you

DURGAANUGARHA ASTROLOGY.Best Astrologer In tumkur

abhiramindia said...

Great post, thanks

ABHIRAM ASTROLOGY CENTER.Best Astrologer In whitefield

Anonymous said...

Fast-track your data analytics and machine learning course with guaranteed placement opportunities. Most extensive, industry-approved experiential learning program ideal for future Data Scientists.