1 / 24

Lecture 6

Quicksort. 2. 6.1 QuicksortQuicksort(A[p..r])Divide: ? A[p..r] ?? A[p..q-1] ? A[q 1..r]AAConquer: ??? A[p..q-1] ? A[q 1..r] ??Combine: ???????. . . . . . . . . . . . . . . . p. r. x. pivot. . . . . . . . . . . . . . . . x. A[p..q-1]. . A[q 1..r]. q. Quicksort. 3. Quicksort(A, p, r)1If p

darlita
Download Presentation

Lecture 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


    1. Lecture 6 Quicksort ????????Quicksort??? ?????????????????????Quicksort??? ?????????????

    2. Quicksort 2 6.1 Quicksort Quicksort(A[p..r]) Divide: ? A[p..r] ?? A[p..q-1] ? A[q+1..r] A A Conquer: ??? A[p..q-1] ? A[q+1..r] ?? Combine: ??????? Quicksort?????Divide-and-Conquer: ??????,?????????x ?????? x ?????????,? x ????????? ???????????Quicksort?????Divide-and-Conquer: ??????,?????????x ?????? x ?????????,? x ????????? ???????????

    3. Quicksort 3 Quicksort(A, p, r) 1 If p < r then 2 q ? Partition(A, p, r) /* divide */ 3 Quicksort(A, p, q-1) /* conquer */ 4 Quicksort(A, q+1, r) /* conquer */

    4. Quicksort 4 Partition(A, p, r) 1 x ? A[r] 2 i ? p-1 3 for j? p to r-1 4 do if A[j] ? x 5 then i ? i+1 6 exchange A[i]?A[j] 7 exchange A[i+1]?A[r] 8 return i+1 Partition???????,x ?????? pivot ? x ??????????Partition???????,x ?????? pivot ? x ??????????

    5. Quicksort 5 i ? j ???: i ? j ?????? i ????????? x ??? ?? i, j ????????? x ? j ???????????????i ? j ?????? i ????????? x ??? ?? i, j ????????? x ? j ???????????????

    6. Quicksort 6 i ? j ????: ? j ??????? x ??????, ????? j ??????,?????????????? i, j ??? j ??????? x ??????, ????? j ??????,?????????????? i, j ??

    7. Quicksort 7 i ? j ????: ? j ???????? x ???,???????? i+1 ??????? ?? i ? j ???????? j ???????? x ???,???????? i+1 ??????? ?? i ? j ???????

    8. Quicksort 8 ??: (Partition, x=A[r]=4) ???pivot x, ??????????????? x ??? ????????????????? x ???,?????????????????pivot x, ??????????????? x ??? ????????????????? x ???,??????????????

    9. Quicksort 9 6.2 ?? Worst-case: ?(n2) (?????????) T(n) = = ??: T(n) ? c n2 = O(n2) Substituting: T(n) ? ? c ? c(n-1)2+?(n) ? cn2-c(2n-1)+?(n) ? cn2 (????? c ??) ??????????? ??????? x ?????????????????????? ??????? x ???????????

    10. Quicksort 10 T(n)=?(n2) ?????????,????partition????pivot?? ????partition???????????????????,????partition????pivot?? ????partition??????????

    11. Quicksort 11 Average-case: ?(n lg n) (???????????) T(n)=O(n+X),X ? Partition ?????????? ???? Partition ???,?? A[i]<x<A[j] ? A[j]<x<A[i],A[i] ? A[j] ???????????

    12. Quicksort 12 ??: ? A={3,9,2,7,5}? ??????,A={3,2,5,9,7}? ?? {3,2} ????? {9,7} ???? ? A ???????? z1,z2,...,zn,?? zi ?? i ???????? Zij={zi,zi+1,...,zj}? ??zi : zj :???????? Zij ???? pivot ? zi ? zj?

    13. Quicksort 13 ????? i ? j,?? zi : zj ???? 2/(j-i+1), ??, X = = < (?? Harmonic Series) = = O(nlg n)

    14. Quicksort 14 ??recursion tree????log(n), ?????????T(n) ????????T(n log n)??recursion tree????log(n), ?????????T(n) ????????T(n log n)

    15. Quicksort 15 ??partition?????????????? ????????? n ???????????? 1:9 (?? 1:1000, 1:10000??) ??,?????T(n log n)??partition?????????????? ????????? n ???????????? 1:9 (?? 1:1000, 1:10000??) ??,?????T(n log n)

    16. Quicksort 16 ???? E(n) = = ??????,?? E(n) = nE(n) = ------(1) (n-1)E(n-1) = ------(2) E(n)?Average Case?????? q ? partition ???????????? (1/n) * { E(q-1) + E(n-q) } for all q = 1 to n ???Partition????????????E(n)?Average Case?????? q ? partition ???????????? (1/n) * { E(q-1) + E(n-q) } for all q = 1 to n ???Partition????????????

    17. Quicksort 17 (1)-(2), ?? nE(n) = E(n) = (?? iteration method) = = = = = = ?(n)+?(n) = ?(n)+?(n)??(lg n)+2 (?? Harmonic Series) = ?(nlg n)

    18. Quicksort 18 6.3 Randomized version of quicksort Randomized Algorithm: ???????????? Pseudorandom-number generator: ??????????????? deterministic algorithm ? Randomized-Partition(A, p, r) i ? Random(p, r); exchange(A[r], A[i]); return Partition(A, p, r) ???????????????? pivot, ?????????????????? pivot,????????????worst case??????????????????? pivot, ?????????????????? pivot,????????????worst case???

    19. Quicksort 19 Exercises Problem 1: ??????????????????????????????????????,??? TUT-GLOP ????????????????????????????????????????,???? 310-GINO ? Gino’s ????????????????????????????????”???”3-10-10-10 ? Pizza Hut ?????? ?????????????????????,??????????????????(??:888-1200)?????????????????, ????:

    20. Quicksort 20 Exercises A? B ? C ??? 2; D? E ? F ??? 3 G? H ? I ??? 4; J? K ? L ??? 5 M? N ? O ??? 6; P? R ? S ??? 7 T? U ? V ??? 8; W? X ? Y ??? 9 Q ? Z ??????????????,?????????????TUT-GLOP ?????? 888-4567,310-GINO ?????? 310-4466,? 3-10-10-10 ?? 310-1010? ????????????????????????(?????????)????????????????????????,??????????(???)???????????

    21. Quicksort 21 Exercises ??:??????????,????????????????????????????????(??? 100000),?????????????????????????????,????????????(Q ? Z ??)?????????????????????????????????????? ??:???????????????????????????????,?????????????(?????????)?????????????????????????????,??????: No duplicate. ??????????????

    22. Quicksort 22 ???????????:

    23. Quicksort 23 Exercises Problem 2: ???????????????????????????????????????????,???????????????????,???????????????????????????,????????????????????:?????????,??????????

    24. Quicksort 24 Exercises ??:????????????????(?? 2 ? 12 ??),??????????????????????????,?????????,???????????????(????????????)???????: ??,??,??,??,????,????,?????? (?????) ???? 20000 ???? ??:????????:(???????) -------------------- <??> <??> <??> <??> Department: <????> Home Phone: <????> Work Phone: <????> Campus Box: <??????>

    25. Quicksort 25 ???????????:

More Related