Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % PROGRAM A
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- perm([], []).
- perm(List, [First|Perm]) :-
- select(First, List, Rest),
- perm(Rest, Perm).
- select(X, [X|Tail], Tail).
- select(Elem, [Head|Tail], [Head|Rest]) :-
- select(Elem, Tail, Rest).
- is_sorted([]).
- is_sorted([_]).
- is_sorted([X,Y|T]):-X=<Y,is_sorted([Y|T]).
- % Naive sort uses the generate and test approach to solving problems which is usually utilized in case when everything else failed. However, sort is not such case.
- naive_sort(List,Sorted):-perm(List,Sorted),is_sorted(Sorted).
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % PROGRAM B
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- quick_sort2(List,Sorted):-q_sort(List,[],Sorted).
- q_sort([],Acc,Acc).
- q_sort([H|T],Acc,Sorted):-
- pivoting(H,T,L1,L2),
- q_sort(L1,Acc,Sorted1),q_sort(L2,[H|Sorted1],Sorted)
- pivoting(H,[],[],[]).
- pivoting(H,[X|T],[X|L],G):-X=<H,pivoting(H,T,L,G).
- pivoting(H,[X|T],L,[X|G]):-X>H,pivoting(H,T,L,G).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement