cs107 computing for math cs107 computing for math and
play

CS107: Computing for Math CS107: Computing for Math and Science - PowerPoint PPT Presentation

CS107: Computing for Math CS107: Computing for Math and Science and Science Lecture 15: Real Numbers Lecture 15 CS107, Prof. Steinberg, f10 1 Sorting Sorting There are many ways to sort data Insertion Sort Selection Sort


  1. CS107: Computing for Math CS107: Computing for Math and Science and Science Lecture 15: Real Numbers Lecture 15 CS107, Prof. Steinberg, f10 1

  2. Sorting Sorting • There are many ways to sort data – Insertion Sort – Selection Sort – Quick Sort – Merge Sort – (Many others) Lecture 15 CS107, Prof. Steinberg, f10 2

  3. Insertion Sort Insertion Sort • One by one add numbers to the sorted vector – Moving numbers already there to make room 20 10 30 5 Unsorted: Sorted: Lecture 15 CS107, Prof. Steinberg, f10 3

  4. Insertion Sort Insertion Sort • One by one add numbers to the sorted vector – Moving numbers already there to make room 20 10 30 5 Unsorted: 20 Sorted: Lecture 15 CS107, Prof. Steinberg, f10 4

  5. Insertion Sort Insertion Sort • One by one add numbers to the sorted vector – Moving numbers already there to make room 20 10 30 5 Unsorted: 20 Sorted: Lecture 15 CS107, Prof. Steinberg, f10 5

  6. Insertion Sort Insertion Sort • One by one add numbers to the sorted vector – Moving numbers already there to make room 20 10 30 5 Unsorted: 10 20 Sorted: Lecture 15 CS107, Prof. Steinberg, f10 6

  7. Insertion Sort Insertion Sort • One by one add numbers to the sorted vector – Moving numbers already there to make room 20 10 30 5 Unsorted: 10 20 30 Sorted: Lecture 15 CS107, Prof. Steinberg, f10 7

  8. Insertion Sort Insertion Sort • One by one add numbers to the sorted vector – Moving numbers already there to make room 20 10 30 5 Unsorted: 10 20 30 Sorted: Lecture 15 CS107, Prof. Steinberg, f10 8

  9. Insertion Sort Insertion Sort • One by one add numbers to the sorted vector – Moving numbers already there to make room 20 10 30 5 Unsorted: 10 20 30 Sorted: Lecture 15 CS107, Prof. Steinberg, f10 9

  10. Insertion Sort Insertion Sort • One by one add numbers to the sorted vector – Moving numbers already there to make room 20 10 30 5 Unsorted: 10 20 30 Sorted: Lecture 15 CS107, Prof. Steinberg, f10 10

  11. Insertion Sort Insertion Sort • One by one add numbers to the sorted vector – Moving numbers already there to make room 20 10 30 5 Unsorted: 5 10 20 30 Sorted: Lecture 15 CS107, Prof. Steinberg, f10 11

  12. In Place Insertion Sort In Place Insertion Sort • We don’t really need two vectors, unsorted and sorted - we can keep all the data in one vector Lecture 15 CS107, Prof. Steinberg, f10 12

  13. In Place Insertion Sort In Place Insertion Sort 20 10 30 5 Sorted | unsorted Lecture 15 CS107, Prof. Steinberg, f10 13

  14. In Place Insertion Sort In Place Insertion Sort 10 20 10 30 5 Sorted | unsorted Lecture 15 CS107, Prof. Steinberg, f10 14

  15. In Place Insertion Sort In Place Insertion Sort 10 20 20 30 5 Sorted | unsorted Lecture 15 CS107, Prof. Steinberg, f10 15

  16. In Place Insertion Sort In Place Insertion Sort 10 10 20 30 5 Sorted | unsorted Lecture 15 CS107, Prof. Steinberg, f10 16

  17. In Place Insertion Sort In Place Insertion Sort 10 20 30 5 Sorted | unsorted Lecture 15 CS107, Prof. Steinberg, f10 17

  18. In Place Insertion Sort In Place Insertion Sort 10 20 30 5 Sorted | unsorted Lecture 15 CS107, Prof. Steinberg, f10 18

  19. In Place Insertion Sort In Place Insertion Sort 10 20 30 5 Sorted | unsorted Lecture 15 CS107, Prof. Steinberg, f10 19

  20. In Place Insertion Sort In Place Insertion Sort 5 10 20 30 5 Sorted | unsorted Lecture 15 CS107, Prof. Steinberg, f10 20

  21. In Place Insertion Sort In Place Insertion Sort 5 10 20 30 30 Sorted | unsorted Lecture 15 CS107, Prof. Steinberg, f10 21

  22. In Place Insertion Sort In Place Insertion Sort 5 10 20 20 30 Sorted | unsorted Lecture 15 CS107, Prof. Steinberg, f10 22

  23. In Place Insertion Sort In Place Insertion Sort 5 10 10 20 30 Sorted | unsorted Lecture 15 CS107, Prof. Steinberg, f10 23

  24. In Place Insertion Sort In Place Insertion Sort 5 5 10 20 30 Sorted | unsorted Lecture 15 CS107, Prof. Steinberg, f10 24

  25. How Much Work is it? How Much Work is it? • Approximately length squared Lecture 15 CS107, Prof. Steinberg, f10 25

  26. How Much Work is it? How Much Work is it? • Length 2 is worst case • We might find the right place early • If vector already close to sorted, takes much less than length 2 • Note the cost of moving things in addition to comparing Lecture 15 CS107, Prof. Steinberg, f10 26

  27. Selection Sort Selection Sort • Repeatedly – Find smallest in unsorted – Move it to end of sorted Unsorted: 20 10 5 30 Sorted: Lecture 15 CS107, Prof. Steinberg, f10 27

  28. Selection Sort Selection Sort • Repeatedly – Find smallest in unsorted – Move it to end of sorted Unsorted: 10 20 5 30 Sorted: Lecture 15 CS107, Prof. Steinberg, f10 28

  29. Selection Sort Selection Sort • Repeatedly – Find smallest in unsorted – Move it to end of sorted Unsorted: 10 20 5 7 Sorted Lecture 15 CS107, Prof. Steinberg, f10 29

  30. Selection Sort Selection Sort • Repeatedly – Find smallest in unsorted – Move it to end of sorted Unsorted: 10 20 5 7 Sorted: 5 How can we delete it from unsorted? Lecture 15 CS107, Prof. Steinberg, f10 30

  31. Selection Sort Selection Sort • Repeatedly – Find smallest in unsorted – Move it to end of sorted Unsorted: 10 20 7 7 Sorted: 5 ignore How can we delete it from unsorted? ` Lecture 15 CS107, Prof. Steinberg, f10 31

  32. Selection Sort Selection Sort • Repeatedly – Find smallest in unsorted – Move it to end of sorted Unsorted: 10 20 7 7 Sorted: 5 ignore Lecture 15 CS107, Prof. Steinberg, f10 32

  33. Selection Sort Selection Sort • Repeatedly – Find smallest in unsorted – Move it to end of sorted Unsorted: 10 20 7 7 Sorted: 5 7 ignore Lecture 15 CS107, Prof. Steinberg, f10 33

  34. Selection Sort Selection Sort • Repeatedly – Find smallest in unsorted – Move it to end of sorted ignore Unsorted: 10 20 7 7 Sorted: 5 7 Lecture 15 CS107, Prof. Steinberg, f10 34

  35. Selection Sort Selection Sort • Repeatedly – Find smallest in unsorted – Move it to end of sorted ignore Unsorted: 20 20 7 7 Sorted: 5 7 10 Lecture 15 CS107, Prof. Steinberg, f10 35

  36. Selection Sort Selection Sort • Repeatedly – Find smallest in unsorted – Move it to end of sorted ignore Unsorted: 20 20 7 7 Sorted: 5 7 10 Lecture 15 CS107, Prof. Steinberg, f10 36

  37. Selection Sort Selection Sort • Repeatedly – Find smallest in unsorted – Move it to end of sorted ignore Unsorted: 20 20 7 7 Sorted: 5 7 10 20 See selectSort.m Lecture 15 CS107, Prof. Steinberg, f10 37

  38. In Place Selection Sort In Place Selection Sort • Key idea: swap smallest unsorted and first unsorted - see inplaceSelectSort.m and minPlace.m |20 10 5 8 5|10 20 8 5 8| 20 10 5 8 10| 20 Lecture 15 CS107, Prof. Steinberg, f10 38

  39. How Much Work is it? How Much Work is it? • Length 2 • Same for all data Lecture 15 CS107, Prof. Steinberg, f10 39

  40. Selection vs vs Insertion Insertion Selection Selection sort: Insertion sort • Simpler to write • Harder to write • Same worst case: • Same worst case: O(n 2 ) O(n 2 ) • Cannot make use of • Can make use of partial pre-sorting: partially sorted list: worse average case better average case Lecture 15 CS107, Prof. Steinberg, f10 40

  41. Quick Sort Quick Sort • Divide and conquer – Divide numbers into two groups – Sort each group separately – Combine sorted groups • Quick sort: – Choose one number, call it pivot – Groups are numbers > pivot, < pivot – Combine: just append Lecture 15 CS107, Prof. Steinberg, f10 41

  42. Quick Sort Quick Sort 2 5 1 8 3 9 4 Pivot -> 4 2 1 3 5 8 9 1 2 3 5 8 9 1 2 3 4 5 8 9 How do we sort the groups? Quick sort! If group length = 1, already sorted Lecture 15 CS107, Prof. Steinberg, f10 42

  43. Partitioning Partitioning How to separate by the pivot • Scan from small indexes looking for a number > pivot • Scan from large indexes looking for a number < pivot • Swap the two numbers Lecture 15 CS107, Prof. Steinberg, f10 43

  44. Partitioning Partitioning 2 2 1 1 9 9 8 8 7 7 4 4 5 5 10 10 Pivot = 6 Lecture 15 CS107, Prof. Steinberg, f10 44

  45. Partitioning Partitioning 2 2 1 1 9 9 8 8 7 7 4 4 5 5 10 10 Pivot = 6 Lecture 15 CS107, Prof. Steinberg, f10 45

  46. Partitioning Partitioning 2 2 1 1 5 5 8 8 7 7 4 4 9 9 10 10 Pivot = 6 Lecture 15 CS107, Prof. Steinberg, f10 46

  47. Partitioning Partitioning 2 2 1 1 5 5 8 8 7 7 4 4 9 9 10 10 Pivot = 6 Lecture 15 CS107, Prof. Steinberg, f10 47

  48. Partitioning Partitioning 2 2 1 1 5 5 4 4 7 7 8 8 9 9 10 10 Pivot = 6 Lecture 15 CS107, Prof. Steinberg, f10 48

  49. Partitioning Partitioning 2 2 1 1 5 5 4 4 7 7 8 8 9 9 10 10 Pivot = 6 Lecture 15 CS107, Prof. Steinberg, f10 49

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend