r e a l w o r l d s o r t i n g n o t o n e x a m s o r t
play

R e a l - w o r l d s o r t i n g ( n o t o n - PowerPoint PPT Presentation

R e a l - w o r l d s o r t i n g ( n o t o n e x a m ) S o r t i n g a l g o r i t h m s s o f a r Wo r s t c a s e A v e r a g e c a s e B e s t c a s e I n s e r t


  1. R e a l - w o r l d s o r t i n g ( n o t o n e x a m )

  2. S o r t i n g a l g o r i t h m s s o f a r Wo r s t c a s e A v e r a g e c a s e B e s t c a s e I n s e r t i o n 2 2 O ( n ) O ( n ) O ( n ) s o r t Q u i c k s o r t 2 O ( n l o g n ) O ( n l o g n ) O ( n ) Me r g e s o r t O ( n l o g n ) O ( n l o g n ) O ( n l o g n )

  3. S o r t i n g a l g o r i t h m s s o f a r Wo r s t c a s e A v e r a g e c a s e B e s t c a s e I n s e r t i o n 2 O ( n 2 ) O ( n ) O ( n ) s o r t Q u i c k s o r t 2 N o c l e a r w i n n e r . . . O ( n l o g n ) O ( n l o g n ) O ( n ) t h e b e s t a l g o r i t h m s c o m b i n e i d e a s Me r g e s o r t O ( n l o g n ) O ( n l o g n ) O ( n l o g n ) f r o m s e v e r a l

  4. I n t r o s o r t Q u i c k s o r t : f a s t i n p r a c t i c e , b u t O ( n ) 2 w o r s t c a s e I n t r o s o r t : ● S t a r t w i t h Q u i c k s o r t ● I f t h e r e c u r s i o n d e p t h g e t s t o o b i g , s w i t c h t o h e a p s o r t , w h i c h i s O ( n l o g n ) P l u s s t a n d a r d Q u i c k s o r t o p t i m i s a t i o n s : ● C h o o s e p i v o t v i a m e d i a n - o f - t h r e e ● S w i t c h t o i n s e r t i o n s o r t f o r s m a l l a r r a y s U s e d b y e . g . C + + S T L , . N E T , . . .

  5. D u a l - p i v o t q u i c k s o r t I n s t e a d o f o n e p i v o t , p i c k t w o I n s t e a d o f p a r t i t i o n i n g t h e a r r a y i n t o t w o p i e c e s , p a r t i t i o n i t i n t o t h r e e p i e c e s ● I f p i v o t s a r e a n d , t h e n : x y ● e l e m e n t s < x ● e l e m e n t s > x a n d < y ● e l e m e n t s > y S a m e c o m p l e x i t y a s Q u i c k s o r t , b u t f e w e r r e c u r s i v e c a l l s b e c a u s e t h e a r r a y g e t s s p l i t u p q u i c k e r U s e d b y J a v a f o r p r i m i t i v e t y p e s ( i n t , . . . )

  6. T r a d i t i o n a l m e r g e s o r t ( a r e c a p )

  7. 1 3 6 8 4 2 1 3 7 5 s p l i t s p l i t 1 3 6 8 4 2 1 3 7 5 s p l i t s p l i t s p l i t s p l i t 1 3 6 8 4 2 1 3 7 5 1 3 2 1 1 3 6 8 4 2 1 3 7 5

  8. 1 3 6 8 4 2 1 3 7 5 1 3 1 2 1 3 6 4 8 1 2 3 5 7 m e r g e m e r g e m e r g e m e r g e 1 3 4 6 8 1 2 3 5 7 m e r g e m e r g e 1 1 2 3 3 4 5 6 7 8

  9. N a t u r a l m e r g e s o r t T r a d i t i o n a l m e r g e s o r t s p l i t s t h e i n p u t l i s t i n t o b e f o r e m e r g i n g s i n g l e e l e m e n t s e v e r y t h i n g b a c k t o g e t h e r a g a i n B e t t e r i d e a : s p l i t t h e i n p u t i n t o r u n s ● A r u n i s a s e q u e n c e o f i n c r e a s i n g e l e m e n t s ● . . . o r a s e q u e n c e o f d e c r e a s i n g e l e m e n t s ● F i r s t r e v e r s e a l l t h e d e c r e a s i n g r u n s , s o t h e y b e c o m e i n c r e a s i n g ● Tie n m e r g e a l l t h e r u n s t o g e t h e r

  10. 1 3 6 8 4 2 1 3 7 5 s p l i t s p l i t s p l i t s p l i t 1 3 6 8 4 2 1 3 7 5 r e v e r s e 1 2 4 m e r g e m e r g e m e r g e m e r g e 1 1 2 3 4 6 8 3 5 7 m e r g e m e r g e 1 1 2 3 3 4 5 6 7 8

  11. N a t u r a l m e r g e s o r t B i g a d v a n t a g e : O ( n ) t i m e f o r s o r t e d d a t a ● . . . a n d r e v e r s e - s o r t e d d a t a ● . . . a n d “ a l m o s t ” - s o r t e d d a t a C o m p l e x i t y : O ( n l o g r ) , w h e r e r i s t h e n u m b e r o f r u n s i n t h e i n p u t ● . . . w o r s t c a s e , e a c h r u n h a s t w o e l e m e n t s , s o r = n / 2 , s o O ( n l o g n ) U s e d b y G H C

  12. 1368 4 2137 5 s p l i t s p l i t s p l i t s p l i t 1368 4 21 37 5 r e v e r s e T o t a l t i m e i s O ( l o g r ) 124 O ( n l o g r ) ! “ l e v e l s ” m e r g e m e r g e m e r g e m e r g e 1123 4 68 35 7 m e r g e m e r g e 1123 3 4567 8 O ( n ) t i m e p e r l e v e l

  13. T i m s o r t N a t u r a l m e r g e s o r t i s r e a l l y g o o d o n s o r t e d / n e a r l y - s o r t e d d a t a ● Y o u g e t l o n g r u n s s o n o t m a n y m e r g e s t o d o B u t n o t s o g o o d o n r a n d o m d a t a ● S h o r t r u n s s o m a n y m e r g e s I d e a o f T i m s o r t : o n s h o r t r a n d o m p a r t s o f t h e l i s t , s w i t c h t o i n s e r t i o n s o r t H o w t o d e t e c t r a n d o m n e s s ? ● S e v e r a l s h o r t r u n s n e x t t o o n e a n o t h e r

  14. 1 3 6 8 4 2 1 3 7 5 s p l i t s p l i t s p l i t 1 3 6 8 4 2 1 3 7 5 D o n ' t s p l i t 3 7 5 i n t o t w o r u n s , r e v e r s e i t ' s t o o s h o r t ! 1 2 4 m e r g e m e r g e i n s e r t i o n s o r t 1 1 2 3 4 6 8 3 5 7 m e r g e m e r g e 1 1 2 3 3 4 5 6 7 8

  15. T i m s o r t S p e c i fj c a l l y : ● I f w e c o m e a c r o s s a s h o r t r u n , j o i n i t t o g e t h e r w i t h a l l f o l l o w i n g s h o r t r u n s u n t i l w e r e a c h a t h r e s h o l d ● Tie n u s e i n s e r t i o n s o r t o n t h a t p a r t A l s o s o m e o p t i m i s a t i o n s f o r m e r g e : ● Me r g e s m a l l e r r u n s t o g e t h e r fj r s t ● I f t h e m e r g e b e g i n s w i t h s e v e r a l e l e m e n t s f r o m t h e s a m e a r r a y , u s e b i n a r y s e a r c h t o fj n d o u t h o w m a n y a n d t h e n c o p y t h e m a l l i n o n e g o U s e d i n J a v a f o r a r r a y s o f o b j e c t s , P y t h o n h t t p : / / e n . w i k i p e d i a . o r g / w i k i / T i m s o r t

  16. T h e b e s t s o r t i n g a l g o r i t h m ? A g o o d s o r t i n g a l g o r i t h m s h o u l d : ● h a v e O ( n l o g n ) c o m p l e x i t y ● h a v e O ( n ) c o m p l e x i t y o n n e a r l y - s o r t e d d a t a ● b e s i m p l e ● b e i n - p l a c e ● b e s t a b l e ● c a c h e - f r i e n d l y N o a l g o r i t h m s e e m s t o h a v e a l l o f t h e s e !

  17. S u m m a r y N o o n e - s i z e fj t s a l l a n s w e r ● B e s t o v e r a l l c o m p l e x i t y : n a t u r a l m e r g e s o r t ● B u t q u i c k s o r t h a s s m a l l e r c o n s t a n t f a c t o r s D i fg e r e n t a l g o r i t h m s a r e g o o d i n d i fg e r e n t s i t u a t i o n s ● . . . y o u s h o u l d fj n d o u t w h i c h i n t h e l a b : ) B e s t s o r t i n g f u n c t i o n s c o m b i n e i d e a s f r o m s e v e r a l a l g o r i t h m s ● I n t r o s o r t : q u i c k s o r t + h e a p s o r t + i n s e r t i o n s o r t ● T i m s o r t : n a t u r a l m e r g e s o r t + i n s e r t i o n s o r t

  18. S p e c i a l - p u r p o s e s o r t i n g a l g o r i t h ms ( n o t o n e x a m )

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend