Skip to content

Instantly share code, notes, and snippets.

@KodaDono
Forked from PythonJedi/bubbleSort.pl
Created October 14, 2017 20:16
Show Gist options
  • Save KodaDono/8f3bab58f7efb896705b5e1c372a6d5e to your computer and use it in GitHub Desktop.
Save KodaDono/8f3bab58f7efb896705b5e1c372a6d5e 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