Quicksort in SML using foldr -


i'm working on assignment partition function of quicksort in sml may done foldr, , no library functions may used. i've gotten partitioning down fine, , have fundamentals of quicksort fine, running problems seems merge wrong lists together/merge in wrong order.

(*comp placeholder, given comparator function , list input*) fun comp b = > b;  (*both partition functions working intended.*) fun getgreater (a::b) c = foldr (fn (a, lst) => if (comp c) a::lst else lst) [] (a::b); fun getlesserorequal (a::b) c = foldr (fn (a, lst) => if not (comp c) a::lst else lst) [] (a::b);  fun merge (a::b) (c::d) = foldr (op::) (c::d) (a::b);  fun triplemerge (a::b) c (d::e) = merge (a::b) (c::(d::e));  (*removes head, uses head pivot. recursive calls on results on getlesserorequal , getgreater*) fun sort [] = [] | sort (a::b) = if ([a] = (a::b)) [a] else     triplemerge (sort(getlesserorequal b a)) (sort(getgreater b a)); 

as example, here of tests i'm running. when follow logic on paper, don't same answers incorrect items:

sort [2, 6, 3, 6, 4, 100, 0]; val = [0,2,3,6,4,6,100] : int list (*incorrect*)  sort [1, 2, 3, 4, 5]; val = [1,2,3,4,5] : int list  sort [5, 4, 3, 2, 1]; val = [5,4,3,2,1] : int list (*incorrect*)  sort [1, 100, 10, 1000, 0]; val = [0,1,10,100,1000] : int list  sort [1, 2, 1, 2, 1, 2, 5, 1]; val = [1,1,1,1,2,2,2,5] : int list 

is mistake obvious anyone?

the mistake see in merge , triplemerge functions. using patterns a::b disallowing empty lists. -- when pivot either smallest or greatest element, 1 of partitioning functions returning empty lists. if tweak code of 2 functions this:

fun merge xs ys = folder (op::) ys xs;  fun triplemerge xs y ys = merge xs (y::ys); 

and leave rest of code alone, work expected.


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -