sorting - Prolog Bubblesort -
i'm trying best understand code. how perceive what's happening:
so can see if x > y
swap elements, if not recurse down sublist until find x
x > y
, if not, list sorted. problems i'm having don't understand base case, bubblesort(sorted, sorted)
. thought need base case empty list? appreciate if describe sort of step step description of program.
bubblesort(list,sorted) :- swap(list,list1), !, bubblesort(list1,sorted). bubblesort(sorted,sorted). swap([x,y|rest],[y,x|rest]) :- % swaps x y if gt(x,y) true. gt(x,y). swap([z|rest],[z|rest1]) :- % calls swap on sublists excluding heads. swap(rest,rest1). gt(x,y) :- % true if x greater y. x > y.
Comments
Post a Comment