Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Algorithms - Sorting & Searching

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.

Algorithms - Sorting & Searching

Overview and visualisation of some sorting and searching algorithms.

Avatar for Rowan Merewood

Rowan Merewood

June 08, 2013
Tweet

More Decks by Rowan Merewood

Other Decks in Programming

Transcript

  1. WHO AM I? Software Engineer and Technical Team Lead $

    i n v i q a G r o u p = [ ' I n v i q a ' , ' S e n s i o L a b s U K ' , ' S e s s i o n D i g i t a l ' ] ; $ s o c i a l = [ ' b l o g ' = > ' h t t p : / / m e r e w o o d . o r g ' , ' f a c e b o o k ' = > $ t h i s - > g r a p h S e a r c h ( ' p e o p l e i a m s t a l k i n g ' ) , ' g i t h u b ' = > ' h t t p s : / / g i t h u b . c o m / r o w a n - m ' , ' g o o g l e + ' = > ' h t t p s : / / p l u s . g o o g l e . c o m / 1 1 1 8 1 3 8 4 5 7 2 7 0 0 5 1 6 0 1 6 4 ' , ' i d e n t i c a ' = > ' h t t p : / / i d e n t i . c a / r o w a n m ' , ' i m d b ' = > ' h t t p : / / i m d b . c o m / n a m e / n m 1 4 1 2 3 4 8 ' , ' t w i t t e r ' = > ' h t t p s : / / t w i t t e r . c o m / r o w a n _ m ' , ] ;
  2. WHY AM I HERE? 1. Because straight-up, pure computer science

    is AWESOME. Also, understanding the lower level detail helps you make better use of the higher level abstractions. 2. ^ E O F
  3. WHY SORT? Displaying lists to humans Categorising data Preparing data

    for merging Preparing data for searching In general: to display or to prepare for another operation.
  4. STABLE UNSTABLE Order of unsorted portion maintained Order of unsorted

    portion may change STABILITY 4 : A 1 : A 1 : B 3 : A 2 : A 3 : B 2 : B 2 : C 1 1 : A 1 : B 2 : A 2 : B 2 : C 3 : A 3 : B 4 : B 2 4 : A 1 : A 1 : B 3 : A 2 : A 3 : B 2 : B 2 : C 1 1 : B 1 : A 2 : C 2 : B 2 : A 3 : A 3 : B 4 : B 2 1 : A 1 : B 2 : A 2 : B 2 : C 3 : A 3 : B 4 : B 3
  5. BIG O NOTATION Rate of growth for resource usage based

    on the size of its input. Resource usage: CPU cycles / time, memory usage Size of input: number of elements
  6. BIG O NOTATION (CONT.) Ο(1): Constant Ο(n): Linear growth Ο(n

    log n): Logarithmic growth Ο(n2): Quadratic growth
  7. INSERTION SORT CODE c l a s s I n

    s e r t i o n S o r t { p u b l i c f u n c t i o n s o r t ( a r r a y $ e l e m e n t s ) { $ i t e r a t i o n s = c o u n t ( $ e l e m e n t s ) ; f o r ( $ i n d e x = 1 ; $ i n d e x < $ i t e r a t i o n s ; $ i n d e x + + ) { $ e l e m e n t T o I n s e r t = $ e l e m e n t s [ $ i n d e x ] ; $ i n s e r t I n d e x = $ i n d e x ; w h i l e ( $ i n s e r t I n d e x > 0 & & $ e l e m e n t T o I n s e r t < $ e l e m e n t s [ $ i n s e r t I n d e x - 1 ] ) { $ e l e m e n t s [ $ i n s e r t I n d e x ] = $ e l e m e n t s [ $ i n s e r t I n d e x - 1 ] ; $ e l e m e n t s [ $ i n s e r t I n d e x - 1 ] = $ e l e m e n t T o I n s e r t ; $ i n s e r t I n d e x - - ; } } r e t u r n $ e l e m e n t s ; } }
  8. INSERTION SORT (CONT.) CODE: ITERATE THROUGH THE LIST p u

    b l i c f u n c t i o n s o r t ( a r r a y $ e l e m e n t s ) { / / A t l e a s t o n e i t e r a t i o n p e r e l e m e n t $ i t e r a t i o n s = c o u n t ( $ e l e m e n t s ) ; f o r ( $ i n d e x = 1 ; $ i n d e x < $ i t e r a t i o n s ; $ i n d e x + + ) { / / I f n o o t h e r v a r i a b l e o p e r a t i o n s h a p p e n h e r e : / / a l g o r i t h m i s O ( n ) } }
  9. INSERTION SORT (CONT.) CODE: COMPARE ELEMENTS If the list is

    in order, the w h i l e loop is not entered. f o r ( $ i n d e x = 1 ; $ i n d e x < $ i t e r a t i o n s ; $ i n d e x + + ) { / / " P i c k u p " t h e c u r r e n t e l e m e n t a n d i t s p o s i t i o n $ e l e m e n t T o I n s e r t = $ e l e m e n t s [ $ i n d e x ] ; $ i n s e r t I n d e x = $ i n d e x ; / / I t e r a t e b a c k t h r o u g h t h e e l e m e n t s / / u n t i l t h e c o r r e c t p o s i t i o n i t r e a c h e d w h i l e ( $ i n s e r t I n d e x > 0 & & $ e l e m e n t T o I n s e r t < $ e l e m e n t s [ $ i n s e r t I n d e x - 1 ] ) { / / S w a p o u t o f o r d e r e l e m e n t s $ e l e m e n t s [ $ i n s e r t I n d e x ] = $ e l e m e n t s [ $ i n s e r t I n d e x - 1 ] ; $ e l e m e n t s [ $ i n s e r t I n d e x - 1 ] = $ e l e m e n t T o I n s e r t ; $ i n s e r t I n d e x - - ; } }
  10. INSERTION SORT (CONT.) ITERATIONS 8 8 6 4 1 4

    6 1 1 6 1 8 9 3 0 1 8 6 5 3 1 8 8 ↪ 6 4 1 4 6 1 1 6 1 8 9 3 0 1 8 6 5 3 2 ↪ 6 4 ⇅ 8 8 ⇅ 1 4 6 1 1 6 1 8 9 3 0 1 8 6 5 3 3 6 4 8 8 ↪ 1 4 6 1 1 6 1 8 9 3 0 1 8 6 5 3 4 6 4 8 8 1 4 6 ↪ 1 1 6 1 8 9 3 0 1 8 6 5 3 5 6 4 8 8 ↪ 1 1 6 ⇅ 1 4 6 ⇅ 1 8 9 3 0 1 8 6 5 3 6 6 4 8 8 1 1 6 1 4 6 ↪ 1 8 9 3 0 1 8 6 5 3 7
  11. 6 4 8 8 1 1 6 1 4 6

    1 8 9 ↪ 3 0 1 8 6 5 3 8 6 4 8 8 1 1 6 1 4 6 ↪ 3 0 ⇅ 1 8 9 ⇅ 1 8 6 5 3 9 6 4 8 8 1 1 6 ↪ 3 0 ⇅ 1 4 6 ⇅ 1 8 9 1 8 6 5 3 1 0 6 4 8 8 ↪ 3 0 ⇅ 1 1 6 ⇅ 1 4 6 1 8 9 1 8 6 5 3 1 1 6 4 ↪ 3 0 ⇅ 8 8 ⇅ 1 1 6 1 4 6 1 8 9 1 8 6 5 3 1 2 ↪ 3 0 ⇅ 6 4 ⇅ 8 8 1 1 6 1 4 6 1 8 9 1 8 6 5 3 1 3 3 0 6 4 8 8 1 1 6 1 4 6 1 8 9 ↪ 1 8 6 5 3 1 4 3 0 6 4 8 8 1 1 6 1 4 6 ↪ 1 8 6 ⇅ 1 8 9 ⇅ 5 3 1 5 3 0 6 4 8 8 1 1 6 1 4 6 1 8 6 1 8 9 ↪ 5 3 1 6 3 0 6 4 8 8 1 1 6 1 4 6 1 8 6 ↪ 5 3 ⇅ 1 8 9 ⇅ 1 7 3 0 6 4 8 8 1 1 6 1 4 6 ↪ 5 3 ⇅ 1 8 6 ⇅ 1 8 9 1 8 3 0 6 4 8 8 1 1 6 ↪ 5 3 ⇅ 1 4 6 ⇅ 1 8 6 1 8 9 1 9 3 0 6 4 8 8 ↪ 5 3 ⇅ 1 1 6 ⇅ 1 4 6 1 8 6 1 8 9 2 0 3 0 6 4 ↪ 5 3 ⇅ 8 8 ⇅ 1 1 6 1 4 6 1 8 6 1 8 9 2 1
  12. Sorted! 3 0 ↪ 5 3 ⇅ 6 4 ⇅

    8 8 1 1 6 1 4 6 1 8 6 1 8 9 2 2 3 0 ↪ 5 3 6 4 8 8 1 1 6 1 4 6 1 8 6 1 8 9 2 3
  13. INSERTION SORT (CONT.) SUMMARY Best case: Ο(n) Average / worst

    case: Ο(n2) Memory usage: Ο(n) Adaptive, stable, in place, and on line(n)
  14. BUBBLE SORT CODE c l a s s B u

    b b l e S o r t { p u b l i c f u n c t i o n s o r t ( a r r a y $ e l e m e n t s ) { f o r ( $ i n d e x = c o u n t ( $ e l e m e n t s ) ; $ i n d e x > 0 ; $ i n d e x - - ) { $ s w a p p e d = f a l s e ; f o r ( $ s w a p I n d e x = 0 ; $ s w a p I n d e x < $ i n d e x - 1 ; $ s w a p I n d e x + + ) { i f ( $ e l e m e n t s [ $ s w a p I n d e x ] > $ e l e m e n t s [ $ s w a p I n d e x + 1 ] ) { $ t m p = $ e l e m e n t s [ $ s w a p I n d e x ] ; $ e l e m e n t s [ $ s w a p I n d e x ] = $ e l e m e n t s [ $ s w a p I n d e x + 1 ] ; $ e l e m e n t s [ $ s w a p I n d e x + 1 ] = $ t m p ; $ s w a p p e d = t r u e ; } } i f ( ! $ s w a p p e d ) { r e t u r n $ e l e m e n t s ; } } } }
  15. BUBBLE SORT (CONT.) CODE: ITERATE THROUGH THE LIST / /

    I t e r a t e t h r o u g h t h e e l e m e n t s f o r ( $ i n d e x = c o u n t ( $ e l e m e n t s ) ; $ i n d e x > 0 ; $ i n d e x - - ) { $ s w a p p e d = f a l s e ; / / S w a p o u t o f o r d e r e l e m e n t s / / u n t i l t h e r e ' s n o t h i n g l e f t t o s w a p i f ( ! $ s w a p p e d ) { r e t u r n $ e l e m e n t s ; } }
  16. BUBBLE SORT (CONT.) CODE If the list is in order,

    then $ s w a p p e d stays f a l s e . f o r ( $ i n d e x = c o u n t ( $ e l e m e n t s ) ; $ i n d e x > 0 ; $ i n d e x - - ) { $ s w a p p e d = f a l s e ; / / I t e r a t e t h r o u g h t h e u n s o r t e d p o r t i o n o f t h e l i s t f o r ( $ s w a p I n d e x = 0 ; $ s w a p I n d e x < $ i n d e x - 1 ; $ s w a p I n d e x + + ) { / / C o m p a r e a n d s w a p e l e m e n t s i f ( $ e l e m e n t s [ $ s w a p I n d e x ] > $ e l e m e n t s [ $ s w a p I n d e x + 1 ] ) { $ t m p = $ e l e m e n t s [ $ s w a p I n d e x ] ; $ e l e m e n t s [ $ s w a p I n d e x ] = $ e l e m e n t s [ $ s w a p I n d e x + 1 ] ; $ e l e m e n t s [ $ s w a p I n d e x + 1 ] = $ t m p ; $ s w a p p e d = t r u e ; } } i f ( ! $ s w a p p e d ) { r e t u r n $ e l e m e n t s ; } }
  17. BUBBLE SORT (CONT.) ITERATIONS 2 5 4 3 4 5

    1 9 1 2 9 8 1 4 5 3 4 2 2 2 6 2 1 2 1 2 5 4 3 4 5 1 9 1 2 9 8 1 4 5 3 4 2 2 2 6 ↪ 2 1 2 2 ↪ 2 5 4 3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 1 9 1 2 2 6 ↪ 2 1 2 2 5 4 2 9 8 3 4 2 3 4 5 3 4 1 4 5 1 9 1 ↪ 2 2 6 ↪ 2 1 2 2 5 4 2 9 8 3 4 2 3 4 5 3 5
  20. Sorted! 1 4 5 1 9 1 2 1 2

    ⇅ ↪ 2 2 6 ⇅ 2 5 4 2 9 8 3 4 2 3 4 5 3 6 1 4 5 1 9 1 ↪ 2 1 2 2 2 6 2 5 4 2 9 8 3 4 2 3 4 5 3 7 ↪ 1 4 5 1 9 1 ↪ 2 1 2 2 2 6 2 5 4 2 9 8 3 4 2 3 4 5 3 8 1 4 5 ↪ 1 9 1 ↪ 2 1 2 2 2 6 2 5 4 2 9 8 3 4 2 3 4 5 3 9 1 4 5 1 9 1 ↪ 2 1 2 2 2 6 2 5 4 2 9 8 3 4 2 3 4 5 4 0
  21. BUBBLE SORT (CONT.) THE UGLY KNUTH “The bubble sort seems

    to have nothing to recommend it, except a catchy name and the fact that it leads to some interesting theoretical problems”
  22. QUICK SORT CODE c l a s s Q u

    i c k S o r t { p u b l i c f u n c t i o n s o r t ( a r r a y $ e l e m e n t s ) { $ t h i s - > d o Q u i c k S o r t ( $ e l e m e n t s , 0 , c o u n t ( $ e l e m e n t s ) - 1 ) ; r e t u r n $ e l e m e n t s ; } f u n c t i o n d o Q u i c k S o r t ( $ e l e m e n t s , $ l e f t I n d e x , $ r i g h t I n d e x ) { / / D i v i d e t h e a r r a y i n t w o , c r e a t i n g a “ p i v o t ” v a l u e / / M o v e a n y v a l u e l o w e r t h a n t h e p i v o t t o t h e l e f t a r r a y / / M o v e a n y v a l u e h i g h e r t h a n t h e p i v o t t o t h e r i g h t a r r a y / / R e c u r s i v e l y r e p e a t t h e s a m e o p e r a t i o n o n b o t h a r r a y s } }
  23. QUICK SORT (CONT.) CODE: CREATE A PIVOT f u n

    c t i o n d o Q u i c k S o r t ( $ e l e m e n t s , $ l e f t I n d e x , $ r i g h t I n d e x ) { / / D i v i d e t h e a r r a y i n t w o , c r e a t i n g a “ p i v o t ” v a l u e $ p i v o t I n d e x = c e i l ( $ l e f t I n d e x + ( ( $ r i g h t I n d e x - $ l e f t I n d e x ) / 2 ) ) ; $ p i v o t E l e m e n t = $ e l e m e n t s [ $ p i v o t I n d e x ] ; $ l e f t S w a p I n d e x = $ l e f t I n d e x ; $ r i g h t S w a p I n d e x = $ r i g h t I n d e x ; w h i l e ( $ l e f t S w a p I n d e x < = $ r i g h t S w a p I n d e x ) { / / M o v e t h e l e f t i n d e x u n t i l w e f i n d a n o u t o f o r d e r e l e m e n t / / M o v e t h e r i g h t i n d e x u n t i l w e f i n d a n o u t o f o r d e r e l e m e n t / / S w a p t h e m } }
  24. QUICK SORT (CONT.) CODE: SWAP VALUES w h i l

    e ( $ l e f t S w a p I n d e x < = $ r i g h t S w a p I n d e x ) { / / M o v e t h e l e f t i n d e x u n t i l w e f i n d a n o u t o f o r d e r e l e m e n t w h i l e ( $ e l e m e n t s [ $ l e f t S w a p I n d e x ] < $ p i v o t E l e m e n t ) { $ l e f t S w a p I n d e x + + ; } / / M o v e t h e r i g h t i n d e x u n t i l w e f i n d a n o u t o f o r d e r e l e m e n t w h i l e ( $ e l e m e n t s [ $ r i g h t S w a p I n d e x ] > $ p i v o t E l e m e n t ) { $ r i g h t S w a p I n d e x - - ; } / / S w a p t h e m i f ( $ l e f t S w a p I n d e x < = $ r i g h t S w a p I n d e x ) { $ t m p = $ e l e m e n t s [ $ l e f t S w a p I n d e x ] ; $ e l e m e n t s [ $ l e f t S w a p I n d e x ] = $ e l e m e n t s [ $ r i g h t S w a p I n d e x ] ; $ e l e m e n t s [ $ r i g h t S w a p I n d e x ] = $ t m p ; $ l e f t S w a p I n d e x + + ; $ r i g h t S w a p I n d e x - - ; } }
  25. QUICK SORT (CONT.) CODE: RECURSE f u n c t

    i o n d o Q u i c k S o r t ( $ e l e m e n t s , $ l e f t I n d e x , $ r i g h t I n d e x ) { / / D i v i d e t h e a r r a y i n t w o , c r e a t i n g a “ p i v o t ” v a l u e / / M o v e a n y v a l u e l o w e r t h a n t h e p i v o t t o t h e l e f t a r r a y / / M o v e a n y v a l u e h i g h e r t h a n t h e p i v o t t o t h e r i g h t a r r a y / / R e c u r s i v e l y r e p e a t t h e s a m e o p e r a t i o n o n b o t h a r r a y s i f ( $ l e f t I n d e x < $ r i g h t S w a p I n d e x ) { $ t h i s - > d o Q u i c k S o r t ( $ e l e m e n t s , $ l e f t I n d e x , $ r i g h t S w a p I n d e x ) ; } i f ( $ l e f t S w a p I n d e x < $ r i g h t I n d e x ) { $ t h i s - > d o Q u i c k S o r t ( $ e l e m e n t s , $ l e f t S w a p I n d e x , $ r i g h t I n d e x ) ; } }
  26. QUICK SORT (CONT.) ITERATIONS 1 9 3 2 0 3

    1 0 2 5 2 6 4 4 2 2 4 4 2 8 2 1 ↪ 1 9 3 2 0 3 1 0 2 5 ↪ 2 6 4 4 2 2 4 4 ↪ 2 8 2 2 1 9 3 2 0 3 1 0 2 5 ↪ 2 6 4 4 2 ↪ 2 4 4 2 8 2 3 1 9 3 2 0 3 1 0 2 5 ↪ 2 4 4 ⇅ 4 2 ↪ 2 6 4 ⇅ 2 8 2 4 ↪ 1 9 3 2 0 3 1 0 2 ↪ 5 2 4 4 ↪ 4 2 2 6 4 2 8 2 5 ↪ 1 9 3 2 0 3 1 0 2 ↪ 5 2 4 4 4 2 2 6 4 2 8 2 6 ↪ 5 ⇅ 2 0 3 1 0 2 ↪ 1 9 3 ⇅ 2 4 4 4 2 2 6 4 2 8 2 7
  27. Sorted! 5 ↪ 2 0 3 1 0 2 ↪

    1 9 3 2 4 4 ↪ 4 2 2 6 4 2 8 2 8 5 ↪ 2 0 3 1 0 2 ↪ 1 9 3 2 4 4 ↪ 4 2 2 6 4 2 8 2 9 5 ↪ 4 2 ⇅ 1 0 2 ↪ 1 9 3 2 4 4 ↪ 2 0 3 ⇅ 2 6 4 2 8 2 1 0 5 4 2 1 0 2 ↪ 1 9 3 2 4 4 2 0 3 2 6 4 2 8 2 1 1 5 4 2 1 0 2 ↪ 1 9 3 2 4 4 2 0 3 2 6 4 2 8 2 1 2 5 ↪ 4 2 ↪ 1 0 2 1 9 3 2 4 4 2 0 3 2 6 4 2 8 2 1 3 5 4 2 ↪ 1 0 2 1 9 3 2 4 4 2 0 3 2 6 4 2 8 2 1 4 5 4 2 ↪ 1 0 2 1 9 3 2 4 4 2 0 3 2 6 4 2 8 2 1 5 5 4 2 1 0 2 1 9 3 ↪ 2 4 4 ↪ 2 0 3 2 6 4 2 8 2 1 6 5 4 2 1 0 2 1 9 3 ↪ 2 4 4 ↪ 2 0 3 2 6 4 2 8 2 1 7 5 4 2 1 0 2 1 9 3 ↪ 2 0 3 ⇅ ↪ 2 4 4 ⇅ 2 6 4 2 8 2 1 8 5 4 2 1 0 2 1 9 3 2 0 3 2 4 4 ↪ 2 6 4 ↪ 2 8 2 1 9 5 4 2 1 0 2 1 9 3 2 0 3 2 4 4 2 6 4 ↪ 2 8 2 2 0 5 4 2 1 0 2 1 9 3 2 0 3 2 4 4 2 6 4 ↪ 2 8 2 2 1
  28. QUICK SORT (CONT.) SUMMARY Best / average case: Ο(n log

    n) Worst case: Ο(n2) Implemented by s o r t ( ) Easily parallelized
  29. HEAP SORT CODE c l a s s H e

    a p S o r t { p u b l i c f u n c t i o n s o r t ( a r r a y $ e l e m e n t s ) { $ s i z e = c o u n t ( $ e l e m e n t s ) ; f o r ( $ i n d e x = f l o o r ( ( $ s i z e / 2 ) ) - 1 ; $ i n d e x > = 0 ; $ i n d e x - - ) { $ e l e m e n t s = $ t h i s - > s i f t D o w n ( $ e l e m e n t s , $ i n d e x , $ s i z e ) ; } f o r ( $ i n d e x = $ s i z e - 1 ; $ i n d e x > = 1 ; $ i n d e x - - ) { $ t m p = $ e l e m e n t s [ 0 ] ; $ e l e m e n t s [ 0 ] = $ e l e m e n t s [ $ i n d e x ] ; $ e l e m e n t s [ $ i n d e x ] = $ t m p ; $ e l e m e n t s = $ t h i s - > s i f t D o w n ( $ e l e m e n t s , 0 , $ i n d e x - 1 ) ; } r e t u r n $ e l e m e n t s ; } }
  30. HEAP SORT (CONT.) CODE: SIFT THE HEAP p u b

    l i c f u n c t i o n s i f t D o w n ( a r r a y $ e l e m e n t s , $ r o o t , $ b o t t o m ) { $ d o n e = f a l s e ; w h i l e ( ( $ r o o t * 2 < = $ b o t t o m ) & & ( ! $ d o n e ) ) { i f ( $ r o o t * 2 = = $ b o t t o m ) $ m a x C h i l d = $ r o o t * 2 ; e l s e i f ( $ e l e m e n t s [ $ r o o t * 2 ] > $ e l e m e n t s [ $ r o o t * 2 + 1 ] ) $ m a x C h i l d = $ r o o t * 2 ; e l s e $ m a x C h i l d = $ r o o t * 2 + 1 ; i f ( $ e l e m e n t s [ $ r o o t ] < $ e l e m e n t s [ $ m a x C h i l d ] ) { $ t m p = $ e l e m e n t s [ $ r o o t ] ; $ e l e m e n t s [ $ r o o t ] = $ e l e m e n t s [ $ m a x C h i l d ] ; $ e l e m e n t s [ $ m a x C h i l d ] = $ t m p ; $ r o o t = $ m a x C h i l d ; } e l s e $ d o n e = t r u e ; } r e t u r n $ e l e m e n t s ; }
  31. HEAP SORT (CONT.) ITERATIONS 1 4 7 1 4 6

    6 1 2 3 3 2 9 7 2 6 6 3 1 2 2 9 2 1 1 4 7 1 4 6 6 1 ↪ 2 3 3 2 9 7 2 6 6 ↪ 3 1 2 2 9 2 2 1 4 7 1 4 6 6 1 ↪ 3 1 2 ⇅ 2 9 7 2 6 6 ↪ 2 3 3 ⇅ 2 9 2 3 1 4 7 1 4 6 ↪ 6 1 3 1 2 ↪ 2 9 7 2 6 6 2 3 3 2 9 2 4 1 4 7 1 4 6 ↪ 2 9 7 ⇅ 3 1 2 ↪ 6 1 ⇅ 2 6 6 2 3 3 2 9 2 5 1 4 7 ↪ 1 4 6 2 9 7 ↪ 3 1 2 6 1 2 6 6 2 3 3 2 9 2 6 1 4 7 ↪ 3 1 2 ⇅ 2 9 7 ↪ 1 4 6 ⇅ 6 1 2 6 6 2 3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MMARY Best / average / worst case:

    Ο(n log n) Implemented by S p l M i n H e a p ( ) $ h = n e w S p l M i n H e a p ( ) ; f o r e a c h ( $ u n s o r t e d a s $ v a l ) $ h - > i n s e r t ( $ v a l ) ; $ h - > t o p ( ) ; w h i l e ( $ h - > v a l i d ( ) ) { e c h o $ h - > c u r r e n t ( ) . " \ n " ; $ h - > n e x t ( ) ; }
  37. SEQUENTIAL SEARCH CODE c l a s s S e

    q u e n t i a l S e a r c h { p u b l i c f u n c t i o n s e a r c h ( $ t a r g e t , a r r a y $ e l e m e n t s ) { $ i t e r a t i o n s = c o u n t ( $ e l e m e n t s ) ; f o r ( $ i n d e x = 0 ; $ i n d e x < = $ i t e r a t i o n s ; $ i n d e x + + ) { i f ( $ t a r g e t = = $ e l e m e n t s [ $ i n d e x ] ) { $ t h i s - > n o t i f y O b s e r v e r s ( a r r a y ( $ i n d e x ) ) ; r e t u r n t r u e ; } } r e t u r n f a l s e ; } }
  38. SEQUENTIAL SEARCH (CONT.) ITERATIONS Found you! 1 8 ↪ 6

    1 8 3 4 5 5 1 7 1 1 8 8 3 1 0 1 1 8 6 ✘ ↪ 1 8 3 4 5 5 1 7 1 1 8 8 3 1 0 2 1 8 6 ✘ ↪ 1 8 ✔ 3 4 5 5 1 7 1 1 8 8 3 1 0 3
  39. SEQUENTIAL SEARCH (CONT.) SUMMARY Best case: Ο(1) Average / Worst

    case: Ο(n) Not as pointless as it looks...
  40. BINARY SEARCH CODE c l a s s B i

    n a r y S e a r c h { p u b l i c f u n c t i o n s e a r c h ( $ t a r g e t , a r r a y $ e l e m e n t s ) { r e t u r n $ t h i s - > d o B i n a r y S e a r c h ( $ t a r g e t , $ e l e m e n t s , 0 , c o u n t ( $ e l e m e n t s ) ) ; } p u b l i c f u n c t i o n d o B i n a r y S e a r c h ( $ t a r g e t , a r r a y $ e l e m e n t s , $ m i n I n d e x , $ m a x I n d e x ) { i f ( $ m a x I n d e x < $ m i n I n d e x ) { r e t u r n f a l s e ; } $ m i d I n d e x = f l o o r ( ( $ m i n I n d e x + $ m a x I n d e x ) / 2 ) ; i f ( $ e l e m e n t s [ $ m i d I n d e x ] > $ t a r g e t ) { r e t u r n $ t h i s - > d o B i n a r y S e a r c h ( $ t a r g e t , $ e l e m e n t s , $ m i n I n d e x , $ m i d I n d e x - 1 ) ; } i f ( $ e l e m e n t s [ $ m i d I n d e x ] < $ t a r g e t ) { r e t u r n $ t h i s - > d o B i n a r y S e a r c h ( $ t a r g e t , $ e l e m e n t s , $ m i d I n d e x + 1 , $ m a x I n d e x ) ; } r e t u r n t r u e ; } }
  41. Found you! 5 6 5 2 5 6 1 2

    8 1 5 6 1 6 5 1 8 2 ↪ 2 3 6 2 5 6 2 7 4 3 1 0 3 1 0 3 2 8 3 3 0 1 5 6 5 2 5 6 ↪ 1 2 8 1 5 6 1 6 5 1 8 2 2 3 6 ✘ 2 5 6 2 7 4 3 1 0 3 1 0 3 2 8 3 3 0 2 5 6 ↪ 5 2 5 6 1 2 8 ✘ 1 5 6 1 6 5 1 8 2 2 3 6 ✘ 2 5 6 2 7 4 3 1 0 3 1 0 3 2 8 3 3 0 3 5 6 5 2 ✘ ↪ 5 6 1 2 8 ✘ 1 5 6 1 6 5 1 8 2 2 3 6 ✘ 2 5 6 2 7 4 3 1 0 3 1 0 3 2 8 3 3 0 4 5 6 5 2 ✘ ↪ 5 6 ✔ 1 2 8 ✘ 1 5 6 1 6 5 1 8 2 2 3 6 ✘ 2 5 6 2 7 4 3 1 0 3 1 0 3 2 8 3 3 0 5
  42. BINARY SEARCH (CONT.) SUMMARY Best case: Ο(1) Average / Worst

    case: Ο(log n) Switch to linear search for smaller partitions
  43. HISTORY Insertion Sort: optimised in 1959 (Shell Sort) Bubble Sort:

    improved in 1980 (Comb Sort) Quick Sort: developed in 1960 (C. A. R. Hoare) Heap Sort: improved in the '60s (Robert Floyd) Oh, and there's Radix Sort used by Herman Hollerith in 1887