Also so funktioniert es zumindest:
Code Alles anzeigenmysort([], []). mysort([X], [X]). mysort([Pivot|Tail], Result) :- leftFromPivot(Pivot, Tail, Left), rightFromPivot(Pivot, Tail, Right), flatten([Left, Pivot, Right], Result). leftFromPivot(Pivot, [], []). leftFromPivot(Pivot, [H|T], U) :- H > Pivot, leftFromPivot(Pivot, T, U). leftFromPivot(Pivot, [H|T], [H|U]) :- H =< Pivot, leftFromPivot(Pivot, T, U). rightFromPivot(Pivot, [], []). rightFromPivot(Pivot, [H|T], U) :- H =< Pivot, rightFromPivot(Pivot, T, U). rightFromPivot(Pivot, [H|T], [H|U]) :- H > Pivot, rightFromPivot(Pivot, T, U).
....so funktioniert bei mir aber nicht...
linke und rechte Seite muss noch geordnet werden...
habs so erweitert...
Code
mysort([], []).
mysort([X], [X]).
mysort([Pivot|Tail], Result) :-
leftFromPivot(Pivot, Tail, Left), mysort(Left, SortedLeft),
rightFromPivot(Pivot, Tail, Right), mysort(Right, SortedRight),
flatten([SortedLeft, Pivot, SortedRight], Result), !.
leftFromPivot(_Pivot, [], []).
leftFromPivot(Pivot, [H|T], U) :- H > Pivot, leftFromPivot(Pivot, T, U).
leftFromPivot(Pivot, [H|T], [H|U]) :- H =< Pivot, leftFromPivot(Pivot, T, U).
rightFromPivot(_Pivot, [], []).
rightFromPivot(Pivot, [H|T], U) :- H =< Pivot, rightFromPivot(Pivot, T, U).
rightFromPivot(Pivot, [H|T], [H|U]) :- H > Pivot, rightFromPivot(Pivot, T, U).
?- mysort([2,5,1,3,6,4],R).
R = [1, 2, 3, 4, 5, 6].
?-
Alles anzeigen