quicksort([X|Xs], Ys) :- partition(Xs, X, L, B), quicksort(L, Ls), quicksort(B, Bs), append(Ls, [X | Bs], Ys). quicksort([], []). partition([X|Xs], Y, [X|Ls], Bs) :- X =< Y, partition(Xs, Y, Ls, Bs). partition([X|Xs], Y, Ls, [X|Bs]) :- X > Y, partition(Xs, Y, Ls, Bs). partition([], Y, [], []). append([], X, X). append([H | T], X, [H | Z]) :- append(T, X, Z). ?- quicksort([4,2,8,12,1,7], X). X = [1,2,4,7,8,12] ->. yes