What is wrong with this Haskell quicksort -
taking functional programming plunge , trying teach myself haskell online materials. pretty basic, cannot figure why implementation of quicksort not terminate input list length longer 1.
here's code:
quicksort :: (ord ord) => [ord] -> [ord quicksort [] = [] quicksort [element] = [element] quicksort array = quicksort right_half ++ [pivot] ++ quicksort left_half   pivot = head array         right_half = [element | element <- array, element <= pivot]         left_half = [element | element <- array, element > pivot] i reading online textbook decided try quicksort myself before reading given solution. after failing, went , understood given answer, still cannot see why mine wrong. haskell newbie appreciated!
notice sort of algorithm not going yield quick sort.  time complexity of (++) destroy you, if nothing else.
as problem, taking elements <= pivot.. (absolutely) include pivot.  if have list [2,1] sort right half call quicksort [2,1], creating loop.
a better solution pattern match, using like:
quicksort (pivot:array) = quicksort righthalf ++ [pivot] ++ quicksort lefthalf edit: see related why minimalist, example haskell quicksort not "true" quicksort?
Comments
Post a Comment