読者です 読者をやめる 読者になる 読者になる

@ゲー単走部

ローグライク雑記。変愚蛮怒、DCSSなど。

マージソート

部分リストのうまい取り方がわからん

merge :: [Int] -> [Int] -> [Int]
merge [] ys = ys
merge xs [] = xs
merge (x:xs) (y:ys)
    | x <= y    = x : merge xs (y:ys)
    | otherwise = y : merge (x:xs) ys

mergesort :: [Int] -> [Int]
mergesort [] = []
mergesort [n] = [n]
mergesort list = merge list1 list2
    where size_center = (length list) `div` 2
          leftlist  = [list !! index | index <- [0..size_center - 1]]
          rightlist = [list !! index | index <- [size_center..(length list) - 1]]
          list1 = mergesort leftlist
          list2 = mergesort rightlist