Wednesday 15 June 2011

Prolog difference list - mergesort -


I must write a predicate that will divide a list into two lists (on half):

  Partial (XY, XZ, ZY): - halve_pom (XY, XY, Z),! Halve_pom (Z-Y, Y-Y, Z) Halve_pom ([_ | A] -Y, [_, _ | B] -Y, Z): - halve_pom (A-Y, B-Y, Z).   

It was easy, but now I have to type the algorithm which will merge - I have no idea. This algorithm should use the difference list.

Please help. No, it was not "easy" because it does not work, unfortunately

Halva ([1,2,3] - [], A, B) does not work; halve_pom ([1,2,3] - [], A, B) does not work either. Apart from this, it is not clear what splashing schemes you like, [1,2,3,4,5,6,7] - & gt; ([1,3,5,7], [2,4,6]) or - & gt; ([1,2,3,4], [5,6,7]) .

If your half works in the works, then you have to define merge to do whatever you have left, which are two parts of the list Mergeresort (A, S): - Halva (A, B - [], C- []), MergeRort (B, SB), Merger (C, SC), Merge (SB, Scheduled Castes, S []).

Note that you might call it with the normal list A , that is, half predicate your first argument to a general list (I.e. not the difference list).

Also see. '-' is unnecessary; Instead, two components of it should be used as two different arguments of an ingenious.


Therefore, a method of code is halve

  half ([A, B | C], [A | X] -JX, [BY] - Jiayu): - Half (C, X-ZX, Wi-YE). Half ([One], [X | X] -X, Y-Y). Half ([], X-X, Y-Y).   

The same approach can be used for code merge :

  merge (SB, SC, SZ): - SB = [A | B], SC = [C | D], A = & lt; C, S = [A | T], merge (B, SC, TZ). Merge (SB, SC, S-Z): - SB = [A | B], SC = [C | D], A & gt; C, ..., .... Merge (SB, SC, S-Z): - SB = [A | B], SC = [], ..., .... Merge (SB, SC, S-Z): - SB = [], SC = [C | D], S = [C | T], .... Merge ([], [], Z-Z).    

No comments:

Post a Comment