Created
March 1, 2017 03:56
-
-
Save PythonJedi/e3a9c63dc594c370e4043a7cf24275ae 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