Prolog: Bubblesort

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Prolog: Bubblesort

    Bubblesort

    Hier sind 3 verschiedene Varianten:

    [code:1]bubblesort(L_In,L_Out):-
    append(L_Front, [A, B|Rest], L_In),
    B < A,!,
    append(L_Front, [B, A|Rest], L_Rest),
    bubblesort(L_Rest, L_Out).
    bubblesort(Liste, Liste).[/code:1]

    [code:1]bubblesort(Xs,Xs):- geordnet(Xs).
    bubblesort(Xs,Ys):-
    append(As,[X,Y|Rs],Xs),
    X>Y,
    append(As,[Y,X|Rs],Xs1),
    bubblesort(Xs1,Ys).[/code:1]

    [code:1]bubble_sort(List,Sorted):-bubblesort(List,[],Sorted).
    bubblesort([],Acc,Acc).
    bubblesort([H|T],Acc,Sorted):-bubble(H,T,NT,Max),bubblesort(NT,[Max|Acc],Sorted).

    bubble(X,[],[],X).
    bubble(X,[Y|T],[Y|NT],Max):-X>Y,bubble(X,T,NT,Max).
    bubble(X,[Y|T],[X|NT],Max):-X=<Y,bubble(Y,T,NT,Max).[/code:1]