Sorting Algorithms Algorithm Analysis and Big-O Searching Checkout - - PowerPoint PPT Presentation

sorting algorithms algorithm analysis and big o searching
SMART_READER_LITE
LIVE PREVIEW

Sorting Algorithms Algorithm Analysis and Big-O Searching Checkout - - PowerPoint PPT Presentation

Sorting Algorithms Algorithm Analysis and Big-O Searching Checkout SortingAndSearching project from SVN Exam results Lets see Shlemiel the Painter Be able to describe basic sorting algorithms: Selection sort Insertion sort


slide-1
SLIDE 1

Sorting Algorithms Algorithm Analysis and Big-O Searching

Checkout SortingAndSearching project from SVN

slide-2
SLIDE 2

Exam results

slide-3
SLIDE 3

Let’s see…

slide-4
SLIDE 4

Shlemiel the Painter

slide-5
SLIDE 5

 Be able to describe basic sorting algorithms:

  • Selection sort
  • Insertion sort
  • Merge sort
  • Quicksort

 Know the run-time efficiency of each  Know the best and worst case inputs for each

slide-6
SLIDE 6

 Basic idea:

  • Think of the list as having a sorted part (at the

beginning) and an unsorted part (the rest)

  • Find the smallest number

in the unsorted part

  • Move it to the end of the

sorted part (making the sorted part bigger and the unsorted part smaller)

Repeat until unsorted part is empty

slide-7
SLIDE 7

 Profiling: collecting data on the run-time

behavior of an algorithm

 How long does selection sort take on:

  • 10,000 elements?
  • 20,000 elements?
  • 80,000 elements?

Q1

slide-8
SLIDE 8

 Analyzing: calculating the performance of an

algorithm by studying how it works, typically mathematically

 Typically we want the relative performance as

a function of input size

 Example: For an array of length n, how many

times does selectionSort() call compareTo()?

Handy Fact

Q2-7

slide-9
SLIDE 9

 In analysis of algorithms we care about

differences between algorithms on very large inputs

 We say, “selection sort takes on the order of

n2 steps”

 Big-Oh gives a formal definition for

“on the order of”

slide-10
SLIDE 10

 We write f(n) = O(g(n)), and

say “f is big-Oh of g”

 if there exists positive constants c and n0 such that  0 ≤ f(n) ≤ c g(n)

for all n > n0

 g is a ceiling on f

Q8,9

slide-11
SLIDE 11

Perhaps it’s time for a break.

slide-12
SLIDE 12

 Basic idea:

  • Think of the list as having a sorted part (at the

beginning) and an unsorted part (the rest)

  • Get the first number in the

unsorted part

  • Insert it into the correct

location in the sorted part, moving larger values up to make room

Repeat until unsorted part is empty

slide-13
SLIDE 13

 Profile insertion sort  Analyze insertion sort assuming the inner

while loop runs that maximum number of times

 What input causes the worst case behavior?

The best case?

 Does the input affect selection sort?

Q10-19 Ask for help if you’re stuck!

slide-14
SLIDE 14

 Consider:

  • Find the CRN of CSSE220 in the printed schedule
  • Find the course whose CRN is 2331

 Why is one task harder than the other?  For searching unsorted data, what’s the worst

case number of comparisons we would have to make?

slide-15
SLIDE 15

 A divide and conquer strategy  Basic idea:

  • Divide the list in half
  • Decide whether result should be in upper or lower

half

  • Recursively search that half
slide-16
SLIDE 16

 What’s the best case?  What’s the worst case?  We use recurrence relations to analyze

recursive algorithms:

  • Let T(n) count the number of comparisons to search

an array of size n

  • Examine code to find recursive formula of T(n)
  • Solve for n

Q20-21

slide-17
SLIDE 17

Review Homework. Determine Mini-project meeting time.