Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,20 @@
|
|||
%___________________________________________________________________________
|
||||
% Bubble sort
|
||||
|
||||
bubble(0, Res, Res, sorted).
|
||||
bubble(Len, [A,B|T], Res, unsorted) :- A > B, !, bubble(Len,[B,A|T], Res, _).
|
||||
bubble(Len, [A|T], [A|Ts], Ch) :- L is Len-1, bubble(L, T, Ts, Ch).
|
||||
|
||||
bubblesort(In, Out) :- length(In, Len), bubblesort(Len, In, Out).
|
||||
bubblesort(0, In, In).
|
||||
bubblesort(Len, In, Out) :-
|
||||
bubble(Len, In, Bubbled, SortFlag), % bubble the list
|
||||
(SortFlag=sorted -> Out=Bubbled; % list is already sorted
|
||||
SegLen is Len - 1, % one fewer to process
|
||||
writef('bubbled=%w\n', [Bubbled]), % show progress
|
||||
bubblesort(SegLen, Bubbled, Out)).
|
||||
|
||||
test :- In = [8,9,1,3,4,2,6,5,4],
|
||||
writef(' input=%w\n', [In]),
|
||||
bubblesort(In, R),
|
||||
writef('-> %w\n', [R]).
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
:- initialization(main).
|
||||
|
||||
|
||||
bubble_sort(Xs,Res) :-
|
||||
write(Xs), nl
|
||||
, bubble_pass(Xs,Ys,Changed)
|
||||
, ( Changed == true -> bubble_sort(Ys,Res) ; Res = Xs )
|
||||
.
|
||||
|
||||
bubble_pass(Xs,Res,Changed) :-
|
||||
Xs = [X|Ys], Ys = [Y|Zs]
|
||||
, ( X > Y -> H = Y, T = [X|Zs], Changed = true
|
||||
; H = X, T = Ys
|
||||
)
|
||||
, Res = [H|R], !, bubble_pass(T,R,Changed)
|
||||
; Res = Xs
|
||||
.
|
||||
|
||||
|
||||
test([8,9,1,3,4,2,6,5,4]).
|
||||
|
||||
main :- test(T), bubble_sort(T,_), halt.
|
||||
Loading…
Add table
Add a link
Reference in a new issue