Sorting Practical in Place Mergsort
-
Upload
honestmanusa -
Category
Documents
-
view
216 -
download
0
Transcript of Sorting Practical in Place Mergsort
-
8/18/2019 Sorting Practical in Place Mergsort
1/13
N o r d i c J o u r n a l o f C o m p u t i n g
P R A C T I C A L I N - P L A C E M E R G E S O R T
J Y R K I K A T A J A I N E N
D e p a r t m e n t o f C o m p u t e r S c i e n c e , U n i v e r s i t y o f C o p e n h a g e n
U n i v e r s i t e t s p a r k e n 1 , D K - 2 1 0 0 C o p e n h a g e n E a s t , D E N M A R K
E l e c t r o n i c m a i l : j y r k i @ d i k u . d k
T O M I P A S A N E N
y
T u r k u C e n t r e f o r C o m p u t e r S c i e n c e ,
L e m m i n k a i s e n k a t u 1 4 A , F I N - 2 0 5 2 0 T u r k u , F I N L A N D
E l e c t r o n i c m a i l : T o m i . P a s a n e n @ u t u . f i
J U K K A T E U H O L A
z
D e p a r t m e n t o f C o m p u t e r S c i e n c e , U n i v e r s i t y o f T u r k u
L e m m i n k a i s e n k a t u 1 4 A , F I N - 2 0 5 2 0 T u r k u , F I N L A N D
E l e c t r o n i c m a i l : J u k k a . T e u h o l a @ u t u . f i
A b s t r a c t . T w o i n - p l a c e v a r i a n t s o f t h e c l a s s i c a l m e r g e s o r t a l g o r i t h m a r e a n a l y s e d
i n d e t a i l . T h e r s t , s t r a i g h t f o r w a r d v a r i a n t p e r f o r m s a t m o s t N l o g
2
N + O ( N )
c o m p a r i s o n s a n d 3 N l o g
2
N + O ( N ) m o v e s t o s o r t N e l e m e n t s . T h e s e c o n d , m o r e
a d v a n c e d v a r i a n t r e q u i r e s a t m o s t N l o g
2
N + O ( N ) c o m p a r i s o n s a n d " N l o g
2
N
m o v e s , f o r a n y x e d " > 0 a n d a n y N > N ( " ) . I n t h e o r y , t h e s e c o n d o n e i s
s u p e r i o r t o a d v a n c e d v e r s i o n s o f h e a p s o r t . I n p r a c t i c e , d u e t o t h e o v e r h e a d i n t h e
i n d e x m a n i p u l a t i o n , o u r f a s t e s t i n - p l a c e m e r g e s o r t b e h a v e s s t i l l a b o u t 5 0 p e r c e n t
s l o w e r t h a n t h e b o t t o m - u p h e a p s o r t . H o w e v e r , o u r i m p l e m e n t a t i o n s a r e p r a c t i c a l
c o m p a r e d t o m e r g e s o r t a l g o r i t h m s b a s e d o n i n - p l a c e m e r g i n g .
K e y w o r d s : s o r t i n g , m e r g e s o r t , i n - p l a c e a l g o r i t h m s
C R C l a s s i c a t i o n : F . 2 . 2
1 . I n t r o d u c t i o n
A s s u m e t h a t w e a r e g i v e n a n a r r a y A o f N e l e m e n t s t h a t a r e t o b e s o r t e d .
M e r g e s o r t i s a c l a s s i c a l s o r t i n g r o u t i n e t h a t c a n i t e r a t i v e l y b e d e s c r i b e d a s
f o l l o w s . I n i t i a l l y , e a c h e l e m e n t i s t h o u g h t t o f o r m a s o r t e d s u b s e q u e n c e o f
s i z e o n e . T h e s e s u b s e q u e n c e s a r e m e r g e d p a i r w i s e , a n d i n t h i s w a y w e c a n
( a l m o s t ) h a l v e t h e n u m b e r o f s o r t e d s u b s e q u e n c e s . T h i s p r o c e s s i s t h e n
r e p e a t e d u n t i l w e h a v e o n l y o n e s o r t e d s e q u e n c e l e f t , c o n t a i n i n g a l l t h e
e l e m e n t s .
P r e s e n t e d a t t h e 7 t h F i n n i s h S y m p o s i u m o n C o m p u t e r S c i e n c e , L i p e r i , F i n l a n d , J a n u a r y
1 9 9 4 .
y
S u p p o r t e d b y t h e A c a d e m y o f F i n l a n d u n d e r c o n t r a c t N o . 1 0 2 1 1 2 9 .
z
S u p p o r t e d b y t h e A c a d e m y o f F i n l a n d u n d e r c o n t r a c t N o . 1 0 7 1 3 3 7 .
R e c e i v e d F e b r u a r y 1 9 9 6 .
-
8/18/2019 Sorting Practical in Place Mergsort
2/13
2
T h e h e a r t o f t h e c o n s t r u c t i o n i s t h e m e r g e r o u t i n e , w h i c h c o m b i n e s t w o
s o r t e d s e q u e n c e s i n t o o n e . T h e d r a w b a c k o f t h e s t r a i g h t 2 - w a y m e r g e a l g o -
r i t h m 9 ] , w h i c h r e p e a t e d l y m o v e s t h e s m a l l e r o f t h e m i n i m a o f t h e r e m a i n i n g
s u b s e q u e n c e s t o a n o u t p u t a r e a , i s t h a t i t r e q u i r e s e x t r a s t o r a g e s p a c e f o r i t s
o p e r a t i o n . I f t h e s e q u e n c e s t o b e m e r g e d a r e o f s i z e m a n d n , r e s p e c t i v e l y ,
t h e n a t r i v i a l i m p l e m e n t a t i o n o f t h e 2 - w a y m e r g e r e q u i r e s a t m o s t m + n ? 1
c o m p a r i s o n s , m + n e l e m e n t m o v e s , a n d m + n e x t r a s t o r a g e l o c a t i o n s .
A s s u m i n g t h a t w e h a v e a n e x t r a a r r a y B o f s i z e N a v a i l a b l e , t h e m e r g e s o r t
a l g o r i t h m c a n b e t e c h n i c a l l y r e a l i z e d s o t h a t t h e e l e m e n t s o f A a r e m e r g e d
b a c k a n d f o r t h f r o m A t o B . W i t h t h e 2 - w a y m e r g e r o u t i n e , t h e n u m b e r o f
c o m p a r i s o n s p e r f o r m e d w i l l b e N l o g
2
N + O ( N ) a n d t h e n u m b e r o f e l e m e n t
m o v e s a l s o N l o g
2
N + O ( N ) . I f K - w a y m e r g e i s a p p l i e d , t h e n u m b e r o f
m o v e s c a n b e r e d u c e d t o ( 2 = d l o g
2
K e ) N l o g
2
N + O ( N ) w i t h o u t a e c t i n g
t h e n u m b e r o f c o m p a r i s o n s ( c f . S e c t i o n 3 ) .
I n t h i s p a p e r w e e x p l o r e t h e q u e s t i o n a s t o h o w t h e m e r g e s o r t a l g o r i t h m
c a n b e t u r n e d i n t o a n e c i e n t i n - p l a c e a l g o r i t h m . M o r e p r e c i s e l y , w e a s s u m e
t h a t o n l y o n e e x t r a s t o r a g e l o c a t i o n ( i n a d d i t i o n t o t h e a r r a y A ) i s a v a i l a b l e
f o r s t o r i n g t h e d a t a e l e m e n t s a n d O ( 1 ) s t o r a g e l o c a t i o n s a r e a v a i l a b l e f o r
s t o r i n g i n d i c e s o f t h e i n p u t a r r a y A . T h e o n l y o p e r a t i o n s p e r m i t t e d o n
t h e d a t a e l e m e n t s a r e c o m p a r i s o n s a n d m o v e s . I n t h e b a s i c a l g o r i t h m , t h e
i n d i c e s a r e m a n i p u l a t e d o n l y b y a d d i t i o n a n d s u b t r a c t i o n . ( T h e a l g o r i t h m
c o u l d b e i m p l e m e n t e d , w i t h o u t l o s s o f e c i e n c y , e v e n i f a d d i t i o n b y o n e
a n d s u b t r a c t i o n b y o n e w e r e t h e o n l y a r i t h m e t i c o p e r a t i o n s a l l o w e d . T h e s e
d e t a i l s a r e , h o w e v e r , l e f t f o r t h e i n t e r e s t e d r e a d e r . ) I n t h e n e - t u n e d v e r s i o n ,
w e a l s o n e e d d i v i s i o n ( o r s h i f t ) a s i n a d v a n c e d h e a p s o r t v a r i a t i o n s 3 ] .
I n t h e t h e o r e t i c a l a n a l y s i s o f t h e i n - p l a c e a l g o r i t h m s t o b e p r e s e n t e d , w e
c a l c u l a t e t h e n u m b e r o f e l e m e n t c o m p a r i s o n s a n d e l e m e n t m o v e s ( o r a s -
s i g n m e n t s ) m a d e i n t h e w o r s t c a s e . T h e u l t i m a t e g o a l w o u l d b e a s o r t i n g
a l g o r i t h m t h a t p e r f o r m s N l o g
2
N + O ( N ) c o m p a r i s o n s a n d O ( N ) m o v e s
( c f . M u n r o a n d R a m a n 1 1 ] ) . T h e p e r f o r m a n c e o f o u r a l g o r i t h m s i s w e l l
c h a r a c t e r i z e d b y t h e s e t w o q u a n t i t i e s , s i n c e t h e w o r k r e q u i r e d b y t h e i n d e x
m a n i p u l a t i o n s i s c l o s e l y r e l a t e d t o t h e n u m b e r o f t h e a b o v e o p e r a t i o n s . A s
t o t h e p r a c t i c a l e c i e n c y o f v a r i o u s i n - p l a c e a l g o r i t h m s , t h e i n d e x m a n i p u -
l a t i o n i s o f c o u r s e o f i m p o r t a n c e ( c f . S e c t i o n 4 ) .
M a n y a l g o r i t h m s f o r i n - p l a c e m e r g i n g h a v e b e e n p r o p o s e d , t h e p r e s e n t
c h a m p i o n b e i n g t h e o n e b y H u a n g a n d L a n g s t o n 7 ] . T h e i r a l g o r i t h m m e r g e s
t w o s e q u e n c e s o f s i z e m a n d n i n O ( m + n ) t i m e o r , m o r e p r e c i s e l y , i t
p e r f o r m s a t m o s t 1 5 ( m + n ) + O (
p
m + n l o g
2
( m + n ) ) c o m p a r i s o n s a n d
5 ( m + n ) + O (
p
m + n l o g
2
( m + n ) ) m o v e s
1
. W h e n t h i s m e r g e r o u t i n e i s
u s e d f o r i m p l e m e n t i n g m e r g e s o r t , N e l e m e n t s w i l l b e s o r t e d i n - p l a c e w i t h
a t m o s t 1 5 N l o g
2
N + O ( N ) c o m p a r i s o n s a n d 5 N l o g
2
N + O ( N ) m o v e s .
W e s h a l l s h o w t h a t m e r g e s o r t c a n b e i m p l e m e n t e d i n - p l a c e m o r e e c i e n t l y ,
1
A c t u a l l y , i n 1 2 ] i t w a s s h o w n t h a t t h e n u m b e r o f m o v e s i s b o u n d e d b y 6 ( m + n ) +
O (
p
m + n l o g
2
( m + n ) ) b u t b y u s i n g t h e \ h o l e " t e c h n i q u e , t o b e d e s c r i b e d i n S e c t i o n 2 ,
a n i m p l e m e n t a t i o n r e q u i r i n g a t m o s t 5 ( m + n ) + O (
p
m + n l o g
2
( m + n ) ) m o v e s i s o b t a i n e d .
-
8/18/2019 Sorting Practical in Place Mergsort
3/13
3
i f w e u s e t h e s t a n d a r d m e r g e r o u t i n e a s a s t a r t i n g p o i n t , i n s t e a d o f t h e k n o w n
i n - p l a c e m e r g e a l g o r i t h m s . T h e b a s i c o b s e r v a t i o n ( s e e 1 0 , L e m m a 3 ] o r 9 ,
E x e r c i s e 5 . 2 . 4 - 1 0 ] ) i s t h a t t h e 2 - w a y m e r g e c a n b e e a s i l y m o d i e d s u c h t h a t
i t w i l l m e r g e t w o s e q u e n c e s o f s i z e m a n d n b y u s i n g o n l y m i n ( m ; n ) e x t r a
s p a c e . O n e w a y t o o b t a i n t h i s i s a s f o l l o w s . F o r t h e s a k e o f s i m p l i c i t y , a s s u m e
t h a t t h e s e q u e n c e s X a n d Y t o b e m e r g e d a r e c o n s e c u t i v e s u b s e q u e n c e s o f
t h e s a m e a r r a y , a s t h e y a r e i n m e r g e s o r t , a n d t h a t X i s n o t l o n g e r t h a n Y
N o w , m o v e r s t t h e e l e m e n t s o f X t o a w o r k a r e a W , a n d t h e n m e r g e W
a n d Y i n t h e u s u a l m a n n e r t o t h e a r e a c o v e r e d b y X a n d Y
O u r m e r g e s o r t v a r i a n t i s b a s e d o n t h e p a r t i t i o n i n g p r i n c i p l e t h a t h a s
t u r n e d o u t t o b e u s e f u l i n m a n y c o n t e x t s ( s e e , e . g . 4 , 1 0 , 1 3 ] ) . F i r s t , w e s o r t
a b o u t o n e h a l f o f t h e e l e m e n t s b y u s i n g t h e s e c o n d h a l f o f t h e i n p u t a r r a y a s
a w o r k a r e a . S e c o n d , w e s o r t h a l f o f t h e s e c o n d h a l f i n t h e s a m e w a y . T h i r d ,
w e m e r g e t h e t w o s e q u e n c e s o f s i z e s a b o u t N = 2 a n d N = 4 b y u s i n g t h e l a s t
N = 4 p o s i t i o n s o f t h e i n p u t a r r a y a s a w o r k a r e a . T h e n w e r e p e a t e d l y s o r t
h a l f o f t h e r e m a i n i n g e l e m e n t s a n d m e r g e t h i s b l o c k t o g e t h e r w i t h t h e b i g
s o r t e d b l o c k o b t a i n e d s o f a r . I t i s n o t d i c u l t t o s e e t h a t t h i s a l g o r i t h m r u n s
i n O ( N l o g
2
N ) t i m e . A d e t a i l e d a n a l y s i s , t o b e g i v e n i n S e c t i o n 2 , s h o w s
t h a t t h e a l g o r i t h m c a n b e e a s i l y i m p l e m e n t e d s u c h t h a t i t w i l l p e r f o r m a t
m o s t N l o g
2
N + O ( N ) c o m p a r i s o n s a n d 3 N l o g
2
N + O ( N ) m o v e s .
I n S e c t i o n 3 w e d e s c r i b e a n a d v a n c e d v e r s i o n o f t h e a l g o r i t h m a n d s h o w
t h a t i t p e r f o r m s a t m o s t N l o g
2
N + O ( N ) c o m p a r i s o n s a n d " N l o g
2
N m o v e s ,
f o r a n y x e d " > 0 a n d a n y N > N ( " ) . ( O b s e r v e t h a t " h a s t o b e a x e d c o n -
s t a n t , s i n c e t h e a m o u n t o f e x t r a s p a c e r e q u i r e d b y t h e a l g o r i t h m i s e x p o n e n -
t i a l o n 1 = " . ) T h e s e g u r e s s h o u l d b e c o m p a r e d t o t h e c o r r e s p o n d i n g b o u n d s
k n o w n , f o r e x a m p l e , f o r h e a p s o r t . F l o y d ' s i m p r o v e m e n t 6 ] o f t h e s t a n d a r d
h e a p s o r t 1 9 ] m a k e s 2 N l o g
2
N + O ( N ) c o m p a r i s o n s a n d N l o g
2
N + O ( N )
m o v e s , a n d t h e b o t t o m - u p h e a p s o r t 5 , 1 8 ] 1 5 N l o g
2
N + O ( N ) c o m p a r i s o n s
a n d 1 5 N l o g
2
N + O ( N ) m o v e s , i n t h e w o r s t c a s e . M o r e a d v a n c e d h e a p s o r t
v a r i a t i o n s e x i s t ( s e e , e . g . 2 , 3 ] ) , b u t t h e s e a s s u m e t h a t t h e g e n e r a l s h i f t
o p e r a t i o n i s a c o n s t a n t t i m e o p e r a t i o n . ( O b s e r v e t h a t a l r e a d y b o t t o m - u p
h e a p s o r t u s e s t h e d i v i s i o n o p e r a t i o n . ) I f t h e e l e m e n t s t o b e s o r t e d a r e a l l
d i s t i n c t , t h e n u m b e r o f m o v e s p e r f o r m e d b y t h e h e a p s o r t a l g o r i t h m s i s a t
l e a s t 0 5 N l o g
2
N ? O ( N ) 1 6 ] . S o b o t h t h e n u m b e r o f c o m p a r i s o n s a n d
m o v e s m a d e b y h e a p s o r t a r e i n t h e w o r s t c a s e l a r g e r t h a n t h o s e m a d e b y
o u r i n - p l a c e m e r g e s o r t .
I n p r a c t i c a l e x p e r i m e n t s o u r f a s t e s t i n - p l a c e m e r g e s o r t p r o g r a m w a s a b o u t
5 0 p e r c e n t s l o w e r t h a n t h e b o t t o m - u p h e a p s o r t p r o g r a m . T h e r e f o r e , t h e
n u m b e r o f e l e m e n t c o m p a r i s o n s a n d e l e m e n t m o v e s d o e s n o t t e l l t h e w h o l e
t r u t h . H o w e v e r , i f t h e e c i e n c y i s c o m p a r e d t o e a r l i e r i n - p l a c e m e r g e s o r t i m -
p l e m e n t a t i o n s , e . g . , t h a t u t i l i z i n g t h e i n - p l a c e m e r g i n g a l g o r i t h m o f H u a n g
a n d L a n g s t o n 7 ] , o u r i m p l e m e n t a t i o n i s c o n s i d e r a b l y f a s t e r . T h e e x p e r i -
m e n t a l r e s u l t s a r e r e p o r t e d i n S e c t i o n 4 .
I t s h o u l d b e o b s e r v e d t h a t a m e r g e s o r t v e r s i o n s i m i l a r t o o u r s a p p e a r s
a l r e a d y i n t h e s e m i n a l p a p e r b y K r o n r o d 1 0 ] ( s e e a l s o 1 5 ] ) . T h e m a i n
c o n t r i b u t i o n i n t h i s p a p e r i s t h e d e t a i l e d a n a l y s i s o f t h e a l g o r i t h m a n d t h e
-
8/18/2019 Sorting Practical in Place Mergsort
4/13
4
e v a l u a t i o n o f i t s p r a c t i c a l e c i e n c y . O b s e r v e a l s o t h a t a n i n - p l a c e s o r t i n g a l -
g o r i t h m p e r f o r m i n g N l o g
2
N + O ( N ) c o m p a r i s o n s a n d " N l o g
2
N m o v e s h a s
b e e n d e v e l o p e d i n d e p e n d e n t l y b y R e i n h a r d t 1 4 ] . H e u s e s s i m i l a r t e c h n i q u e s
a s w e d o , b u t h i s m a i n r e s u l t s c o n c e r n m i n i m i z i n g t h e n u m b e r o f c o m p a r -
i s o n s , t h a t i s , t h e c o n s t a n t i n t h e l i n e a r t e r m . H e a l s o a p p l i e s q u i c k s o r t - l i k e
p a r t i t i o n i n g , w h i c h i n o u r o p i n i o n d o e s n o t b e l o n g t o a \ p u r e " m e r g e s o r t a l -
g o r i t h m . W i t h o u t e x a g g e r a t i n g , i t c a n b e s a i d t h a t o u r a l g o r i t h m i s s i m p l e r
t h a n t h a t g i v e n i n 1 4 ] .
2 . S t r a i g h t f o r w a r d i n - p l a c e m e r g e s o r t
I n t h i s s e c t i o n w e d e s c r i b e a n i n - p l a c e s o r t i n g a l g o r i t h m t h a t u s e s m e r g i n g
a s i t s b a s i c t o o l . T h i s a l g o r i t h m i s a v a r i a n t o f t h e s e c o n d i n - p l a c e s o r t i n g
a l g o r i t h m p r o p o s e d b y K r o n r o d 1 0 ] . W e a l s o a n a l y s e t h e e c i e n c y o f t h e
a l g o r i t h m . A m o r e n e - t u n e d v e r s i o n w i l l b e g i v e n i n t h e n e x t s e c t i o n .
A s s u m e t h a t t h e e l e m e n t s t o b e s o r t e d a r e g i v e n i n a n a r r a y A o f s i z e N
W e u s e t h e n o t a t i o n h i ; j i t o d e n o t e t h e b l o c k A i : : j ] o f e l e m e n t s . T h e c a s e
i > j r e p r e s e n t s a n e m p t y b l o c k . B y X ? Y w e m e a n a b l o c k , w h i c h i s l e f t
o v e r X w h e n t h e e l e m e n t s o f Y a r e r e m o v e d f r o m X , a n d b y X + Y a b l o c k
w h i c h i n c l u d e s a l l e l e m e n t s o f X a n d Y . T w o b l o c k s P a n d Q a r e s a i d t o b e
e q u i v a l e n t , d e n o t e d P Q , i f t h e y c o n t a i n p r e c i s e l y t h e s a m e e l e m e n t s o f
A , p o s s i b l y i n a d i e r e n t o r d e r . T h e s i z e P o f a n o n - e m p t y b l o c k P = h i ; j i
i s j ? i + 1 . T h e s i z e o f a n e m p t y b l o c k i s 0 .
I n o u r s o r t i n g a l g o r i t h m , i l l u s t r a t e d i n F i g . 1 , w e m a i n t a i n a n i n v a r i a n t
t h a t a r r a y A c o n s i s t s o f t w o c o n s e c u t i v e b l o c k s , P a n d Q , w h e r e P c o n t a i n s
s o m e e l e m e n t s i n n o s p e c i c o r d e r , w h e r e a s t h e e l e m e n t s o f Q a r e i n s o r t e d
o r d e r . A n i n i t i a l s i t u a t i o n i s c r e a t e d b y c h o o s i n g P = h 1 ; d N = 2 e i a n d s o r t i n g
t h e r e s t o f A t o Q b y u s i n g P a s t h e w o r k a r e a . T h e s i z e o f Q i s i n c r e a s e d
g r a d u a l l y a s f o l l o w s . F i r s t , w e d i v i d e P i n t o t w o b l o c k s , P
1
a n d P
2
, s o t h a t
P
1
= P
2
o r P
1
= P
2
? 1 . S e c o n d , w e s o r t P
1
b y u s i n g t h e s t a n d a r d
m e r g e s o r t r o u t i n e . B l o c k P
2
i s u s e d a s a w o r k a r e a . A s a r e s u l t w e g e t t w o
b l o c k s P
0
1
a n d P
0
2
, w h e r e P
0
1
P
1
a n d P
0
2
P
2
, a n d t h e e l e m e n t s o f P
0
1
a r e i n
s o r t e d o r d e r . T h i r d , w e m e r g e t h e b l o c k s P
0
1
a n d Q . T h e o u t p u t i s p r o d u c e d
\ o v e r " P
0
2
a n d Q . A s a r e s u l t o f t h i s m e r g e w e g e t t w o c o n s e c u t i v e b l o c k s ,
P
0 0
2
a n d Q
0
, w h e r e P
0 0
2
P
0
2
P
2
a n d Q
0
P
0
1
+ Q , a n d t h e e l e m e n t s o f Q
0
a p p e a r i n s o r t e d o r d e r . N o w w e a s s i g n P P
0 0
2
a n d Q Q
0
, a n d r e p e a t
t h e p r o c e s s , u n t i l P = 1 i n w h i c h c a s e t h e s i n g l e e l e m e n t i s m o v e d i n t o i t s
r i g h t p l a c e i n Q , p u s h i n g t h e s m a l l e r e l e m e n t s o n e s t e p l e f t .
F o r t h e p u r p o s e o f t h e a n a l y s i s , t w o p a r t s o f t h e a l g o r i t h m c a n b e r e a d i l y
s e p a r a t e d : t h e s o r t p a r t c o n s i s t s o f t h e c a l l s t o t h e m e r g e s o r t r o u t i n e a n d
t h e m e r g e p a r t c o n s i s t s o f t h e c a l l s t o t h e m e r g e r o u t i n e ( o u t s i d e m e r g e s o r t ) .
L e t S ( m ) d e n o t e t h e t i m e r e q u i r e d b y t h e s o r t p a r t a f t e r P h a s r e a c h e d t h e
s i z e m , a n d l e t M ( m ; n ) d e n o t e t h e t i m e r e q u i r e d b y t h e m e r g e p a r t a f t e r
P h a s r e a c h e d t h e s i z e m a n d Q t h e s i z e n . T h e t i m e r e q u i r e m e n t s o f b o t h
-
8/18/2019 Sorting Practical in Place Mergsort
5/13
5
A
Q
P
Q
P
2
P
1
Q
P
2
s o r t
s o r t
m e r g e
q
F i g . 1 : I l l u s t r a t i o n o f t h e s t e p s i n t h e b a s i c a l g o r i t h m . A h o r i z o n t a l r e c t a n g l e r e p r e s e n t s
a n u n o r d e r e d b l o c k , a n d a t r i a n g l e a s o r t e d b l o c k .
p a r t s c a n b e e x p r e s s e d r e c u r s i v e l y w i t h t h e f o l l o w i n g r e c u r r e n c e r e l a t i o n s :
S ( m ) = O ( m l o g
2
m ) + S ( d m = 2 e ) ; f o r m > 1 ;
S ( 1 ) = O ( 1 ) ;
M ( m ; n ) = O ( m + n ) + M ( d m = 2 e ; b m = 2 c + n ) ; f o r m > 1 ;
M ( 1 ; n ) = O ( 1 + n ) ;
I t i s e a s y t o s e e t h a t S ( m ) = O ( m l o g
2
m ) a n d a l s o M ( d m = 2 e ; b m = 2 c ) =
O ( m l o g
2
m ) . S i n c e t h e t o t a l r u n n i n g t i m e o f t h e a l g o r i t h m i s p r o p o r t i o n a l
t o S ( d N = 2 e ) + M ( d N = 2 e ; b N = 2 c ) , t h e a l g o r i t h m r u n s i n O ( N l o g
2
N ) t i m e .
N e x t w e s h a l l g i v e a d e t a i l e d d e s c r i p t i o n o f t h e s u b r o u t i n e s u s e d , i n o r d e r
t o a n a l y s e t h e n u m b e r o f c o m p a r i s o n s a n d m o v e s p e r f o r m e d . W e s t a r t w i t h
-
8/18/2019 Sorting Practical in Place Mergsort
6/13
6
t h e m e r g e r o u t i n e , w h i c h i s n e e d e d i n t h e s o r t p a r t . T h e b a s i c t a s k i s t o
m e r g e r e p e a t e d l y t w o c o n s e c u t i v e b l o c k s , X a n d Y , o f a b o u t t h e s a m e s i z e
t o a n o t h e r b l o c k W i n t h e w o r k a r e a . E a c h t i m e a n e l e m e n t e f r o m X o r
Y i s m o v e d t o W , t h e e l e m e n t s i t t i n g i n W s h o u l d b e m o v e d t o t h e p l a c e
o f e . U s u a l l y t h i s i s d o n e b y o n e s w a p , b u t s i n c e a s w a p r e q u i r e s 3 m o v e s
( a s s i g n m e n t s ) , w e c h o o s e a n o t h e r w a y o f i m p l e m e n t i n g t h e m e r g e s . W e t a k e
t h e r s t e l e m e n t o f W a n d s t o r e i t s e p a r a t e l y . T h i s c r e a t e s a h o l e i n W
T h e n t h e n e x t e l e m e n t e o f X o r Y i s m o v e d t o t h e h o l e , a n d a n e w h o l e i s
c r e a t e d b y m o v i n g t h e e l e m e n t o f W b e s i d e t h e p r e v i o u s h o l e i n t o t h e p l a c e
o f e . T h i s i s r e p e a t e d u n t i l a l l t h e e l e m e n t s o f X a n d Y a r e m o v e d t o W
F i n a l l y , t h e r s t s t o r e d e l e m e n t o f W i s m o v e d t o t h e h o l e i n X o r Y
I m p l e m e n t e d t h i s w a y , t h e m e r g i n g o f t w o b l o c k s o f s i z e m a n d n r e q u i r e s
m + n ? 1 c o m p a r i s o n s a n d o n l y 2 ( m + n ) + 1 m o v e s , n o t 3 ( m + n ) a s w h e n
i m p l e m e n t e d w i t h s w a p s . T o s o r t n
0
e l e m e n t s , n
0
l o g
2
n
0
+ O ( n
0
) c o m p a r i s o n s
a n d 2 n
0
l o g
2
n
0
+ O ( n
0
) m o v e s a r e r e q u i r e d . I n t h e s o r t p a r t , s o r t i n g i s n e e d e d
f o r b l o c k s o f s i z e b
N
2
c ; b d
N
2
e = 2 c , e t c . I t i s n o t d i c u l t t o s e e t h a t i n t h e s o r t
p a r t , t h e n u m b e r o f c o m p a r i s o n s i s b o u n d e d b y N l o g
2
N + O ( N ) a n d t h e
n u m b e r o f m o v e s b y 2 N l o g
2
N + O ( N )
A c t u a l l y , w e c o u l d d i v i d e t h e w o r k a b i t d i e r e n t l y , s o t h a t t h e e l e m e n t s i n
t h e b l o c k h d N = 3 e + 1 ; N i a r e m e r g e s o r t e d r s t , b y u s i n g t h e b l o c k h 1 ; d N = 3 e i
a s t h e w o r k a r e a . H o w e v e r , i t c a n b e e a s i l y v e r i e d t h a t t h i s c h o i c e h a s n o
e e c t o n t h e c o n s t a n t s o f t h e a b o v e f o r m u l a s . H e n c e , w e s t i c k t o t h e m o r e
u n i f o r m a p p r o a c h o f d i v i d i n g b l o c k s a l w a y s a t h a l f .
I n t h e m e r g e p a r t o f o u r a l g o r i t h m , t h e t w o b l o c k s t o b e m e r g e d a r e n o t o f
t h e s a m e s i z e . T h e r e f o r e , i t i s h e r e b e t t e r t o u s e t h e b i n a r y m e r g e r o u t i n e ,
i n s t e a d o f t h e n o r m a l ( u n a r y 2 - w a y ) o n e . T h e b i n a r y m e r g e a l g o r i t h m w a s
d e s c r i b e d i n 8 ] . T h e b a s i c i d e a i s a s f o l l o w s . L e t X a n d Y b e t h e b l o c k s
t o b e m e r g e d . L e t t h e i r s i z e s b e m a n d n , r e s p e c t i v e l y . F o r t h e s a k e o f
s i m p l i c i t y , a s s u m e t h a t m n . N o w l e t t = b l o g
2
( n = m ) c a n d c o m p a r e
x
1
, t h e r s t e l e m e n t o f X , t o y
2
t , t h e 2
t
t h e l e m e n t o f Y I f x
1
< y
2
t , t h e
p r o p e r p l a c e o f x
1
i s s e a r c h e d f o r b y a p p l y i n g b i n a r y s e a r c h . L e t u s a s s u m e
t h a t y
k ? 1
x
1
< y
k
, k 2
t
. N o w t h e Y - b l o c k h 1 ; k ? 1 i i s m o v e d t o t h e
o u t p u t a r e a f o l l o w e d b y x
1
, a n d t h e m e r g i n g p r o c e s s i s r e p e a t e d f o r X - b l o c k
h 2 ; X i a n d Y - b l o c k h k ; Y i I f x
1
y
2
t , t h e n Y - b l o c k h 1 ; 2
t
i i s m o v e d t o
t h e o u t p u t a r e a a n d t h e m e r g e p r o c e s s i s r e p e a t e d f o r X - b l o c k h 1 ; X i a n d
Y - b l o c k h 2
t
+ 1 ; Y i
H w a n g a n d L i n 8 ] p r o v e d t h a t t h e b i n a r y m e r g e r o u t i n e m e r g e s t w o
b l o c k s o f s i z e m a n d n w i t h d l o g
2
?
m + n
m
e + m i n f m ; n g c o m p a r i s o n s , w h i c h i s
O ( m l o g
2
( n = m ) ) . T h e n u m b e r o f m o v e s w i l l b e 2 ( m + n ) + 1 , i f i m p l e m e n t e d
a s c a r e f u l l y a s i n t h e m e r g e s o f t h e s o r t p a r t . N o w , i n t h e m e r g e p a r t , t h e
b i n a r y m e r g e r o u t i n e i s c a l l e d a t m o s t l o g
2
N + 2 t i m e s . F i r s t , t h e s i z e s
o f t h e m e r g e d b l o c k s a r e b
N
2
c a n d b d
N
2
e = 2 c ; s e c o n d , b
N
2
c + b d
N
2
e = 2 c a n d
b d d
N
2
e = 2 e = 2 c , e t c . T h e n u m b e r o f c o m p a r i s o n s i s t h e n o f t h e o r d e r
O
N
4
l o g
2
2 +
N
8
l o g
2
6 + +
N
2
i
l o g
2
( 2
i
? 2 ) +
;
-
8/18/2019 Sorting Practical in Place Mergsort
7/13
7
w h i c h i s
O
0
@
N
o g
2
N
X
i = 1
i = 2
i
1
A
= O ( N )
T h e n u m b e r o f m o v e s i s b o u n d e d a b o v e b y
2 ( ( N = 2 + N = 4 ) + ( N = 2 + N = 4 + N = 8 ) + ) + O ( N ) ;
w h i c h g i v e s 2 N l o g
2
N + O ( N )
I n t h e m e r g e p a r t , m o s t w o r k i s d o n e w h e n s m a l l b l o c k s a r e m e r g e d w i t h
t h e b i g s o r t e d b l o c k . W e s a y t h a t a b l o c k i s t i n y i f i t s s i z e i s s m a l l e r t h a n
p
N , o t h e r w i s e a b l o c k i s c a l l e d h u g e . B o t h t h e n u m b e r o f t i n y a n d h u g e
b l o c k s i s l e s s t h a n o r e q u a l t o
1
2
l o g
2
N + 2 . T o s a v e w o r k i n t h e m e r g e p a r t ,
t h e h u g e a n d t i n y b l o c k s c a n b e m e r g e d s e p a r a t e l y . T h a t i s , w h e n t h e r s t
t i n y b l o c k i s e n c o u n t e r e d , t h i s i s n o m o r e m e r g e d t o t h e m e r g e r e s u l t o f t h e
h u g e b l o c k s , b u t a n o t h e r s o r t e d b l o c k o f s i z e a t m o s t 2
p
N i s f o r m e d b y
m e r g i n g a l l t i n y b l o c k s t o g e t h e r . F i n a l l y , t h e t w o s o r t e d b l o c k s ( X a n d Y )
a r e m e r g e d t o g e t h e r b y m o v i n g t h e e l e m e n t s o f X i n t o t h e i r p r o p e r p l a c e s
i n t h e f o l l o w i n g w a y . F i r s t , n d t h e p r o p e r p l a c e w i t h i n Y f o r t h e m i n i m u m
e l e m e n t o f X . T h e n i n t e r c h a n g e b l o c k X w i t h t h e f r o n t p a r t o f Y , a n d
r e p e a t t h e p r o c e d u r e f o r t h e n e x t s m a l l e s t e l e m e n t o f X a n d t h e r e s t o f Y
( T h i s i s a n a l o g o u s t o t h e b l o c k m e r g e b a c k w a r d s o p e r a t i o n o f T r a b b
P a r d o 1 7 ] . ) I t i s e a s y t o s e e t h a t t h i s n a l m e r g e o n l y r e q u i r e s O ( N ) t i m e ,
s i n c e e a c h e l e m e n t o f X i s m o v e d o v e r a s u b b l o c k o f Y a t m o s t 2
p
N t i m e s ,
a n d e a c h e l e m e n t o f Y i s t o u c h e d o n l y o n c e . T h i s t r i c k r e d u c e s t h e n u m b e r
o f m o v e s p e r f o r m e d i n t h e m e r g e p a r t o f o u r a l g o r i t h m t o N l o g
2
N + O ( N )
T o s u m u p , t h e n u m b e r o f c o m p a r i s o n s p e r f o r m e d i n t h e w h o l e a l g o r i t h m i s
b o u n d e d b y N l o g
2
N + O ( N ) a n d t h e n u m b e r o f m o v e s b y 3 N l o g
2
N + O ( N )
3 . A d v a n c e d i n - p l a c e m e r g e s o r t
I n t h i s s e c t i o n w e s h o w h o w t h e e c i e n c y o f o u r i n - p l a c e m e r g e s o r t a l g o r i t h m
c a n b e i m p r o v e d . O u r p u r p o s e i s r s t t o r e d u c e t h e n u m b e r o f m o v e s i n t h e
s o r t p a r t o f t h e a l g o r i t h m a n d t h e n t h o s e p e r f o r m e d i n t h e m e r g e p a r t .
T o r e d u c e t h e n u m b e r o f m o v e s r e q u i r e d i n t h e s o r t p a r t , w e u s e K -
w a y m e r g e ( K b e i n g a n a r b i t r a r y c o n s t a n t ) i n s t e a d o f 2 - w a y m e r g e a s d o n e
e a r l i e r . B y d o i n g t h i s , t h e n u m b e r o f m e r g i n g l e v e l s w i l l c o m e d o w n t o
d l o g
K
N e + O ( 1 ) . C h o o s i n g , e . g . , K = 4 w i l l r e a d i l y r e d u c e t h e n u m b e r
o f m o v e s b y a f a c t o r o f t w o . M o r e g e n e r a l l y , t h e n u m b e r o f m o v e s w i l l b e
( 2 = d l o g
2
K e ) N l o g
2
N + O ( N ) . H o w e v e r , t h e n u m b e r o f c o m p a r i s o n s c a n
b e c o m e l a r g e r i f t h e K - w a y m e r g e i s n o t i m p l e m e n t e d c a r e f u l l y . N e x t w e
s h o w h o w t h e n u m b e r o f c o m p a r i s o n s c a n b e k e p t a l m o s t u n c h a n g e d .
L e t t h e K b l o c k s t o b e m e r g e d b e X
1
; : : : ; X
K
. T o d e c i d e , w h i c h o f t h e
b l o c k s c o n t a i n s t h e s m a l l e s t e l e m e n t , K ? 1 c o m p a r i s o n s a r e r e q u i r e d . T h i s
i s d o n e b y b u i l d i n g a s e l e c t i o n t r e e ( c f . 9 ] ) o f d e p t h d l o g
2
K e a b o v e t h e K
e l e m e n t s . T h e s m a l l e s t e l e m e n t i s m o v e d t o t h e o u t p u t a r e a . A f t e r t h i s , t h e
-
8/18/2019 Sorting Practical in Place Mergsort
8/13
8
s e l e c t i o n t r e e i s u p d a t e d b y i n s e r t i n g i n t h e t r e e t h e e l e m e n t f o l l o w i n g t h e
s m a l l e s t o n e o f t h e s a m e b l o c k . T o d o t h i s , o n l y d l o g
2
K e c o m p a r i s o n s a r e
n e e d e d . N o t e t h a t , t o a v o i d e l e m e n t m o v e s , o n l y p o i n t e r s t o e l e m e n t s a r e
s t o r e d i n t h e t r e e .
S i n c e , d u r i n g t h e a l g o r i t h m , o n l y O ( N = K ) K - w a y m e r g e s a r e p e r f o r m e d ,
K - m e r g i n g w i l l c a u s e O ( N ) e x t r a c o m p a r i s o n s . F o r a l l e l e m e n t s , e x c e p t
t h e s m a l l e s t , d l o g
2
K e c o m p a r i s o n s a r e d o n e p e r e l e m e n t a t e v e r y m e r g i n g
l e v e l . B u t s i n c e t h e n u m b e r o f l e v e l s i s n o w d l o g
K
N e + O ( 1 ) , t h e n u m b e r
o f c o m p a r i s o n s p e r f o r m e d i s N l o g
2
N + O ( N ) i n t o t a l .
L e t u s n e x t c o n s i d e r , h o w t h e n u m b e r o f m o v e s c a n b e r e d u c e d i n t h e
m e r g e p a r t o f t h e a l g o r i t h m . A c t u a l l y , o n e c o u l d s a y t h a t t h e m e r g e s i n
t h i s p a r t a r e d o n e i n t h e w o r s t p o s s i b l e o r d e r . I f t h e s m a l l e s t b l o c k c o u l d
b e m e r g e d w i t h t h e s e c o n d s m a l l e s t , a n d t h e r e s u l t o f t h i s w i t h t h e t h i r d
s m a l l e s t , a n d s o o n , t h e t o t a l w o r k r e q u i r e d f o r t h e m e r g e s w o u l d b e O ( N )
H o w e v e r , t h i s c a n n o t b e d o n e , b e c a u s e w e n e e d a w o r k a r e a f o r o u r m e r g e s .
A s o b s e r v e d a l r e a d y i n t h e p r e v i o u s s e c t i o n , m o s t w o r k i n t h e m e r g e p a r t
i s d o n e w h e n s m a l l b l o c k s a r e m e r g e d w i t h t h e u n i o n o f p r e v i o u s b l o c k s .
N o w w e w i l l e l a b o r a t e t h i s i d e a f u r t h e r o n . L e t u s c a l l a b l o c k s m a l l , i f i t s
s i z e i s l e s s t h a n N = l o g
2
N ; o t h e r w i s e c a l l i t b i g . I n t h e s a m e w a y a s e a r l i e r
w e s o r t a n d m e r g e t h e b i g b l o c k s ( f o r m i n g b l o c k Y ) u n t i l t h e r s t s m a l l
b l o c k i s e n c o u n t e r e d . B e c a u s e t h e s i z e o f t h e w o r k a r e a ( i n c l u d i n g t h e r s t
s m a l l b l o c k ) i s n o w a t m o s t 2 N = l o g
2
N , w e c a n s o r t i t b y t h e s t r a i g h t f o r w a r d
i n - p l a c e m e r g e s o r t i n a l i n e a r t i m e g i v i n g b l o c k X , s e e F i g . 2 ( a ) .
W h e n m e r g i n g t h e b l o c k s X a n d Y w e n e e d a w o r k a r e a o f s i z e X . T h i s
i s f o r m e d b y s e a r c h i n g t h e X s m a l l e s t e l e m e n t s o f X a n d Y g i v i n g b l o c k s x
a n d y ( a c t u a l l y w e m u s t o n l y e n s u r e t h a t X ? x x + y ) , s e e F i g . 2 ( b ) .
T h e n a l s t e p s o f t h e a l g o r i t h m a r e : i n t e r c h a n g e b l o c k s X ? x a n d x , m e r g e
b l o c k s X ? x a n d Y ? y a n d s o r t b l o c k x + y b y t h e s t r a i g h t f o r w a r d i n - p l a c e
m e r g e s o r t , s e e F i g . 2 ( c , d , e ) . A s w e e a s i l y s e e , t h e n a l s t e p s o f t h e a l g o r i t h m
n e e d o n l y a l i n e a r t i m e . A l t e r n a t i v e l y , X a n d Y c o u l d h a v e b e e n m e r g e d ,
a s p r o p o s e d b y K r o n r o d 1 0 ] , b y u s i n g a n i n - p l a c e m e r g i n g r o u t i n e , t h e u s e
o f w h i c h w e h a v e w a n t e d t o a v o i d .
T o s u m m a r i z e , i n t h e m e r g e p a r t o f t h e a l g o r i t h m w e m a k e O ( N ) c o m -
p a r i s o n s a n d 2 N l o g
2
l o g
2
N + O ( N ) m o v e s , s i n c e t h e n u m b e r o f m e r g e s i s
l o g
2
l o g
2
N + O ( 1 ) . T h u s , t h e c o m p u t a t i o n a l c o s t s o f t h e a l g o r i t h m a r e d o m -
i n a t e d b y t h o s e o f t h e s o r t p a r t , w h i c h i s s h o w n t o r e q u i r e N l o g
2
N + O ( N )
c o m p a r i s o n s a n d ( 2 = d l o g
2
K e ) N l o g
2
N + O ( N ) = 2 N l o g
K
N + O ( N ) m o v e s ,
w h e r e K 2 i s a c o n s t a n t . I n o t h e r w o r d s , t h e n u m b e r o f c o m p a r i s o n s p e r -
f o r m e d b y t h e a l g o r i t h m i s b o u n d e d a b o v e b y N l o g
2
N + O ( N ) a n d t h e
n u m b e r o f m o v e s b y " N l o g
2
N , f o r a n y x e d " > 0 , i f N > N ( " ) ( e . g . ,
c h o o s e K = 1 6
1 = "
a n d l e t N b e b i g e n o u g h ) .
-
8/18/2019 Sorting Practical in Place Mergsort
9/13
9
X Y
X ? x
Y ? y
y
x
x + y
Y ? y
y
X ? x
( e )
( d )
( c )
( b )
( a )
j
/
m e r g e
x
j
F i g . 2 : I l l u s t r a t i o n o f t h e a d v a n c e d a l g o r i t h m . A h o r i z o n t a l r e c t a n g l e r e p r e s e n t s a n
u n o r d e r e d b l o c k .
4 . E x p e r i m e n t a l r e s u l t s
T o e v a l u a t e t h e p r a c t i c a l e c i e n c y o f t h e i n - p l a c e m e r g e s o r t a l g o r i t h m p r e -
s e n t e d i n t h e p r e v i o u s s e c t i o n , w e p r o g r a m m e d i t w i t h t h e C l a n g u a g e . F o r
t h e s a k e o f c o m p a r i s o n s , w e p r o g r a m m e d a l s o t h e b o t t o m - u p h e a p s o r t a l g o -
r i t h m 1 8 ] a n d t h e s t a n d a r d m e r g e s o r t a l g o r i t h m u s i n g l i n e a r e x t r a s p a c e 9 ] .
O u r v e r y r s t i m p l e m e n t a t i o n o f t h e i n - p l a c e m e r g e s o r t a l g o r i t h m u s e d a
g e n e r a l k - w a y m e r g e r o u t i n e . T h e p r o g r a m t u r n e d o u t t o b e u n a c c e p t a b l y
s l o w , s o w e w r o t e s p e c i a l i z e d p r o g r a m s f o r k e q u a l t o 2 , 4 , a n d 8 . T h e p r o -
g r a m w i t h k = 4 b e h a v e d b e s t i n t h e f o l l o w i n g e x p e r i m e n t s . A f t e r s o m e p r o -
l i n g , i t w a s o b s e r v e d t h a t t h e s e l e c t i o n t r e e w a s a b o t t l e n e c k i n t h e i n - p l a c e
4 - m e r g e s o r t p r o g r a m . T h e r e f o r e w e r e p l a c e d t h e s e l e c t i o n t r e e b y a n a r r a y
o f s i z e 4 . T h e r e s u l t i n g p r o g r a m i s d e n o t e d i n - p l a c e 4 * - m e r g e s o r t . N o w ,
t o n d t h e s m a l l e s t o f a n y f o u r e l e m e n t s , t h r e e c o m p a r i s o n s a r e r e q u i r e d ,
-
8/18/2019 Sorting Practical in Place Mergsort
10/13
1 0
w h i l e o n l y t w o a r e n e c e s s a r y i f t h e s e l e c t i o n t r e e i s i n u s e . T h e i n c r e a s e i n
t h e n u m b e r o f c o m p a r i s o n s c a n b e c l e a r l y s e e n i n F i g . 3 ( a ) . T h e t h e o r e t i c a l
a n a l y s i s o f t h e 4 * - m e r g e s o r t a l g o r i t h m i s l e f t f o r t h e r e a d e r . E v e n i f t h i s
i m p l e m e n t a t i o n i s t h e o r e t i c a l l y i n f e r i o r t o t h e i m p l e m e n t a t i o n o f S e c t i o n 3 ,
i t w a s t h e f a s t e s t i n - p l a c e m e r g e s o r t a l g o r i t h m i n o u r e x p e r i m e n t s .
T h e e x p e r i m e n t a l r e s u l t s r e p o r t e d i n F i g . 3 a n d F i g . 4 w e r e c a r r i e d o u t o n
S o l b o u r n e S - 4 0 0 0 , w h i c h h a s a S p a r c p r o c e s s o r . T h e p r o g r a m s w e r e c o m p i l e d
b y t h e G n u - C c o m p i l e r w i t h n o o p t i m i z a t i o n s . T h e i n p u t e l e m e n t s w e r e
p s e u d o - r a n d o m i n t e g e r s a n d t h e a m o u n t o f e l e m e n t s i n d i e r e n t e x p e r i m e n t s
w e r e 5 0 0 0 , 1 0 0 0 0 , 2 0 0 0 0 , 4 0 0 0 0 , 6 0 0 0 0 , 8 0 0 0 0 , a n d 1 0 0 0 0 0 . T h e
m e a s u r e d q u a n t i t i e s i n F i g . 3 a n d F i g . 4 a r e t h e a v e r a g e v a l u e s f o r 1 0 a n d
( a )
5 , 0 0 0 2 5 , 0 0 0 5 0 , 0 0 0 7 5 , 0 0 0 1 0 0 , 0 0 0
1 , 0 0 0 , 0 0 0
2 , 0 0 0 , 0 0 0
# E l e m e n t s
# C o m p a r i s o n s
I n - p l a c e 2 - m e r g e s o r t
I n - p l a c e 4 - m e r g e s o r t
I n - p l a c e 4 * - m e r g e s o r t
? M e r g e s o r t
H e a p s o r t
?
?
?
?
?
?
?
( b )
I n - p l a c e 2 - m e r g e s o r t
I n - p l a c e 4 - m e r g e s o r t
I n - p l a c e 4 * - m e r g e s o r t
? M e r g e s o r t
H e a p s o r t
5 , 0 0 0
1 , 0 0 0 , 0 0 0
2 , 0 0 0 , 0 0 0
3 , 0 0 0 , 0 0 0
4 , 0 0 0 , 0 0 0
2 5 , 0 0 0 5 0 , 0 0 0 7 5 , 0 0 0 1 0 0 , 0 0 0
# E l e m e n t s
# M o v e s
?
?
?
?
?
?
?
F i g . 3 : T h e n u m b e r o f ( a ) c o m p a r i s o n s a n d ( b ) m o v e s p e r f o r m e d b y v a r i o u s s o r t i n g
p r o g r a m s f o r r a n d o m l y g e n e r a t e d d a t a .
-
8/18/2019 Sorting Practical in Place Mergsort
11/13
1 1
5 0 r u n s , r e s p e c t i v e l y .
F i g . 3 ( a ) g i v e s t h e n u m b e r o f c o m p a r i s o n s a n d F i g . 3 ( b ) t h e n u m b e r o f
m o v e s p e r f o r m e d b y t h e v a r i o u s s o r t i n g p r o g r a m s . W e s t u d i e d t h e a c t u a l
r u n n i n g t i m e s o f t h e p r o g r a m s i n t w o m o d e l s : M I X m o d e l 9 ] a n d q s o r t
m o d e l 1 ] . T h e M I X m o d e l r e e c t s t h e f a c t t h a t t h e e l e m e n t s t o b e s o r t e d
a r e i n t e g e r s , w h i c h i s k n o w n b e f o r e h a n d . T h e r e f o r e , t h e c o s t o f c o m p a r i s o n s ,
m o v e s , a n d i n d e x o v e r h e a d s h o u l d b e a b o u t t h e s a m e . M o v e s s h o u l d , h o w -
e v e r , b e m o r e e x p e n s i v e i f w e w e r e s o r t i n g b i g r e c o r d s . T h e q s o r t m o d e l
t a k e s i n t o a c c o u n t t h e f a c t t h a t a g e n e r a l s o r t i n g p r o g r a m s h o u l d b e p o l y -
m o r p h i c , a s t h e q s o r t s o r t i n g r o u t i n e i n a C l i b r a r y . S i n c e e a c h c o m p a r i s o n
i n v o l v e s a f u n c t i o n c a l l , c o m p a r i s o n s a r e m o r e e x p e n s i v e t h a n m o v e s a n d i n -
( a )
I n - p l a c e 2 - m e r g e s o r t
I n - p l a c e 4 - m e r g e s o r t
I n - p l a c e 4 * - m e r g e s o r t
?
M e r g e s o r t
H e a p s o r t
5 , 0 0 0
1
2
3
4
5
6
7
2 5 , 0 0 0 5 0 , 0 0 0 7 5 , 0 0 0 1 0 0 , 0 0 0
# E l e m e n t s
R u n n i n g t i m e s ( s e c o n d s ) w i t h i n - l i n e c o m p a r i s o n s
?
?
?
?
?
?
?
( b )
I n - p l a c e 4 * - m e r g e s o r t
?
M e r g e s o r t
H e a p s o r t
5 , 0 0 0
1
2
3
4
5
6
7
8
2 5 , 0 0 0 5 0 , 0 0 0 7 5 , 0 0 0 1 0 0 , 0 0 0
# E l e m e n t s
R u n n i n g t i m e s ( s e c o n d s ) f o r p o l y m o r p h i c a l g o r i t h m s
?
?
?
?
?
?
?
F i g . 4 : T h e r u n n i n g t i m e s o f v a r i o u s s o r t i n g p r o g r a m s f o r r a n d o m l y g e n e r a t e d d a t a u n d e r
( a ) t h e M I X m o d e l a n d ( b ) t h e q s o r t m o d e l .
-
8/18/2019 Sorting Practical in Place Mergsort
12/13
1 2
d e x o v e r h e a d . F i g . 4 ( a ) g i v e s t h e r u n n i n g t i m e s f o r t h e p r o g r a m s w i t h i n - l i n e
c o m p a r i s o n s a n d F i g . 4 ( b ) f o r t h e p r o g r a m s w i t h t h e q s o r t i n t e r f a c e .
O n e s h o u l d o b s e r v e t h a t t h e a v e r a g e c a s e f o r m e r g e s o r t i s o n l y a l i t t l e b i t
b e t t e r t h a n t h e w o r s t c a s e w h i l e f o r b o t t o m - u p h e a p s o r t t h i s i s n o t t r u e ; t h e
e x p e r i m e n t s r e e c t t h i s b e c a u s e p s e u d o - r a n d o m i n t e g e r s w e r e u s e d a s i n p u t .
A c c o r d i n g t o o u r e x p e r i m e n t s b o t t o m - u p h e a p s o r t i s s t i l l t h e f a s t e s t i n - p l a c e
s o r t i n g m e t h o d g u a r a n t e e i n g t h e O ( N l o g
2
N ) w o r s t - c a s e p e r f o r m a n c e . I n -
p l a c e m e r g e s o r t i s 2 5 - 5 0 p e r c e n t s l o w e r t h a n b o t t o m - u p h e a p s o r t d e p e n d i n g
o n t h e c o s t m o d e l . W e c o m p a r e d a l s o o u r i n - p l a c e m e r g e s o r t i m p l e m e n t a t i o n
t o t h o s e b a s e d o n i n - p l a c e m e r g i n g , w h i c h h a d b e e n p r o g r a m m e d b y t h e
s e c o n d a u t h o r a s p a r t o f h i s M . S c . p r o j e c t 1 2 ] . T h e s e e a r l i e r m e r g e s o r t
p r o g r a m s w e r e h o w e v e r m u c h s l o w e r t h a n t h o s e r e p o r t e d i n t h e p r e s e n t
w o r k .
5 . C o n c l u s i o n s
T h e p e r f o r m a n c e o f t h e p r e s e n t e d i n - p l a c e a l g o r i t h m s i s s u m m a r i z e d i n T a -
b l e I . T h e r s t a l g o r i t h m i s t h e b a s i c o n e w h e r e n o o p t i m i z a t i o n i s d o n e . T h e
s e c o n d o n e m e r g e s t h e t i n y b l o c k s ( s m a l l e r t h a n
p
N ) a n d t h e h u g e b l o c k s
s e p a r a t e l y . T h e l a s t o n e u s e s K - w a y m e r g e i n s o r t i n g a s w e l l a s m e r g e s t h e
s m a l l b l o c k s ( s m a l l e r t h a n N = l o g
2
N ) a n d t h e b i g b l o c k s s e p a r a t e l y .
T a b l e I : S u m m a r y o f t h e r e s u l t s .
c o m p a r i s o n s m o v e s
b a s i c s o r t N l o g
2
N + O ( N ) 2 N l o g
2
N + O ( N )
a l g o r i t h m m e r g e O ( N ) 2 N l o g
2
N + O ( N )
s t r a i g h t f o r w a r d s o r t N l o g
2
N + O ( N ) 2 N l o g
2
N + O ( N )
a l g o r i t h m m e r g e O ( N ) N l o g
2
N + O ( N )
a d v a n c e d s o r t N l o g
2
N + O ( N ) ( 2 = d l o g
2
K e ) N l o g
2
N + O ( N )
a l g o r i t h m m e r g e O ( N ) 2 N l o g
2
l o g
2
N + O ( N )
W e h a v e s h o w n t h a t i t i s r e l a t i v e l y s i m p l e t o d e v i c e a n i n - p l a c e m e r g e -
s o r t , t h e p e r f o r m a n c e o f w h i c h i s o f t h e o r d e r O ( N l o g
2
N ) . W i t h v a r i o u s
e l a b o r a t i o n s , a l s o t h e p r e c i s e c o m p l e x i t y c a n b e b r o u g h t c l o s e t o t h e o p t i -
m u m N l o g
2
N ? N l o g
2
e +
1
2
l o g
2
N + O ( 1 ) , w i t h r e s p e c t t o t h e n u m b e r o f
c o m p a r i s o n s 9 ] . A n a t u r a l q u e s t i o n i s , w h e t h e r t h e n u m b e r o f m o v e s c a n
b e r e d u c e d f r o m " N l o g
2
N , w h i l e k e e p i n g t h e n u m b e r o f c o m p a r i s o n s t h e
s a m e .
O u r s t a r t i n g p o i n t w a s a b i t d i e r e n t f r o m t h e u s u a l a p p r o a c h e s : W e d i d
n o t t r y t o s o l v e t h e i n - p l a c e m e r g i n g p r o b l e m b u t w e n t d i r e c t l y t o s o l v e
t h e s o r t i n g p r o b l e m . S i n c e m e r g i n g i s s t a b l e b y n a t u r e , i t i s f a i r t o a s k ,
-
8/18/2019 Sorting Practical in Place Mergsort
13/13
1 3
w h e t h e r i t w o u l d b e p o s s i b l e t o d e v e l o p a p o w e r f u l s t a b l e i n - p l a c e m e r g e s o r t
a l g o r i t h m t h a t i s m o r e e c i e n t t h a n t h o s e b a s e d o n s t a b l e i n - p l a c e m e r g i n g .
R e f e r e n c e s
1 ] B e n t l e y J . L . a n d M c I l r o y M . D . , E n g i n e e r i n g a s o r t f u n c t i o n , S o f t w a r e | P r a c t i c e
a n d E x p e r i e n c e 2 3 ( 1 9 9 3 ) 1 2 4 9 { 1 2 6 5 .
2 ] C a r l s s o n S . , A v a r i a n t o f H E A P S O R T w i t h a l m o s t o p t i m a l n u m b e r o f c o m p a r i s o n s ,
I n f o r m . P r o c e s s . L e t t . 2 4 ( 1 9 8 7 ) 2 4 7 { 2 5 0 .
3 ] C a r l s s o n S . , A n o t e o n H E A P S O R T , C o m p u t . J . 3 5 ( 1 9 9 2 ) 4 1 0 { 4 1 1 .
4 ] C a r l s s o n S . , K a t a j a i n e n J . , a n d T e u h o l a J . , I n - p l a c e l i n e a r p r o b i n g s o r t , P r o c . o f
t h e 9 t h S y m p o s i u m o n T h e o r e t i c a l A s p e c t s o f C o m p u t e r S c i e n c e , L e c t u r e N o t e s i n
C o m p u t . S c i . 5 7 7 , S p r i n g e r - V e r l a g ( 1 9 9 2 ) , p p . 5 8 1 { 5 8 7 .
5 ] F l e i s c h e r R . , A t i g h t l o w e r b o u n d f o r t h e w o r s t c a s e o f B o t t o m - U p H e a p s o r t , P r o c .
o f t h e 2 n d I n t e r n a t i o n a l S y m p o s i u m o n A l g o r i t h m s , L e c t u r e N o t e s i n C o m p u t . S c i .
5 5 7 , S p r i n g e r - V e r l a g ( 1 9 9 1 ) , p p . 2 5 1 { 2 6 2 .
6 ] F l o y d R . W . , T r e e s o r t 3 ( A l g o r i t h m 2 4 5 ) , C o m m . A C M 7 ( 1 9 6 4 ) 7 0 1 .
7 ] H u a n g B . - C . a n d L a n g s t o n M . A . , P r a c t i c a l i n - p l a c e m e r g i n g , C o m m . A C M 3 1 ( 1 9 8 8 )
3 4 8 { 3 5 2 .
8 ] H w a n g F . K . a n d L i n S . , A s i m p l e a l g o r i t h m f o r m e r g i n g t w o d i s j o i n t l i n e a r l y o r d e r e d
s e t s , S I A M J . C o m p u t . 1 ( 1 9 7 2 ) 3 1 { 3 9 .
9 ] K n u t h D . E . , T h e A r t o f C o m p u t e r P r o g r a m m i n g , V o l . 3 : S o r t i n g a n d S e a r c h i n g
A d d i s o n - W e s l e y , 1 9 7 3 .
1 0 ] K r o n r o d M . A . , O p t i m a l o r d e r i n g a l g o r i t h m w i t h o u t o p e r a t i o n a l e l d , S o v i e t M a t h .
D o k l . 1 0 ( 1 9 6 9 ) 7 4 4 { 7 4 6 .
1 1 ] M u n r o J . I . a n d R a m a n V . , S o r t i n g w i t h m i n i m u m d a t a m o v e m e n t , J . A l g o r i t h m s 1 3
( 1 9 9 2 ) 3 7 4 - 3 9 3 .
1 2 ] P a s a n e n T . , L a j i t t e l u m i n i m i t i l a s s a , M . S c . T h e s i s , D e p t . o f C o m p u t e r S c i e n c e , U n i v .
o f T u r k u , T u r k u , F i n l a n d , 1 9 9 3 .
1 3 ] R a m a n V . , S o r t i n g i n - p l a c e w i t h m i n i m u m d a t a m o v e m e n t , P h . D . D i s s e r t a t i o n , D e p t .
o f C o m p u t e r S c i e n c e , U n i v . o f W a t e r l o o , W a t e r l o o , C a n a d a , 1 9 9 1 .
1 4 ] R e i n h a r d t K . , S o r t i n g i n - p l a c e w i t h a w o r s t c a s e c o m p l e x i t y o f n l o g n ? 1 3 n + O ( l o g n )
c o m p a r i s o n s a n d " n l o g n + O ( 1 ) t r a n s p o r t s , P r o c . o f t h e 3 r d I n t e r n a t i o n a l S y m p o s i u m
o n A l g o r i t h m s a n d C o m p u t a t i o n , L e c t u r e N o t e s i n C o m p u t . S c i . 6 5 0 , S p r i n g e r - V e r l a g
( 1 9 9 2 ) , p p . 4 8 9 { 4 9 8 .
1 5 ] S a l o w e J . a n d S t e i g e r W . , S i m p l i e d s t a b l e m e r g i n g t a s k s , J . A l g o r i t h m s 8 ( 1 9 8 7 )
5 5 7 { 5 7 1 .
1 6 ] S c h a e r R . a n d S e d g e w i c k R . , T h e a n a l y s i s o f H e a p s o r t , J . A l g o r i t h m s 1 5 ( 1 9 9 3 )
7 6 { 1 0 0 .
1 7 ] T r a b b P a r d o L . , S t a b l e s o r t i n g a n d m e r g i n g w i t h o p t i m a l s p a c e a n d t i m e b o u n d s ,
S I A M J . C o m p u t . 6 ( 1 9 7 7 ) 3 5 1 - 3 7 2 .
1 8 ] W e g e n e r I . , B O T T O M - U P - H E A P S O R T , a n e w v a r i a n t o f H E A P S O R T b e a t i n g , o n
a n a v e r a g e , Q U I C K S O R T ( i f n i s n o t v e r y s m a l l ) , T h e o r e t . C o m p u t . S c i . 1 1 8 ( 1 9 9 3 )
8 1 { 9 8 .
1 9 ] W i l l i a m s J . W . J . , H e a p s o r t ( A l g o r i t h m 2 3 2 ) , C o m m . A C M 7 ( 1 9 6 4 ) 3 4 7 { 3 4 8 .