Advertisement
Guest User

Untitled

a guest
Oct 7th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 1.55 KB | None | 0 0
  1. % =========================================================================
  2. % FILE     : quicksort.pl
  3. % SUPPORT  : Bapst Frederic, HEIA-FR.
  4. % CONTEXT  : Techniques Avancées de Programmation 3 : Programmation Logique
  5. % =========================================================================
  6. % OVERVIEW : Quicksort with lists in Prolog
  7. % =========================================================================
  8.  
  9.  
  10. % ------------------------------------------------------------
  11. %--- partition(+P, +Ls, ?Ss, ?Gs) : Ss and Gs gives a correct partition
  12. %                                   of Ls with Ss <= P, Gs > P
  13.  
  14. % Pseudo-code : mettre le 1er élt de Ls dans Ss ou Gs, et relancer
  15.  
  16. % A COMPLETER
  17.  
  18. % ------------------------------------------------------------
  19. %--- quickSort(+Ls, ?Ss) : Ss is a sorted version of Ls
  20.  
  21. % Pseudo-code : prendre comme pivot P le 1er élt de Ls,
  22. %               partitionner le reste en As, Bs
  23. %               trier les 2 parties AsSorted, BsSorted
  24. %               concaténer AsSorted, P, BsSorted
  25.  
  26. % A COMPLETER
  27.  
  28. qsorttest :-
  29.     Ls=[4,5,3,6,8,2],
  30.     write('   Input list: '), write(Ls), nl,
  31.     quickSort(Ls, Res),
  32.     write('Sorted result: '), write(Res).
  33.  
  34. partition(_, [], [], []).
  35.  
  36. partition(P,[L|Ls],[L|Ss],Gs) :-
  37.     P >= L,
  38.     partition(P, Ls, Ss,Gs).
  39.    
  40. partition(P,[L|Ls],Ss,[L|Gs]) :-
  41.     P < L,
  42.     partition(P, Ls, Ss, Gs).
  43.  
  44. quickSort([], []).
  45.  
  46. quickSort([P|Ls], Ss) :-
  47.     partition(P, Ls, As, Bs),
  48.     quickSort(As, AsSorted),
  49.     quickSort(Bs, BsSorted),
  50.     append(AsSorted, [P], AsSortedP),
  51.     append(AsSortedP, BsSorted, Ss).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement