ࡱ> Ff  !"#$%&'()*+,-./0123456789:;<=>?@ABCDEdIJKLMNOPQRSTUVWXYZ[\]^_`abceRdO)GPowerPoint Document(SummaryInformation(HX6DocumentSummaryInformation8,A(   F6"Wagner@cs.ucr.eduN:http://www.cs.ucr.edu/~wagnerH4http://www.cs.ucr.edu/cs12>zhttp://ciips.ee.uwa.edu.au/~morris/Year2/PLDS210/sorting.html/ 0DTimes New Romanla 'K DArialNew Romanla 'K  DWingdingsRomanla 'K 0DTekton Plus RegularSC'K @DBillsDingbatsgularSC'K PDProFontgbatsgularSC'K  ` .  @n?" dd@  @@``  Hq       c $ n3f3@8 >$ʚ;+4ʚ;g4UdUd@t@ppp@ <4!d!d-gX<4dddd-gXg4`d`d4@@8p@ pp^___PPT9@>h___PPT2001D<4X?-"2003 W L Truppel bCS 14: Intro. Data Structures & Algs. " Lecture 5O =$3%Intro to Data Structures & AlgorithmsWagner Truppel Lecturer, Dept. of Computer Science & Engineering UC Riverside wagner@cs.ucr.edu http://www.cs.ucr.edu/~wagner http://www.cs.ucr.edu/cs14zO  O`a~Today s TopicstADT Counter review ADT Array Sorting Magical Sorting Dumb Sorting Bubble Sorting Selection Sorting Insertion Sorting&%P%PmADT Counter reviewadt Counter //  filename uses Integer // other adt s used defines Counter // name of type defined operations get: Counter int //  observer inc: Counter Counter //  modifier new: Counter //  constructor preconditions // none for this adt axioms //  contract get( new() ) = 0 // new starts at 0 get( inc(c) ) = get(c) + 1 // inc s by 1~z& Z-L}at  ADT ArrayOk& what s an array really like ? What do we need ? It s a collection of elements of some type, so we need a type. Call it T. We need to be able to refer to each element, so we need some kind of index& an integer will do just fine. We need to be able to retrieve and set the values stored in the array, so we need get and put functions But if we re going to search through the array to find some element, we need to know when to stop& we need an array lengthZm Qf  ADT Arrayadt Array uses Integer, Any defines Array // Any supports == operations new: Integer x T -/-> Array get: Array x Integer -/-> T put: Array x Integer x T -/-> Array length: Array ---> Integer$0  ADT Array preconditions new(l, t): l > 0 get(a, i): 0 <= i < length(a) put(a, i, t): 0 <= i < length(a) axioms // how to find them ? length( new(l, t) ) = l length(put(a, i, t)) = length(a) get(new(l,t), i) = t get(put(a, i, t), j) = t if i == j, = get(a, j) if i != jZ6  ADT ArrayWith a complete ADT for arrays we can now implement arrays any way we like make sure that our implementations do satisfy the array interface make sure that our implementations do satisfy the array axioms (remember: helps in testing)&** Sorting0Sorting requires additional information Equality operator (==) alone isn t enough That is, it requires support for an additional operation: a comparison operator (<, >, <=, or >=) So we ll define an ADT called Ordered which is basically Any with the additional comparison operatorZ;f )SortingFormal definitions: Ascending for all i in [0, length-2]: a[i] <= a[i+1] 2, 7, 9, 9, 14, 27, 31, 31, 56 Descending for all i in [0, length-2]: a[i] >= a[i+1] 56, 31, 31, 27, 14, 9, 9, 7, 2lZ ZKZ ZJZ K J Magical SortingThe human sorting 12, 4, 7, 3, 9 We  magically sort this array by moving each element to its correct place in a single step: 3, 4, 7, 9, 12 Time complexity: O(1) [ constant time ] Problems with this approach: We can do it by eye; the computer can t do it It seems we have some global view of the array; the computer only sees things locally Even we cannot do it for large arrays\Z{ZEZZ{E Dumb SortingSuppose we have a function bool isAscending(cs14::Array<T> &a) which returns true if the array is in ascending order First, how much time does that function take to execute ? It should take O(n) time Here s an implementation bool isAscending(cs14::Array<T> &a) { for int (i = 0; i < a.length() - 1; i++) { if (a[i] > a[i + 1]) return false; } return true; } ZZZlZZZ$6IP? " Dumb SortingAnother question: how many different possibilities (permutations) are there for an array of n elements ? Example: n = 3, a = { 3, 2, 7 } { 2, 3, 7 }, { 2, 7, 3 } { 3, 2, 7 }, { 3, 7, 2 } { 7, 2, 3 }, { 7, 3, 2 } For n = 3 we have 6 permutations In general, there are n! permutations [ n factorial = n! = n x (n-1) x & x 2 x 1 ]:Z.Z\- Dumb SortingSo here s a sorting algorithm while there are permutations produce the next permutation if IsAscending(a) Break How much time does it take ? There are n! permutations, each taking O(n) time, so the total time complexity is O(n x n!) Worse than O(n!), which is really bad ! The computer could do it, but would take forever;Z/ZZZ;/          [ Sorting extremesSo we have two extremes Magical Sorting O(1), which is awesome but the computer cannot do it Dumb Sorting Worse than O(n!), which is terrible The computer could do it, but it would take forever All other sorting algorithms lie in between these extremesZZ5Z ZYZ;Z      5  ; Bubble SortOn the board& int n = a.length(); for (int i = 0; i < n - 1; i++) for (int j = 0; j < n - 1 - i; j++) if (a[j+1] < a[j]) Swap(a[j+1], a[j]); It s very slow& O(n2) in the worst case With an optimization, it s possible to get O(n) in the best case [ array already sorted ] The optimization is: stop when no swaps have occurred for an entire pass (an entire iteration of the i variable)Z4Z$ZZ ZZ4$   >>7Selection SortSimilar to Bubble Sort but instead of bubbling the largest element towards the end of the array, we find that largest element and just move it to where it should be On the board& int idx_of_max; // index of the max element int k = a.length() - 1; // monitor value while (k >=0 ) { idx_of_max = 0; for (int i = 0; i <= k; i++) { if (a[i] >= a[idx_of_max]) idx_of_max = i; } swap(a[k], a[idx_of_max]); k--; } It s also slow& O(n2) in the worst case, but typically better than bubble sort ZZOZZ  9%6& dInsertion Sort~On the board& It s also slow& O(n2) in the worst case For animations of these three sorting algorithms, point your browser to http://ciips.ee.uwa.edu.au/~morris/Year2/PLDS210/sorting.html Z?Z  \=r>/PsxHH@Rg(HH(dh   ` www3ff` 3fff` ___>?" dF@,?n<d@uA @ " d`  n?" dd@   @@``PR   @ ` `P0p>> -"2003 W L Truppel bCS 14: Intro. Data Structures & Algs. " Lecture 5 j( = 2  NPsS?"! :    NrSgֳgֳ ?"P  T Click to edit Master title style! !<  HqSgֳgֳ ?"  RClick to edit Master text styles Second level Third level Fourth level Fifth level!     S  N0uSgֳgֳ ?"`p  Z*    NvSgֳgֳ ?"`   n*2    NwSgֳgֳ ?"`@  \*   N  6n޽h? ? www3ff $Blank PresentationM  -"2003 W L Truppel bCS 14: Intro. Data Structures & Algs. " Lecture 5 =5  ( |  B  N1?"442  NxS?"! :    N0xSgֳgֳ ?"` D T Click to edit Master title style! !  HzSgֳgֳ ?"P  D W#Click to edit Master subtitle style$ $  N0{Sgֳgֳ ?"`p D Z*    N|Sgֳgֳ ?"`@ D \*     Nc0gֳgֳ ?"`  D n*2  N  6n޽h? ?  www3ff `<f(  < < 01 "P   ה R*   < 0`1 "   ה T*  r < c $ ?"  ה$ < 00 " @ ה RClick to edit Master text styles Second level Third level Fourth level Fifth level!     S < 60 "`P  ה R*   < 6f "`  ה T*  H < 0޽h ? ̙33> `D( @ $  D D 0  "P   ה ACS 14 Lecture 5  D 0` 1 "   ה T*   D 6 1 "`P  ה ACS 14 Lecture 5  D 6T "`  ה T*  H D 0޽h ? ̙33  d\@ (  P   c $ 1 "P D   c $@ 1 "P  D    < 1D :  B  s *޽h ? 3ff   P@(    S 0x"P  D   S x"  D B  s *޽h ? 33̙ffJ   @x(    c $0x"P  D   c $x"<$ D B  s *޽h ? 33̙ffJ  m 0x( 00//   c $Y"  D   c $ j"<$  D B  s *޽h ? 33̙ffJ  t  x( 00//   c $ Y"@  D   c $Y"p`<$  D B  s *޽h ? 33̙ffJ   x( 00//   c $Y"@  D   c $Y"p`<$  D B  s *޽h ? 33̙ff   (  l  C `Y0  D l  C Y  D H  0޽h ? www3ff(   P( 00// r  S Y0  D   S Y`<$  D H  0޽h ? www3ff(   $P(  $r $ S  Y0  D  $ S `Y`<$  D H $ 0޽h ? www3ff(    P( 00//  r   S Y0  D    S @Y0<$  D H   0޽h ? www3ff(   (P(  (r ( S qS0  D  ( S h00<$  D H ( 0޽h ? www3ff(   ,P(  ,r , S l0  D  , S  p0<$  D H , 0޽h ? www3ff(   0P( 00// 0r 0 S m0  D  0 S o0<$  D H 0 0޽h ? www3ff(   4P( 00// 4r 4 S @n0  D  4 S n0<$  D H 4 0޽h ? www3ff(   8P( 00// 8r 8 S q0  D  8 S @q0<$ D H 8 0޽h ? www3ff(   p<P( 00// <r < S j0  D  < S @k0<$ D H < 0޽h ? www3ff(   0@P(  @r @ S  10  D  @ S 1<$ D H @ 0޽h ? www3ff tlPH(  HR H 3 <   4r H # h< @  4  H H 0޽h ? ̙33cxp^RЀ3ÿ lHbP  @AL G@;b `B&VL^a TZmct7f`h-m%ojUqsPuwzE|u~Ղ,  ՜.+,D՜.+,l(    'On-screen Showsn-sr Times New RomanArial WingdingsTekton Plus RegularSCBillsDingbatsProFontBlank Presentation&Intro to Data Structures & Algorithms Oh+'0(6 px  'GeneraleneeneWagner Truppelo132Microsoft PowerPointrP@Tv@@S@@S@QGG5PICT4 HH HH  )tnnnnnnnnnnnnnnnnggs Tpٷggs Tpnb,Jnm^nm^ne,2@inm^n h8$0Qknnm^nm^nq7g7g 7g /2 q7g7gq7g7g 7g /2 q7g7gnZ1nj,nj,n_  cnj,n m1nj,nj,nqݯP Pe_ҮPҮ3w@'PӰPDZηqݯP Pe_ҮPҮ3w@'PӰPDZηnZ1jUd_KbnT"\iUeO^nneMVmn T"\nneMVmn_,nnC)nm[K[nnT"\nm[K[n3\3nnjT"\iUeO^jUbnjUbnn[LcnnT"\jUbnjUbiUeO^nnVLcnmYOfnqB -H.^9-H9e4x-H4x;" j-H.^js-H.^N  qB -H.^9-H9e4x-H4x;" j-H.^js-H.^N  nZ1]ZW(n?gnn?gn_,n l4nl4n;m.OknnW([:n\8n.2n[:n\8W(m"Lng  Hnq0^)Hk ^Hke<m-)^m-):^)`u?^)ާ;q0^)Hk ^Hke<m-)^m-):^)`u?^)ާ;nZ1]iJ:_(gWX`Za@.n _(gZa@.n_,n]/Vln_(g`/Vln:O Bn_(gWX`[:n\8T)i2f_(g[:n\8WX`H;i mEJlNnm n g-n>Nnm n_,nnQ, ng-nnQ, nmT5 \g-nW5nn[9n[8=Ong-n[9n[8W5nn1 &bg,[nqyg 4 g 4e 5aBgaB1g߽gP$qyg 4 g 4e 5aBgaB1g߽gP$nZ1]7n`4g,nWnnaI*nnhY&8]LejUhY&8W>nn[(Z!mCG`lnqyB8$s B8$s .2KB82K IB8q| 'B8q| (X7qyB8$s B8$s .2KB82K IB8q| 'B8q| (X7nZ1]7n`4nW>nR2ln nR2ln_Hnd bnd b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nnnnnj~tqurnnnnnnnnnnz~6rwvnnnnnnnnnnnm~  syqnn nnnnnnnnn"~i{t{unn$~1u}|nn"~ āwӀsnn"~k ~xӂynn"~!:yӄnn"~ˁ!ҁz҇snn"~{"{҈{nn"~0"L|Ҋnn#~#}ьqnnn#~#~юznnn#~X$pёnnn#~$4ѓonn ~́%ՁЕunn4~%Зnnn~M&hИnnnnnnnnnnnnn~ &3 Кonnnnnnnnnn nnnnnnnnn~'Ϝvnnnnnnnnnnnnnn4~'Ϟnnn(~Yʀ'sϟnn~ (CϢn~(Σsn~)Υna~y)Φnnnnnnnnnʁ~G *f Ψnnnnnnnnnnnnnnnn nnnnnnn~ *= Ωnnnnnnnnnnnnnn nnnnnnnnnnnnnǁ~*ͫtnnnnnnnnnnnnnnnnnnnnn7~+ͭnnn(~+ͮnn~W+tͯn~,,Qͱn~,1Ͳon~؄,߄̴xn~-̴n~-̶n~b-~̷n~?.b̸n~.D̹n~.˺pn~ԅ.܅˻znv~/˼nnnnnnnnnnnnā~/˽nnnnnnnnnnnnnnnnnnnnn~| / ˾nnnnnnnnnnnnnn nnnnnnnnnnnnn~_/}˿nnnnnnnnnnnnnnnnn+~B0fnn(~'0Pnn~0=n~0on~0wn~͆0׆~n~1Ćn~1n~1n~1n~s1n~c1n~S1tn~F2kn~<2cn~32[n~ )22222222 2S ɺnnnnnnnnnnnnnnnnnnn ~ 2222222222222Lɽnnnnnnnnnnnnnnnnnnnnnnnnnف~ 222222 2D nnnnnnnnnnnnnnnnnn7~222?nn~ 2=ņn~ 2:Ɔn~27Ɇn~25Ɇn~2ˆnTodays TopicsADT Counter review ADT Array ADT Array ADT Array ADT ArraySortingSortingMagical Sorting Dumb Sorting Dumb Sorting Dumb SortingSorting extremes Bubble SortSelection SortInsertion Sort  Fonts UsedDesign Template Slide Titles@ 8@ _PID_HLINKS'Amailto:Wagner@cs.ucr.eduhttp://www.cs.ucr.edu/~wagnerhttp://www.cs.ucr.edu/cs12>http://ciips.ee.uwa.edu.au/~morris/Year2/PLDS210/sorting.html&_CTWagner TruppelCurrent User.