I have a code for quicksort in prolog:
gt(X,Y):- X @> Y.
conc([], List, List).
conc([Head|Tail], List1, [Head|List2]):- conc(Tail, List1, List2).
quicksort([], []).
quicksort([X|Tail], Sorted):-
split(X,Tail,Small,Big),
quicksort(Small,SortedSmall),
quicksort(Big, SortedBig),
conc(SortedSmall, [X|SortedBig], Sorted).
split(X,[],[],[]).
split(X,[Y|Tail],[Y|Small],Big):-
gt(X,Y),!,
split(X,Tail,Small, Big).
split(X,[Y|Tail],Small,[Y|Big]):-
split(X,Tail,Small,Big).
The example is quicksort([3,2,4,1,5], Sorted)
.
I've almost draw this one, but I only find the trace for a list of Small=[2, 1]
, then I coudn't do the same for a list of Big
number.
Is there anybody can help me to draw a diagram for this code? I want to understand the running trace the program.
I'd really appreciate that!