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

Insertion Sort

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
Avatar for KuoE0 KuoE0
March 03, 2013

Insertion Sort

Avatar for KuoE0

KuoE0

March 03, 2013
Tweet

More Decks by KuoE0

Other Decks in Programming

Transcript

  1. 4 1 2 1 8 9 3 Set 1st element

    as sorted part. 4
  2. 4 1 2 1 8 9 3 Others are unsorted

    part. 4 1 2 1 8 9 3
  3. 4 1 2 1 8 9 3 Enumerate elements in

    unsorted part. 4 1 2 1 8 9 3 1
  4. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1
  5. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1 Copy the value to temporal variable.
  6. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1 4
  7. 4 1 2 1 8 9 3 Find the foremost

    element great than the current element. 4 temp: 1 2 1 8 9 3 1 1 4
  8. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 4 1
  9. 4 1 2 1 8 9 3 Insert the element.

    4 temp: 1 2 1 8 9 3 1 4 1
  10. 4 1 2 1 8 9 3 Insert the element.

    4 1 2 1 8 9 3 1 1 4
  11. 4 1 2 1 8 9 3 Insert the element.

    4 1 2 1 8 9 3 1 1 1 4
  12. 4 1 2 1 8 9 3 4 1 2

    1 8 9 3 1 1 1 4
  13. 4 1 2 1 8 9 3 Enumerate next element.

    4 1 2 1 8 9 3 1 1 1 4 2
  14. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1 1 4 2 2
  15. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1 1 4 2 2 Copy the value to temporal variable.
  16. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1 1 4 2 2 4
  17. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1 1 4 2 2 Find the foremost element great than the current element. 4
  18. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1 1 4 2 4 2
  19. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1 1 4 2 4 Insert the element. 2
  20. 4 1 2 1 8 9 3 4 1 2

    1 8 9 3 1 1 1 4 2 4 Insert the element. 2
  21. 4 1 2 1 8 9 3 4 1 2

    1 8 9 3 1 1 1 4 2 4 Insert the element. 2 2
  22. 4 1 2 1 8 9 3 4 1 2

    1 8 9 3 1 1 1 4 2 4 2 2
  23. 4 1 2 1 8 9 3 4 1 2

    1 8 9 3 1 1 1 4 2 4 2 2 Enumerate next element. 1
  24. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1 1 4 2 4 2 2 1 1
  25. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1 1 4 2 4 2 2 1 1 Copy the value to temporal variable.
  26. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1 1 4 2 4 2 2 1 1 4 2
  27. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1 1 4 2 4 2 2 1 1 Find the foremost element great than the current element. 4 2
  28. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1 1 4 2 4 2 2 1 1 Find the foremost element great than the current element. 4 2
  29. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 1
  30. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 Insert the element. 1
  31. 4 1 2 1 8 9 3 4 1 2

    1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 Insert the element. 1
  32. 4 1 2 1 8 9 3 4 1 2

    1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 Insert the element. 1 1
  33. 4 1 2 1 8 9 3 4 1 2

    1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 1 1
  34. 4 1 2 1 8 9 3 4 1 2

    1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 1 1 Enumerate next element. 8
  35. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 1 1 8 8
  36. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 1 1 8 Copy the value to temporal variable. 8
  37. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 1 1 8 8 4
  38. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 1 1 8 8 No element is greater than the current element in sorted part. 4
  39. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 1 1 8 4 8
  40. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 1 1 8 4 Put back. 8
  41. 4 1 2 1 8 9 3 4 1 2

    1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 1 1 8 4 Put back. 8
  42. 4 1 2 1 8 9 3 4 1 2

    1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 1 1 8 4 Put back. 8 8
  43. 4 1 2 1 8 9 3 4 1 2

    1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 1 1 8 4
  44. 4 1 2 1 8 9 3 4 1 2

    1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 1 1 8 4 Enumerate next element. 9
  45. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 1 1 8 4 9 9
  46. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 1 1 8 4 9 9 Copy the value to temporal variable.
  47. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 1 1 8 4 9 9
  48. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 1 1 8 4 9 9 No element is greater than the current element in sorted part.
  49. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 1 1 8 4 9 9
  50. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 1 1 8 4 9 Put back. 9
  51. 4 1 2 1 8 9 3 4 1 2

    1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 1 1 8 4 9 Put back. 9
  52. 4 1 2 1 8 9 3 4 1 2

    1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 1 1 8 4 9 Put back. 9 9
  53. 4 1 2 1 8 9 3 4 1 2

    1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 1 1 8 4 9 9 9
  54. 4 1 2 1 8 9 3 4 1 2

    1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 1 1 8 4 9 9 9 Enumerate next element. 3
  55. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 1 1 8 4 9 9 9 3 3
  56. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 1 1 8 4 9 9 9 3 Copy the value to temporal variable. 3
  57. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 1 1 8 4 9 9 9 3 3 9 8 4
  58. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 1 1 8 4 9 9 9 3 3 Find the foremost element great than the current element. 9 8 4
  59. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 1 1 8 4 9 9 9 3 3 Find the foremost element great than the current element. 9 8 4
  60. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 1 1 8 4 9 9 9 3 3 Find the foremost element great than the current element. 9 8 4
  61. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 1 1 8 4 9 9 9 3 9 8 4 3
  62. 4 1 2 1 8 9 3 4 temp: 1

    2 1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 1 1 8 4 9 9 9 3 9 8 4 Insert the element. 3
  63. 4 1 2 1 8 9 3 4 1 2

    1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 1 1 8 4 9 9 9 3 9 8 4 Insert the element. 3
  64. 4 1 2 1 8 9 3 4 1 2

    1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 1 1 8 4 9 9 9 3 9 8 4 Insert the element. 3 3
  65. 4 1 2 1 8 9 3 4 1 2

    1 8 9 3 1 1 1 4 2 4 2 2 1 4 2 1 1 8 4 9 9 9 3 9 8 4 Sort completely! 3 3
  66. End