-
-
Save KodaDono/8f3bab58f7efb896705b5e1c372a6d5e to your computer and use it in GitHub Desktop.
Bubble Sort in prolog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% Bubble sort in prolog, because bubble sort | |
bubble_sort([],Sorted) :- | |
Sorted = []. | |
bubble_sort([X], Sorted) :- | |
Sorted = [X]. | |
bubble_sort(Terms, Sorted) :- | |
bubble(Terms, Terms), Sorted = Terms ; | |
bubble(Terms, Partials), bubble_sort(Partials, Sorted). | |
bubble([], Bubbled) :- Bubbled = []. | |
bubble([X], Bubbled) :- Bubbled = [X]. | |
bubble([X,Y|Terms], [Y|Bubbled]) :- | |
Y < X, bubble([X|Terms], Bubbled). | |
bubble([X,Y|Terms], [X|Bubbled]) :- | |
X =< Y, bubble([Y|Terms], Bubbled). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment