Advertisement
logicmoo

Untitled

Aug 23rd, 2018
586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 1.49 KB | None | 0 0
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. % PROGRAM A
  3. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4.  
  5. perm([], []).
  6. perm(List, [First|Perm]) :-
  7.    select(First, List, Rest),
  8.    perm(Rest, Perm).
  9.  
  10. select(X, [X|Tail], Tail).
  11.   select(Elem, [Head|Tail], [Head|Rest]) :-
  12.   select(Elem, Tail, Rest).
  13.    
  14. is_sorted([]).
  15. is_sorted([_]).
  16. is_sorted([X,Y|T]):-X=<Y,is_sorted([Y|T]).
  17.  
  18. % 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.
  19. naive_sort(List,Sorted):-perm(List,Sorted),is_sorted(Sorted).
  20.  
  21.  
  22.  
  23.  
  24. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  25. % PROGRAM B
  26. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  27.  
  28. quick_sort2(List,Sorted):-q_sort(List,[],Sorted).
  29. q_sort([],Acc,Acc).
  30. q_sort([H|T],Acc,Sorted):-
  31.     pivoting(H,T,L1,L2),
  32.     q_sort(L1,Acc,Sorted1),q_sort(L2,[H|Sorted1],Sorted)
  33.  
  34.  
  35. pivoting(H,[],[],[]).
  36. pivoting(H,[X|T],[X|L],G):-X=<H,pivoting(H,T,L,G).
  37. pivoting(H,[X|T],L,[X|G]):-X>H,pivoting(H,T,L,G).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement