Saturday, November 11, 2006

quick sort style sorting using prolog

program prolog buat sorting pakai gaya quick sort

-----start source----
div(_, [], [], []).
div(Pivot, [D|Data], Left, Right):- D=
div(Pivot, Data, Left2,Right),
append([D|Left2],[], Left).

div(Pivot, [D|Data], Left, Right):- D>Pivot,
div(Pivot, Data, Left,Right2),
append([D|Right2], [], Right).


sorting([],[]).
sorting([X1|X],Y) :- div(X1,X,Kiri,Kanan),
sorting(Kiri,Kiri2),
sorting(Kanan,Kanan2),
append(Kiri2,[X1|Kanan2],Y).
-------end source-----

bikin program pakai prolog bikin pusing tapi bikin penasaran juga

oh iya

cara jalanin programnya yaitu

?- sorting([2,3,1,2,3,4], X).

maka akan keluar jawabannya

X = [1, 1, 2, 4, 4, 6]

yes

~orangIseng

3 comments:

  1. Kesini dong :
    http://cs.hofstra.edu/~cscccl/csc111/qsort.asm

    Quicksort pake assembly !
    XD

    ReplyDelete
  2. halo brigith
    boleh tau brigit pakai prolog apa? swi? atau yg lain

    ReplyDelete