1

我正在尝试实现冒泡排序来按优先级对列表进行排序。例如,列表具有以下格式,第 3 个元素为优先级:

  [("Ted", 100, 3), ("Boris", 100, 1), ("Sam", 100, 2)]

我已经尝试了下面的标准冒泡排序方法,但是这不起作用。任何建议,将不胜感激。

bubbleSort :: (Ord t) => [t] -> [t]
bubbleSort a = loop (length a) bubble a

bubble :: (Ord t) => [t] -> [t] 
bubble (a:b:c) | a < b = a : bubble (b:c)
           | otherwise = b : bubble (a:c)
bubble (a:[]) = [a] 
bubble [] = []

loop :: (Num a, Ord a) => a -> (t -> t) -> t -> t
loop num f x | num > 0 =  loop (num-1) f x'
         | otherwise = x
         where x' = f x
4

1 回答 1

5

正如 luqui 所暗示的,通常不Ord直接实现排序算法的约束版本,而是使用自定义比较的更通用的版本:

bubbleSortBy :: (t->t->Ordering) -> [t] -> [t]
bubbleSortBy cmp a = loop (length a) bubble a
 where
       bubble :: (Ord t) => [t] -> [t] 
       bubble (a:b:c) = case cmp a b of
                          LT -> a : bubble (b:c)
                          _  -> b : bubble (a:c)
       bubble l = l

loop :: Integral a    -- (Num, Ord) is a bit /too/ general here, though it works.
   => a -> (t -> t) -> t -> t
loop num f x | num > 0    = loop (num-1) f x'
             | otherwise  = x
         where x' = f x

版本从这里Ord微不足道:

bubbleSort :: (Ord t) -> [t] -> [t]
bubbleSort = bubbleSortBy compare

但通常直接使用通用版本更实用,就像你的情况一样

import Data.Function(on)

sorted_priority3rd = bubbleSortBy ( compare `on` \(a,b,c) -> (c,a,b) )

它的作用是在每次比较之前更改参数的顺序。显然,这会使冒泡排序变得更慢;通常你宁愿做

import Data.List (sortBy)   -- this is a proper ( log ) sorting algorithm

sorted_by3rd = sortBy ( compare `on` \(a,b,c) -> c )

并在以后关心更精细的订单。

于 2013-03-28T16:20:50.710 回答