The quicksort sorting algorithm, relating a list to its sorted version: partition(, _, , ). partition(, Pivot, Smalls, Bigs) :- ( X @< Pivot -> Smalls = , partition(Xs, Pivot, Rest, Bigs) ; Bigs = , partition(Xs, Pivot, Smalls, Rest) ). quicksort() --> . quicksort() --> { partition(Xs, X, Smaller, Bigger) }, quicksort(Smaller), , quicksort(Bigger).
Следува алгоритмот за брзо сортирање (Quicksort) имплементиран во Prolog: partition(, _, , ). partition(, Pivot, Smalls, Bigs) :- ( X @< Pivot -> Smalls = , partition(Xs, Pivot, Rest, Bigs) ; Bigs = , partition(Xs, Pivot, Smalls, Rest) ). quicksort() --> . quicksort() --> { partition(Xs, X, Smaller, Bigger) }, quicksort(Smaller), , quicksort(Bigger).