Różnice
Różnice między wybraną wersją a wersją aktualną.
|
|
pl:prolog:pllib:insert_sort [2019/06/27 15:50] |
pl:prolog:pllib:insert_sort [2019/06/27 15:50] (aktualna) |
| ====== Insert sort ====== |
| {{tag>sorting algorithms recursion}} |
| ===== Description ===== |
| Prolog implementation of insert sort based on idea of accumulator. |
| |
| **Source**: Guide to Prolog Programming (on-line tutorial) |
| ===== Download ===== |
| Program source code: {{insert_sort.pl}} |
| ===== Listing ===== |
| <code prolog> |
| insert_sort(List,Sorted):-i_sort(List,[],Sorted). |
| i_sort([],Acc,Acc). |
| i_sort([H|T],Acc,Sorted):-insert(H,Acc,NAcc),i_sort(T,NAcc,Sorted). |
| |
| insert(X,[Y|T],[Y|NT]):-X>Y,insert(X,T,NT). |
| insert(X,[Y|T],[X,Y|T]):-X=<Y. |
| insert(X,[],[X]).</code> |
| ===== Comments ===== |
| |