s o r t i n g w e i s s c h a p t e r 8 1 8 3 s o r t i n
play

S o r t i n g ( W e i s s c h a p t e r 8 . 1 - PowerPoint PPT Presentation

S o r t i n g ( W e i s s c h a p t e r 8 . 1 8 . 3 ) S o r t i n g 5 3 9 2 8 7 3 2 1 4 1 2 2 3 3 4 5 7 8 9 Z i l l i o n s o f s o r t i n g a l g o r i t h m s (


  1. S o r t i n g ( W e i s s c h a p t e r 8 . 1 – 8 . 3 )

  2. S o r t i n g 5 3 9 2 8 7 3 2 1 4 1 2 2 3 3 4 5 7 8 9 Z i l l i o n s o f s o r t i n g a l g o r i t h m s ( b u b b l e s o r t , i n s e r t i o n s o r t , s e l e c t i o n s o r t , q u i c k s o r t , h e a p s o r t , m e r g e s o r t , s h e l l s o r t , c o u n t i n g s o r t , r a d i x s o r t , … )

  3. S o r t i n g Wh y i s s o r t i n g i m p o r t a n t ? B e c a u s e s o r t e d d a t a i s m u c h e a s i e r t o d e a l w i t h ! ● S e a r c h i n g – u s e b i n a r y i n s t e a d o f l i n e a r s e a r c h ● F i n d i n g d u p l i c a t e s – t a k e s l i n e a r i n s t e a d o f q u a d r a t i c t i m e ● e t c . Mo s t s o r t i n g a l g o r i t h m s a r e b a s e d o n c o m p a r i s o n s ● C o m p a r e e l e m e n t s – i s o n e b i g g e r t h a n t h e o t h e r ? I f n o t , d o s o m e t h i n g a b o u t i t ! ● A d v a n t a g e : t h e y c a n w o r k o n a l l s o r t s o f d a t a ● D i s a d v a n t a g e : s p e c i a l i s e d a l g o r i t h m s f o r e . g . s o r t i n g l i s t s o f i n t e g e r s c a n b e f a s t e r

  4. B u b b l e s o r t G o t h r o u g h t h e a r r a y , c o m p a r i n g a d j a c e n t e l e m e n t s ● I f w e fj n d t w o t h a t a r e i n t h e w r o n g o r d e r , s w a p t h e m O n c e w e r e a c h t h e e n d o f t h e a r r a y , g o b a c k a n d s t a r t a g a i n !

  5. B u b b l e s o r t C o m p a r e a n d : a [ 0 ] a [ 1 ] 9 2 8 5 3 3 5 9 2 8

  6. B u b b l e s o r t C o m p a r e a n d : a [ 1 ] a [ 2 ] 3 5 2 8 9 3 5 9 2 8

  7. B u b b l e s o r t C o m p a r e a n d : a [ 2 ] a [ 3 ] 3 5 9 8 2 3 5 2 9 8

  8. B u b b l e s o r t C o m p a r e a n d : a [ 3 ] a [ 4 ] 3 5 2 9 8 3 5 2 8 9

  9. B u b b l e s o r t B a c k t o t h e b e g i n n i n g ! 2 8 9 3 5 3 5 2 8 9

  10. B u b b l e s o r t C o m p a r e a n d : a [ 1 ] a [ 2 ] 3 5 8 9 2 3 2 5 8 9

  11. B u b b l e s o r t C o m p a r e a n d : a [ 2 ] a [ 3 ] 3 2 5 9 8 3 2 5 8 9

  12. B u b b l e s o r t C o m p a r e a n d : a [ 3 ] a [ 4 ] 3 2 5 8 9 3 2 5 8 9

  13. B u b b l e s o r t B a c k t o t h e b e g i n n i n g ! 5 8 9 3 2 2 3 5 8 9

  14. B u b b l e s o r t H o w d o w e k n o w w h e n t o s t o p g o i n g b a c k t o t h e b e g i n n i n g ? ● Wh e n t h e a r r a y i s s o r t e d H o w m a n y l o o p s u n t i l t h a t h a p p e n s ? ● E a c h t i m e w e l o o p t h r o u g h t h e a r r a y , a t l e a s t o n e m o r e e l e m e n t e n d s u p i n t h e r i g h t p l a c e : t h e b i g g e s t e l e m e n t t h a t w a s i n t h e w r o n g p l a c e b e f o r e S o r e p e a t a s m a n y t i m e s a s t h e r e a r e e l e m e n t s i n t h e i n p u t a r r a y f o r k = 0 t o a r r a y . l e n g t h - 1 f o r i = 0 t o a r r a y . l e n g t h - 2 i f a r r a y [ i ] < a r r a y [ i + 1 ] s w a p a r r a y [ i ] a n d a r r a y [ i + 1 ]

  15. I n s e r t i o n s o r t I m a g i n e s o m e o n e i s d e a l i n g y o u c a r d s . Wh e n e v e r y o u g e t a n e w c a r d y o u p u t i t i n t o t h e r i g h t p l a c e i n y o u r h a n d : Tii s i s t h e i d e a o f . i n s e r t i o n s o r t

  16. I n s e r t i o n s o r t S o r t i n g : 5 3 9 2 8 S t a r t b y “ p i c k i n g u p ” t h e 5 : 5

  17. I n s e r t i o n s o r t S o r t i n g : 5 3 9 2 8 Tie n i n s e r t t h e 3 i n t o t h e r i g h t p l a c e : 3 5

  18. I n s e r t i o n s o r t S o r t i n g : 5 3 9 2 8 Tie n t h e 9 : 3 5 9

  19. I n s e r t i o n s o r t S o r t i n g : 5 3 9 2 8 Tie n t h e 2 : 2 3 5 9

  20. I n s e r t i o n s o r t S o r t i n g : 5 3 9 2 8 F i n a l l y t h e 8 : 2 3 5 8 9

  21. C o m p l e x i t y o f i n s e r t i o n s o r t I n s e r t i o n s o r t d o e s n i n s e r t i o n s f o r a n a r r a y o f s i z e n D o e s t h i s m e a n i t i s O ( n ) ? N o ! A n i n s e r t i o n i s n o t c o n s t a n t t i m e . T o i n s e r t i n t o a s o r t e d a r r a y , y o u m u s t m o v e a l l t h e e l e m e n t s u p o n e , w h i c h i s O ( n ) . Tiu s t o t a l i s O ( n ) . 2

  22. I n - p l a c e i n s e r t i o n s o r t Tii s v e r s i o n o f i n s e r t i o n s o r t n e e d s t o m a k e a n e w a r r a y t o h o l d t h e r e s u l 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 i s o n e t h a t d o e s n ' t n e e d t o m a k e t e m p o r a r y a r r a y s ● H a s t h e p o t e n t i a l t o b e m o r e e ffjc i e n t L e t ' s m a k e a n i n - p l a c e i n s e r t i o n s o r t ! B a s i c i d e a : l o o p t h r o u g h t h e a r r a y , a n d i n s e r t e a c h e l e m e n t i n t o t h e p a r t w h i c h i s a l r e a d y s o r t e d

  23. I n - p l a c e i n s e r t i o n s o r t 5 3 9 2 8 Tie fj r s t e l e m e n t o f t h e a r r a y i s s o r t e d : 5 3 9 2 8 Wh i t e b i t : s o r t e d

  24. I n - p l a c e i n s e r t i o n s o r t 5 3 9 2 8 I n s e r t t h e 3 i n t o t h e c o r r e c t p l a c e : 3 5 9 2 8

  25. I n - p l a c e i n s e r t i o n s o r t 3 5 9 2 8 I n s e r t t h e 9 i n t o t h e c o r r e c t p l a c e : 3 5 9 2 8

  26. I n - p l a c e i n s e r t i o n s o r t 3 5 9 2 8 I n s e r t t h e 2 i n t o t h e c o r r e c t p l a c e : 2 3 5 9 8

  27. I n - p l a c e i n s e r t i o n s o r t 2 3 5 9 8 I n s e r t t h e 8 i n t o t h e c o r r e c t p l a c e : 2 3 5 8 9

  28. I n - p l a c e i n s e r t i o n O n e w a y t o d o i t : r e p e a t e d l y s w a p t h e e l e m e n t w i t h i t s n e i g h b o u r o n t h e l e f t , u n t i l i t ' s i n t h e r i g h t p o s i t i o n 2 3 5 9 4 2 3 5 4 9

  29. I n - p l a c e i n s e r t i o n 2 3 5 4 9 2 3 4 5 9 w h i l e n > 0 a n d a r r a y [ n ] > a r r a y [ n - 1 ] s w a p a r r a y [ n ] a n d a r r a y [ n - 1 ] n = n - 1

  30. I n - p l a c e i n s e r t i o n A n i m p r o v e m e n t : i n s t e a d o f s w a p p i n g , m o v e e l e m e n t s u p w a r d s t o m a k e a “ h o l e ” w h e r e w e p u t t h e n e w v a l u e 2 3 5 9 4 2 3 5 9

  31. I n - p l a c e i n s e r t i o n 2 3 5 9 4 2 3 5 9 2 3 5 9

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