, . , : gt{ , Y)
, Y, , "". , , , : gt (X, Y): - >.
, gt , , :
gtl X, Y):- @>¥,
, . ,
sort! List, Sorted)
, List , a Sorted , -
gt. Prolog, . .
List :
■ List , X Y, , gt { X, ] , Y List, Listl; Listl;
■ List , X Y, , gt (X, Y) , List .
, X Y, , , . , . (bubble sort). Prolog, , bubblesort.
bubblesort (List, Sorted);-
swap(List, Listl),!, i List?
bubblesort (Listl, Sorted).
bubblesort (Sorted, sorted). %
sap([X, Y | Rest], [y, X | Rest]):- *
gt(X, X).
swapf [Z I Rest], [Z 1 Restl]):- %
swap; Rest, Restl).
, .
, L. [X | ], .
1. L.
2. X L , , . .
Prolog insert sort:
|
|
insertsort ([],>-
insertsort! [X I Tail], Suited):-
insertsort (Tail, SortedTa.il), %
insert! X, SortedTail, Sorted).
insert t x, [Y! Sorted], [Y; Sortedl]):-
gt< x, Y),!,
insertf X, Sorted, Sortedl). insert! x, Sorted, [X I Sorted]).
.
bubblesorr insertsort , . (insertsort) . , inserzsort, /. quicksort. , . 9.1.
[5,3,7,8,1,4,7,61
i
( = 51
[3,7,,1,4,7,6]
,'1!>(>;'-! L |
£ 5
[3,1,4]
[1,3,4]
I
i :n nmiMOHTbi | >5 |
[7,8,7,6] | |
1 | |
[ 6.7,7,8] |
$
V
1,3,4,5,6,7,7,8
- 9
quicksort
quicksort .
L, .
1. X L L , Small Big, : L, X, Big, Small.
2. Small, SortedSmall.
3. Big, SortedBig.
4.
SortedSmall [X | SortedBig}.
, . quicksort Prolog -
I. Prolog
9.1. , X, L, L. : split; X, L, Small, Big)
, , , . , , log , . , , , , ;. , , , quicksort , .
, 9.1, , . , 8, . , 9.1, A-Z ;
|
|
SortedSmall A1-Z1 SortedBig A2-Z2
9.1. quicksort
?''qaic):sort7List,'soitedListr:........................
% List quicksort quicksort! [], []).
quick s SortedSmall 3, or [X tedSm,Ei3,. Sorted).
split (x, [ ], [ ], [ 1).
Split; X, [YITail], [ Y | Small], Big):-gt(x, Y),!, split! X, Tail, Small, Big).
Split! X, [Y|Tail), Small, [YjBigU:-split! X, Tail, Small, Big;.
, SortedSmall [X | SortedBig] :
1 - Z1 [X | 2] - Z2
, , :
AI - Z2, 1 Z2 - [X | 2]
Z-Z. (. 9.1) quicksort, quicksort2 9.2. quicksort ,
.
quicksort2, . :
quicksort L, S!:-
quicksorts t L, S - I ]),
9.2. q u i c ks o rt ; s p l i t (x, List, small, Big) 9.1
'■■ quicksort (List, SortedList):
List quicksort
quicksort': List, Sorted):-
quickscrt2 (list, Sorted - [)).
\ quicksort2 (List, SortedDiffList): List,- %
quicksort2 (J.l, Z - Z}.
quicksort2([. Tail), 7U - Z2):-split(X, Tail, Small, Big), quicksorts! Small, Al -.:■: A2 ]), quicksort2 I eiq, A2 - Z2).
9.1.
, , :
?- raerqef [2,5,6,6,8], [1,3,5,9], L). L = [1,2,3,3,5,6, "3,6,5]
9.2. , 9.1 9.2, , , . . , 9.1, , 9.2.
9.3. quicksort , . , .
9.4. , quicksort, , , , . , _, :
: , L1 L2, ;
L1 L2, S1 S2;
S1 S2 L.
- . - quicksort.
196 I. Prolog