Guest User

Untitled

a guest
Oct 19th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. {-# LANGUAGE UnicodeSyntax #-}
  2. module Inversions (inversions) where
  3.  
  4. inversions ∷ Ord α ⇒ [α] → Int
  5. inversions = fst . inversions'
  6. where
  7. inversions' list@(_:_:_) = (leftInvs + rightInvs + splitInvs, sortedList)
  8. where (leftInvs, leftSortedList) = inversions' leftList
  9. (rightInvs, rightSortedList) = inversions' rightList
  10. (splitInvs, sortedList) = mergeInvs leftSortedList rightSortedList
  11. (leftList,rightList) = splitAt ((`div` 2) $ length list) list
  12. inversions' list = (0, list)
  13.  
  14. mergeInvs ∷ Ord α ⇒ [α] → [α] → (Int,[α])
  15. mergeInvs = mergeInvs' 0
  16. where mergeInvs' i a [] = (i, a)
  17. mergeInvs' i [] a = (i, a)
  18. mergeInvs' i (x:xs) ylist@(y:_) | x < y = (i + invs, x : invList)
  19. where (invs, invList) = mergeInvs' 0 xs ylist
  20. mergeInvs' i xlist (y:ys) = (i + invs + length xlist, y : invList)
  21. where (invs, invList) = mergeInvs' 0 xlist ys
Add Comment
Please, Sign In to add comment