美文网首页函数式编程Haskell
[Haskell] 一些常见的排序算法

[Haskell] 一些常见的排序算法

作者: 何幻 | 来源:发表于2017-03-16 14:28 被阅读87次

1. 排序算法

(1)选择排序

-- selection sort
ssort :: (Ord a) => [a] -> [a]
ssort [] = []
ssort xs = m:(ssort $ delete m xs)
  where m = minimum xs
        delete = delete' []

        delete' :: (Eq a) => [a] -> a -> [a] -> [a]
        delete' _ v [] = []
        delete' acc v (x:xs) | v == x = acc ++ xs 
                             | otherwise = delete' (acc ++ [x]) v xs

(2)插入排序

-- insert sort
isort :: (Ord a) => [a] -> [a]
isort [] = []
isort (x:xs) = insert x $ isort xs
  where insert :: (Ord a) => a -> [a] -> [a]
        insert x xs = takeWhile (<= x) xs
          ++ [x]
          ++ dropWhile (<= x) xs

(3)快速排序

-- quick sort
qsort :: (Ord a) => [a] -> [a]
qsort [] = []
qsort (x:xs) = qsort [i | i <- xs, i <= x]
  ++ [x]
  ++ qsort [i | i <- xs, i > x]

(4)归并排序

-- merge sort
msort :: (Ord a) => [a] -> [a]
msort [] = []
msort [x] = [x]
msort xs = merge (msort xs1) (msort xs2)
  where k = (length xs) `div` 2
        xs1 = take k xs
        xs2 = drop k xs

        merge :: (Ord a) => [a] -> [a] -> [a]
        merge [] b = b 
        merge a [] = a
        merge a@(x:xs) b@(y:ys) | x <= y    = x:(merge xs b)
                                | otherwise = y:(merge a ys)

(5)堆排序

-- priority queue
module PQueue(PQueue,emptyPQ,pqEmpty,enPQ,dePQ,frontPQ) where
  
newtype PQueue a = PQ [a]
  deriving Show
  
emptyPQ :: PQueue a 
emptyPQ = PQ []

pqEmpty :: PQueue a -> Bool
pqEmpty (PQ []) = True 
pqEmpty _ = False

enPQ :: (Ord a) => a -> PQueue a -> PQueue a 
enPQ x (PQ q) = PQ (insert x q)
  where insert :: (Ord a) => a -> [a] -> [a]
        insert x [] = [x]
        insert x r@(y:ys) | x <= y    = x:r
                          | otherwise = y:insert x ys

dePQ :: (Ord a) => PQueue a -> PQueue a 
dePQ (PQ []) = error "dePQ: empty priority queue"
dePQ (PQ (x:xs)) = PQ xs

frontPQ :: (Ord a) => PQueue a -> a 
frontPQ (PQ []) = error "frontPQ: empty priority queue"
frontPQ (PQ (x:xs)) = x
-- heap sort
import PQueue

mkPQ :: (Ord a) => [a] -> PQueue a
mkPQ xs = foldr enPQ emptyPQ xs

hsort :: (Ord a) => [a] -> [a]
hsort xs = hsort' (mkPQ xs)
  where hsort' :: (Ord a) => PQueue a -> [a]
        hsort' pq | pqEmpty pq = [] 
                  | otherwise  = (frontPQ pq):(hsort' $ dePQ pq)

(6)树排序

-- binary search tree 
{-# LANGUAGE GADTs #-}

module BinTree(BinTree, inTree, addTree, delTree, buildTree, preorder, inorder, postorder) where
  
data BinTree a where
  EmptyBT :: (Ord a) => BinTree a
  NodeBT:: (Ord a) => a -> BinTree a -> BinTree a -> BinTree a
  
inTree :: (Ord a, Show a) => a -> BinTree a -> Bool
inTree v EmptyBT = False 
inTree v (NodeBT v' lf rt) | v == v' = True
                           | v < v' = inTree v lf
                           | v > v' = inTree v rt

addTree :: (Ord a, Show a) => a -> BinTree a -> BinTree a
addTree v EmptyBT = NodeBT v EmptyBT EmptyBT
addTree v (NodeBT v' lf rt) | v == v'  = NodeBT v' lf rt
                            | v < v'   = NodeBT v' (addTree v lf) rt
                            | otherwise = NodeBT v' lf (addTree v rt)

delTree :: (Ord a, Show a) => a -> BinTree a -> BinTree a
delTree v EmptyBT = EmptyBT
delTree v (NodeBT v' lf EmptyBT) | v == v' = lf 
delTree v (NodeBT v' EmptyBT rt) | v == v' = rt
delTree v (NodeBT v' lf rt) | v < v' = NodeBT v' (delTree v lf) rt 
                            | v > v' = NodeBT v' lf (delTree v rt)
                            | v == v' = NodeBT k lf (delTree k rt)
                              where k = minTree rt
                              
                                    minTree :: BinTree a -> a 
                                    minTree (NodeBT v EmptyBT _) = v 
                                    minTree (NodeBT _ lf _) = minTree lf

buildTree :: (Ord a, Show a) => [a] -> BinTree a
buildTree lf = foldr addTree EmptyBT lf 

preorder :: (Ord a, Show a) => BinTree a -> [a]
preorder EmptyBT = []
preorder (NodeBT v lf rt) = [v] ++ preorder lf ++ preorder rt

inorder :: (Ord a, Show a) => BinTree a -> [a]
inorder EmptyBT = []
inorder (NodeBT v lf rt) = inorder lf ++ [v] ++ inorder rt

postorder :: (Ord a, Show a) => BinTree a -> [a]
postorder EmptyBT = []
postorder (NodeBT v lf rt) = postorder lf ++ postorder rt ++ [v]

import BinTree

-- tree sort 
tsort :: (Ord a) => [a] -> [a]
tsort xs = inorder . buildTree $ xs 

2. 时间复杂度与空间复杂度


参考

Algorithms: A Functional Programming Approach

相关文章

  • [Haskell] 一些常见的排序算法

    1. 排序算法 (1)选择排序 (2)插入排序 (3)快速排序 (4)归并排序 (5)堆排序 (6)树排序 2. ...

  • LeetCode大全

    1.常见排序算法: 常见的排序算法:冒泡排序、选择排序、插入排序、归并排序、快速排序、希尔排序、堆排序、计数排序、...

  • 数据结构与算法

    常见排序算法 堆排序 算法大全 算法大汇总

  • 排序算法

    常见的排序算法 常见的排序算法有:插入、希尔、选择、冒泡、归并、快速、堆排序。。。 插入排序 算法步骤一、从数组的...

  • Python知识点:常见算法的python实现

    提到排序算法,常见的有如下几种:冒泡排序、选择排序、插入排序、快速排序、堆排序、归并排序、希尔排序;查找算法最常见...

  • 常见排序算法及JAVA实现

    排序算法的分类 先看维基百科中的一张关于排序算法的表 我们主要了解常见的一些排序算法。像Bogo排序,臭皮匠排序这...

  • Rust数据结构——排序算法(一)

    Rust数据结构——排序算法(一) 0x01 常见的排序算法 排序算法是数据结构中很常见的算法。如果你了解过数据结...

  • 排序算法(四) 希尔排序(插入排序的进化)

    参考Java排序算法(四):希尔排序常见排序算法 - 希尔排序 (Shell Sort) 希尔排序算法是按其设计者...

  • 排序算法

    排序算法 排序是最基本的算法之一,常见的排序算法有插入排序、希尔排序、选择排序、冒泡排序、堆排序、归并排序及快速排...

  • IOS常见算法

    常见算法: 快速排序: 选择排序: 冒泡排序: 测试代码:

网友评论

    本文标题:[Haskell] 一些常见的排序算法

    本文链接:https://www.haomeiwen.com/subject/fgqjnttx.html