Haskell版冒泡排序

面试的时候问的比较多的算法题要么是binary search,要么是冒泡排序,真的不想用写C写冒泡排序了,贴上个Haskell版的,思维简单,代码简单,下次谁要是再要我用C写冒泡排序,直接上个haskell版的,让他自己去理解吧。

 

 

sort [] = []
sort [x] = [x]
sort (x:x1:xs)
    | x>x1 = x1:sort (x:xs)
    | otherwise = x:sort (x1:xs)
mpsort [] = []
mpsort x = let tmp = sort x in
           mpsort (init tmp) ++ [(last tmp)]

 运行结果:

 

*Main> mpsort [0,1000,-1,5,4,10,10,1,1,2,3]
[-1,0,1,1,2,3,4,5,10,10,1000]

你可能感兴趣的:(冒泡排序,haskell)