Skip to content

Instantly share code, notes, and snippets.

@kubek2k
Created January 26, 2012 12:05
Show Gist options
  • Save kubek2k/1682480 to your computer and use it in GitHub Desktop.
Save kubek2k/1682480 to your computer and use it in GitHub Desktop.
Quicksort in prolog
pivot(_, [], [], []).
pivot(Pivot, [Head|Tail], [Head|LessOrEqualThan], GreaterThan) :- Pivot >= Head, pivot(Pivot, Tail, LessOrEqualThan, GreaterThan).
pivot(Pivot, [Head|Tail], LessOrEqualThan, [Head|GreaterThan]) :- pivot(Pivot, Tail, LessOrEqualThan, GreaterThan).
quicksort([], []).
quicksort([Head|Tail], Sorted) :- pivot(Head, Tail, List1, List2), quicksort(List1, SortedList1), quicksort(List2, SortedList2), append(SortedList1, [Head|SortedList2], Sorted).
@kierankyllo
Copy link

kierankyllo commented May 14, 2022

Thank you. I used this for a school assignment and referenced you for credit.

@kubek2k
Copy link
Author

kubek2k commented May 14, 2022

@kierankyllo this gist aged well then :D - all the best in your future studies 🤞

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment