algorithms - inversion count using haskell
TRANSCRIPT
-
7/27/2019 Algorithms - Inversion Count using Haskell
1/6
Mbchrotajs * Ihu`to`c O`vgrsoh`s
uso`c Amsegbb
To=?>4> Luby 94" ;4=9
-
7/27/2019 Algorithms - Inversion Count using Haskell
2/6
Ih`tg`ts
Ih`tg`ts
M b c h r o t a j s * I h u ` t o ` c O ` v g r s o h ` s u s o ` c A m s e g b b 9
4.= O`trhnuitoh. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9 4.; O`vgrsoh. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9
4.;.= Ojpbgjg`tmtoh. . . . . . . . . . . . . . . . . . . . . . . .9 4.;.; Ihng . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6
[gfgrg`igs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6
; | 6
-
7/27/2019 Algorithms - Inversion Count using Haskell
3/6
Mbchrotajs * Ihu`to`c O`vgrsoh`s uso`c Amsegbb
Mbchrotajs * Ihu`to`c O`vgrsoh`suso`c Amsegbb
4.= O`trhnuitoh`O` taos mrtoibg wg wobb bhhe mt Novong m`n ih`qugr mpprhmia usojgrcg shrt mbchrotaj th ihu`t tag `ujkgr hf o`vgrsoh` o` m` mrrmy
4.; O`vgrsoh` ^ag ihu`t hf o`vgrsoh` o` m` mrrmy os tag ihu`t hf m U o Z 0 m U l Z fhr
o 0 l .^aos os kmsoiombby tag `ujkgr hf ihjpmrosoh`s rgquorgn th mrrmcg tag gbgjg`ts o` shrtgn hrngr.
^ag `ujkgr hf o`vgrsoh` fhr shrtgn mrrmy os 4 waobg fhr m rg*vgrsg mrrmy os` ` =
; "jmxojuj `ujkgr hf o`vgrsoh`s tamt im` kg
g`ihu`tgrgn fhr u`shrtgn mrrmy. ^ag `movg wmy hf ihu`to`c ovgrsoh` whubn kg th ihu`t mbb o`stm`ig hf m U o Z 0 m U l Z fhr o 0 l "waoia osH ! @ ; / .
^ag jgrcg shrt im` kg usgn th ihu`t o`vgrsoh`.
4.;.= Ojpbgjg`tmtoh`O` tag jgrcg shrt mbchrotaj "o` tag jgrcg stgp wg iagie o`stm`igx 0 y
hf tag forst gbgjg`t hf mrrmy M wota tag forst gbgjg`t hf mrrmy K.M`n of x 0 y "ih`notoh` whubn rgquorg o`vgrsoh`.
^aus o` tag jgrcg stgp wg ihu`t mbb tag o`stm`igs hf x 0 y ih`notoh` os stmtosfogn. ^aos mppbogs th imsg wag` M m`n K mrg u`shrtgn mrrmys.
O` tag prgsg`t ojpbgjg`tmtoh` wg amvg usgn m khhbgm` fbmc tho`fhrj tag jgrcg stgp of tag o`put mrrmys mrg shrtgn hr u`shrtgn.
9 | 6
-
7/27/2019 Algorithms - Inversion Count using Haskell
4/6
Mbchrotajs * Ihu`to`c O`vgrsoh`s uso`c Amsegbb
@hw of khta M m`n K mrg shrtgn m`n o`vgrsoh` o` g`ihu`tgrgnM ! = / 0 K ! = / "tag` thtmb `ujkgr hf o`vgrsoh` g`ihu`tgrgn o` tag u`oh` hf M"K im` kg gxprgssgn ms bg`cta!M/.
Fhr gxmjpbgsM : U 9 5 7 Z m`n K : U = 5 ; Z " tag `ujkgr hf o`vgrsoh` rgquorgn wrt gbgjg`t! 9 5 = / os ="taus m`y gbgjg`t crgmtgr tam` 9o` shrtgn mrrmy M wobb mbsh rgquorg tag smjg `ujkgr hf o`vgr*soh`s.^aus thtmb `ujkgr hf o`vgrsoh`s os ihu`tgn ms ; fhr pmors! 9 5 = / 5 ! 7 5 = / .Yojobmroby o` tag `gxt stgp fhr tag mrrmyM : U 9 5 7 Z m`nK : U ; Z tag thtmb `ujkgr hf o`vgrsoh`s mrg ; fhr pmors! 9 5 ; / 5 ! 7 5 ; / .^auso` cg`grmb tag `ujkgr hf o`vgrsoh` g`ihutgrgn mt gmia stgp mrgb g ` c t a ! M / .
^ag ojpbgjg`tmtoh` hf mbchrotaj o` Amsegb os prhvongn.* * s p b o t
* * s p b o t m ` m r r m y h f s o z g @ o ` t h t w h m r r m y s h f s o z g @ ( ;
* * t a g i h j p u t m t o h ` h f t h t m b b g ` c t a h f t a g b o s t o s r g q u o r g n
s p b o t < < H r n m : 0 U m Z * 0 O ` t * 0 ! U m Z " U m Z " O ` t /
s p b o t s o : ! b = " b ; " ! o % = / /
w a g r g
! b = " b ; / : s p b o t M t ! n o v ! b g ` c t a ! s / / ; / s
* * j g r c g s h r t
* * t a g f u ` i t o h ` t h j g r c g t a g ; s h r t g n b o s t s
* * o p u t o s t a g b o s t s t h k g s h r t g n m ` n i u r r g ` t ` u j k g r h f o ` v g r s o h ` s
* * h u t p u t o s j g r c g r b o s t % t h t m b ` u j k g r h f i h j p u t m t o h ` r g q u o r g n f h r j g r c g
* * f b m c o s u s g n t h o ` n o i m t g o f m r r m y o s s h r t g n h r u ` s h r t g n
* * u s g n f h r o ` v g r s o h ` i h u ` t
j g r c g = < < H r n m : 0 U m Z * 0 U m Z * 0 O ` t * 0 K h h b * 0 ! U m Z " O ` t /
j g r c g = U Z x s o X : ! x s " o /
j g r c g = x s U Z o X : ! x s " o /
j g r c g = ! x < x s / ! y < y s / o f b m c
| x 2 y : ! x < m = " m ; /
| h t a g r w o s g : i m s g f b m c h f
t | t : : F m b s g * 0 ! y < k = " k ; /
| h t a g r w o s g * 0 ! y < k = " k = /
w a g r g
! m = " m ; / : j g r c g = x s ! y < y s / ! o / f b m c
! k = " k ; / : j g r c g = ! x < x s / y s ! o = / f b m c
o = : i m s g f b m c h f
* * o ` v g r s o h ` h f s o ` c b g g b g j g ` t i h ` s o n g r g n f h r u ` s h r t g n m r r m y
t | t : : F m b s g * 0 ! o % = /
* * o ` v g r s o h ` h f m b b g b g j g ` t s 0 x i h ` s o n g r g n o s M " K s h r t g n m r r m y
| h t a g r w o s g * 0 o % b g ` c t a ! x < x s /
> | 6
-
7/27/2019 Algorithms - Inversion Count using Haskell
5/6
Mbchrotajs * Ihu`to`c O`vgrsoh`s uso`c Amsegbb
* * t a g r g i r s o v g f h r j g r c g s h r t m b c h r o t a j
* * o ` p u t o s b o s t " h u t p u t o s j g r c g s h r t g n b o s t m ` n ` u j k g r h f o ` v g r s o h ` s
j g r c g X s h r t # < < H r n m : 0 U m Z * 0 O ` t * 0 ! U m Z " O ` t /
j g r c g X s h r t # b o s t o
| b g ` c t a ! b o s t / : : = : ! b o s t " o /
| h t a g r w o s g : ! h = " h ; /
w a g r g
! b o s t = " b o s t ; " j 7 / : ! s p b o t b o s t 4 /
! b o s t 9 " j 9 / : j g r c g X s h r t # b o s t = ! 4 /
! b o s t > " j > / : j g r c g X s h r t # b o s t ; ! 4 /
* * i m b b j g r c g w o t a t r u g f b m c f h r o ` v g r s o h ` i h u ` t
! h = " h ; / : j g r c g = b o s t 9 b o s t > ! j 9 % j > / ^ r u g
* * j m o ` f u ` i t o h ` t h k g i m b b g n f h r j g r c g s h r t m b c h r o t j
j g r c g X s h r t < < H r n m : 0 U m Z * 0 ! U m Z " O ` t /
j g r c g X s h r t s : j g r c g X s h r t # s 4
* * f u ` i t o h ` t h s p b o t m r r m y " o f o o s p h s o t o v g p h s o t o h ` f r h j
* * s t m r t h f b o s t " o f o o s ` g c m t o v g p h s o t o h ` f r h j g ` n h f b o s t
s g b g i t < < H r n m : 0 U m Z * 0 O ` t * 0 ! U m Z " U m Z /
s g b g i t ! ` / o : i m s g o h f
t | t 2 : 4 * 0 s p b o t M t ! b g ` c t a ! ` / % t / `
| h t a g r w o s g * 0 s p b o t M t t `
* * f u ` i t o h ` t h r g m n o ` p u t f r h j m f o b g " g m i a g b g j g ` t h f b o ` g
* * p b m i g n o ` m s g p g r m t g b o ` g " o f o : * = t a g b m s t b o ` g h f f o b g
* * i h ` t m o ` s r g s u b t h f o ` v g r s o h ` m ` n o s n o s p b m y g n o ` t a g r g s u b t
r g m n F o b g < < F o b g T m t a * 0 O ` t * 0 O H ! O ` t " U O ` t Z /
r g m n F o b g p m t a o ` t = : n h i h ` t g ` t s 2 * r g m n F o b g p m t a
r g t u r ` ! g x g i u t g ! s g b g i t ! j m p r g m n O ` t = ! b o ` g s i h ` t g ` t s / /
w a g r g
g x g i u t g ! s " s = / : ! s ` n ! j g r c g X s h r t s / " s = /
* * f u ` i t o h ` t h i h ` v g r t s t r o ` c t h o ` t
r g m n O ` t = < < Y t r o ` c * 0 O ` t
r g m n O ` t = : r g m n
7 | 6
-
7/27/2019 Algorithms - Inversion Count using Haskell
6/6
Mbchrotajs * Ihu`to`c O`vgrsoh`s uso`c Amsegbb
4.;.; Ihng ^ag Amsegbb ihng im` kg fhu`n mta t t p s < ( ( c o t a u k . i h j ( p o = ? > 4 > ( j = ? > 4 > (
k b h k ( j m s t g r ( M b c h r o t a j ( s h r t ( s h r t . a s
6 | 6
https://github.com/pi19404/m19404/blob/master/Algorithm/sort/sort.hshttps://github.com/pi19404/m19404/blob/master/Algorithm/sort/sort.hs