Created
January 26, 2012 12:05
-
-
Save kubek2k/1682480 to your computer and use it in GitHub Desktop.
Quicksort in prolog
This file contains 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
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). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you. I used this for a school assignment and referenced you for credit.