Re: In which cases/problems is Prolog faster than Java?
- From: Christina <christine.uk2000@xxxxxxxxx>
- Date: Mon, 19 May 2008 13:43:01 -0700 (PDT)
On 19 Mai, 23:05, bart demoen <b...@xxxxxxxxxxxxxx> wrote:
Show us your mergesort please. A reasonable mergesort in SWI-Prolog
(not the fastest around) sorts 30000 elements easily.
% MERGE SORT
/* merge_sort(Xs, Ys) is true if Ys is a sorted permutation of the
list Xs.*/
group::merge_sort(List, SortedList):-
length(List, Length),
merge_sort_1(Length, List, SortedList, []).
/* merge_sort_1(N, Xs, Ys, Zs) is true if Ys is a sorted permutation
of */
/* the first N elements of the list Xs, and Zs are the remaining
elements */
/* of
Xs. */
merge_sort_1(0, Rest, [], Rest):-!.
merge_sort_1(1, [A|Rest], [A], Rest):-!.
merge_sort_1(2, [A,B|Rest], C, Rest):- A =< B, !, C = [A,B].
merge_sort_1(2, [A,B|Rest], C, Rest):- !, C = [B,A].
merge_sort_1(N, List, Sorted, Rest):-
N1 is N // 2, N2 is N - N1,
merge_sort_1(N1, List, SortedLeft, TempList),
merge_sort_1(N2, TempList, SortedRight, Rest),
ordered_merge(SortedLeft, SortedRight, Sorted).
/* ordered_merge(Xs, Ys, Zs) is true if Zs is an ordered list
obtained */
/* from merging the ordered lists Xs and
Ys. */
ordered_merge([X|Xs], [Y|Ys], [Y|Zs]):-
Y =< X, !,
ordered_merge([X|Xs], Ys, Zs).
ordered_merge([X|Xs], Ys, [X|Zs]):-
ordered_merge(Xs, Ys, Zs).
ordered_merge([], Ys, Ys).
/* length(Xs, L) is true if L is the number of elements in the list
Xs. */
length(Xs, L):-length_1(Xs, 0, L).
/* length_1(Xs, L0, L) is true if L is equal to L0 plus the number
of */
/* elements in the list
Xs. */
length_1([], L, L).
length_1([_|Xs], L0, L):- L1 is L0 + 1, length_1(Xs, L1, L).
I am running Trinc-Prolog R3b RC1 Trial version.
I have also installed SWI-Prolog and Logtalk, now I'm working on
converting the Trinc-Prolog
version of merge sort into the corresponding SWI-Prolog.
Does minimax algorithm work faster in Prolog than in Java?
.
- Follow-Ups:
- Re: In which cases/problems is Prolog faster than Java?
- From: Bart Demoen
- Re: In which cases/problems is Prolog faster than Java?
- References:
- In which cases/problems is Prolog faster than Java?
- From: Christina
- Re: In which cases/problems is Prolog faster than Java?
- From: bart demoen
- In which cases/problems is Prolog faster than Java?
- Prev by Date: Re: In which cases/problems is Prolog faster than Java?
- Next by Date: Re: In which cases/problems is Prolog faster than Java?
- Previous by thread: Re: In which cases/problems is Prolog faster than Java?
- Next by thread: Re: In which cases/problems is Prolog faster than Java?
- Index(es):
Relevant Pages
|
|