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

Quick Sort

KuoE0
March 15, 2013

Quick Sort

KuoE0

March 15, 2013
Tweet

More Decks by KuoE0

Other Decks in Programming

Transcript

  1. Latest update: Mar 15, 2013 Attribution-ShareAlike 3.0 Unported (CC BY-SA

    3.0) http://creativecommons.org/licenses/by-sa/3.0/
  2. Divide ኺᏐྻத௅ग़Ұݸݩૉ࡞ҝ pivotɼར ༻ pivot ሡݪᏐྻ෼ҝၷݸࢠᏐྻɿ • ॴ༗ݩૉখԙ pivot తᏐྻ

    A • ॴ༗ݩૉେԙ pivot తݩૉతᏐྻ B • ౳ԙ pivot తݩૉՄ์ஔࡏ೚Ұݸத
  3. 2 9 4 3 8 7 5 1 6 2

    7 4 3 5 1 6 9 8
  4. 9 8 2 4 3 5 1 6 2 9

    4 3 8 7 5 1 6 2 7 4 3 5 1 6 9 8
  5. 9 8 2 4 3 5 1 6 2 4

    5 1 6 2 9 4 3 8 7 5 1 6 2 7 4 3 5 1 6 9 8 3
  6. 4 5 6 2 1 9 8 2 4 3

    5 1 6 2 4 5 1 6 2 9 4 3 8 7 5 1 6 2 7 4 3 5 1 6 9 8 3
  7. 4 5 6 2 1 2 9 8 2 4

    3 5 1 6 2 4 5 1 6 2 9 4 3 8 7 5 1 6 2 7 4 3 5 1 6 9 8 3 1
  8. 4 5 6 2 1 2 9 8 2 4

    3 5 1 6 2 4 5 1 6 2 9 4 3 8 7 5 1 6 2 2 7 4 3 5 1 6 9 8 3 1
  9. 4 5 6 5 6 2 1 2 9 8

    2 4 3 5 1 6 2 4 5 1 6 2 9 4 3 8 7 5 1 6 2 2 7 4 3 5 1 6 9 8 3 1 4
  10. 5 6 4 5 6 5 6 2 1 2

    9 8 2 4 3 5 1 6 2 4 5 1 6 2 9 4 3 8 7 5 1 6 2 2 7 4 3 5 1 6 9 8 3 1 4
  11. 5 6 5 4 5 6 5 6 2 1

    2 9 8 2 4 3 5 1 6 2 4 5 1 6 2 9 4 3 8 7 5 1 6 2 2 7 4 3 5 1 6 9 8 3 1 4 6
  12. 5 6 5 4 5 6 5 6 2 1

    2 9 8 2 4 3 5 1 6 2 4 5 1 6 2 9 4 3 8 7 5 1 6 2 5 2 7 4 3 5 1 6 9 8 3 1 4 6
  13. 5 6 5 4 5 6 5 6 2 1

    2 9 8 9 2 4 3 5 1 6 2 4 5 1 6 2 9 4 3 8 7 5 1 6 2 5 2 7 4 3 5 1 6 9 8 3 1 4 6 8
  14. 5 6 5 4 5 6 5 6 2 1

    2 9 8 9 2 4 3 5 1 6 2 4 5 1 6 2 9 4 3 8 7 5 1 6 9 2 5 2 7 4 3 5 1 6 9 8 3 1 4 6 8
  15. 2 2

  16. 4 5 6 5 6 1 2 1 2 4

    5 6 5 6 2 5 2 5 1 6 4 3
  17. 4 5 6 5 6 1 2 1 2 4

    5 6 5 6 2 5 2 5 1 6 4 3
  18. 1 2 3 4 5 6 4 5 6 5

    6 1 2 1 2 4 5 6 5 6 1 2 3 4 5 6 9 2 5 2 5 9 1 6 4 3 8
  19. 1 2 3 4 5 6 4 5 6 5

    6 1 2 1 2 4 5 6 5 6 1 2 3 4 5 6 9 2 5 2 5 9 1 6 4 3 8
  20. 8 9 1 2 3 4 5 6 8 9

    4 5 6 5 6 1 2 1 2 4 5 6 5 6 1 2 3 4 5 6 9 2 5 2 5 9 7 1 6 4 3 8
  21. 8 9 1 2 3 4 5 6 8 9

    4 5 6 5 6 1 2 1 2 4 5 6 5 6 1 2 3 4 5 6 9 2 5 2 5 9 7 1 6 4 3 8
  22. 8 9 1 2 3 4 5 6 8 9

    4 5 6 5 6 1 2 1 2 4 5 6 5 6 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 9 2 5 2 5 9 7 1 6 4 3 8
  23. 9 3 How to Divide 4 1 5 8 6

    4 7 2 4 1 3 2 5 8 7 9 6
  24. void quickSort( int L, int R ) { if (

    R - L <= 1 ) return; int pivot = N[ R - 1 ], p = L; for ( int i = L; i < R - 1; ++i ) { if ( N[ i ] <= pivot ) { swap( N[ i ], N[ p ] ); ++p; } } swap( N[ R - 1 ], N[ p ] ); quickSort( L, p ); quickSort( p + 1, R ); } In-place Version
  25. 9 4 3 6 5 1 8 7 How to

    Divide 2 2 6
  26. 9 4 3 6 5 1 8 7 How to

    Divide 2 2 6
  27. 9 4 3 6 5 1 8 7 How to

    Divide 2 2 2 6
  28. 9 9 4 3 6 5 1 8 7 How

    to Divide 2 2 2 6
  29. 9 9 4 4 3 6 5 1 8 7

    How to Divide 2 2 2 9 6
  30. 9 9 4 4 3 6 5 1 8 7

    How to Divide 2 2 2 9 6
  31. 9 9 4 4 3 6 5 1 8 7

    How to Divide 2 2 2 9 4 6
  32. 9 9 4 4 3 3 6 5 1 8

    7 How to Divide 2 2 2 9 4 6
  33. 9 9 4 4 3 3 6 5 1 8

    7 How to Divide 2 2 2 9 4 6
  34. 9 9 4 4 3 3 6 5 1 8

    7 How to Divide 2 2 2 9 4 3 6
  35. 9 9 4 4 3 3 6 5 1 8

    7 8 How to Divide 2 2 2 9 4 3 6
  36. 9 9 4 4 3 3 6 5 1 8

    7 8 8 How to Divide 2 2 2 9 4 3 6
  37. 9 9 4 4 3 3 6 5 1 8

    7 8 7 8 How to Divide 2 2 2 9 4 3 6
  38. 9 9 4 4 3 3 6 5 1 8

    7 8 7 8 7 How to Divide 2 2 2 9 4 3 6
  39. 9 9 4 4 3 3 6 5 1 5

    8 7 8 7 8 7 How to Divide 2 2 2 9 4 3 6
  40. 9 9 4 4 3 3 6 5 1 5

    8 7 8 7 8 7 How to Divide 2 2 2 9 4 3 6
  41. 9 9 4 4 3 3 6 5 1 5

    8 7 8 7 8 7 How to Divide 2 2 2 5 9 4 3 6
  42. 9 9 4 4 3 3 6 5 1 5

    8 7 8 7 8 7 How to Divide 2 2 2 5 9 4 3 1 6
  43. 9 9 4 4 3 3 6 5 1 5

    8 7 8 7 8 7 How to Divide 2 2 2 5 9 4 3 1 6
  44. 9 9 4 4 3 3 6 5 1 5

    8 7 8 7 8 7 How to Divide 2 2 2 5 9 4 3 1 1 6
  45. 9 9 4 4 3 3 6 5 1 5

    8 7 8 7 8 7 How to Divide 2 2 2 5 9 4 3 1 1 6
  46. 9 9 4 4 3 3 6 5 1 5

    8 7 8 7 8 7 How to Divide 2 2 2 5 9 4 3 1 1 6 2 5 4 3 1 8 9 7
  47. 6 How to Conquer 1 4 2 3 5 8

    7 9 1 4 2 3 5 8 7 9
  48. 6 How to Conquer 1 4 2 3 5 8

    7 9 1 4 2 3 5 8 7 9
  49. End