Skip to content

Instantly share code, notes, and snippets.

@PythonJedi
Created March 1, 2017 03:56
Show Gist options
  • Save PythonJedi/e3a9c63dc594c370e4043a7cf24275ae to your computer and use it in GitHub Desktop.
Save PythonJedi/e3a9c63dc594c370e4043a7cf24275ae to your computer and use it in GitHub Desktop.
Bubble Sort in prolog
% 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