我想實現氣泡排序按優先級排序我的列表。例如,該列表是第三元素是優先級的格式如下:Haskell Bubble排序在三部分Tuple
[("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
你的函數對整個元組進行排序,所以如果你想對元組的第三個元素進行排序,你需要修改一些東西。順便說一句,不要使用冒泡排序。 – augustss
@augustss我可以在泡泡排序中使用什麼?插入排序?爲什麼我不應該使用冒泡排序? – user2214957
這只是冒泡排序非常低效。 – leftaroundabout