Docsity

Algorithm Analysis - Algorithms and Abstract Data Structures - Lecture Slides, Slides of Data Structures and Algorithms

Partial preview of the text

Download Algorithm Analysis - Algorithms and Abstract Data Structures - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

CPSC 223

Algorithms & Data Abstract Structures

Lecture 11:

Algorithm Analysis (cont.)

More on Sorting, Mergesort

Today …

• Homework 5

• More on Algorithm Analysis

• Continuing with Sorting

• Mergesort [Sect. 9.2, pp. 466-472]

CPSC 223 -­‐-­‐ Fall 2010 2

Algorithm Analysis:

Constant Factors

CPSC 223 -­‐-­‐ Fall 2010 3

Big-O Notation

We drop the constant factors and dominated terms

  • Instead of O (6 n - 5) we just write O ( n )
  • Instead of n^2 - 5 n + 10 we just write O ( n^2 ) Why can we do this? CPSC 223 -­‐-­‐ Fall 2010 4

Big-O Notation

Here are the graphs of these functions

CPSC 223 -­‐-­‐ Fall 2010 7 6 n + 3 9 n n y 1 2 3 4 5 0 3 9 15 18 21 27

Big-O Notation

  • Suppose A’s detailed worst-case running time is

f ( n ) = n^2 - 5 n + 10

  • we say that A is O ( n^2 )
  • What values do we pick for n 0 and k to solve this? We want to show that
  • n^2 - 5 n + 10 ≤ k · n^2 for some n 0 Lets try n 0 = 2 which gives
  • f (2) = 4 – 2·5 + 10 ≤ k · 4
  • in this case, setting k = 1 does the trick! CPSC 223 -­‐-­‐ Fall 2010 8

Back to Sorting

CPSC 223 -­‐-­‐ Fall 2010 9

Types of Sorting Algorithms

  • Exchange-based
    • Swap pairs of items
    • Bubble sort, cocktail sort, comb sort (1980)
  • Selection-based
    • Select smallest item and move into place
    • Selection sort, heapsort (1964)
  • Insertion-based
    • Insert new items into already sorted lists
    • Linear insertion, shell (1959), tree (1964), library sort (2004!)
  • Partitioning- and Merging-based
    • Divide and conquer (break problem into 2 or more subproblems)
    • Mergesort (1945), quicksort (1962) CPSC 223 -­‐-­‐ Fall 2010 10

Mergesort

CPSC 223 -­‐-­‐ Fall 2010 13

Mergesort [von Neumann, 1945]

  • A “divide and conquer” approach
  • The basic idea:
    • Divide the list into two halves
    • Sort each half
    • Merge the sorted halves … Merging is fast when the two sublists are sorted CPSC 223 -­‐-­‐ Fall 2010 14 29 10 14 13 29 10 14 13 Ini6al list: ( n =4) 29 10 14 13 Split Sort^ Merge 10 29 13 14 10 29 13 14 10 13 14 29

Mergesort

  • Given that mergesort merges two sorted lists to

create a (larger) sorted list

  • How do we sort the two sublists?
    • Using mergesort!
    • Divide the list into two halves, sort each half (using mergesort), and then merge the sorted halves
  • Mergesort is defined using recursion CPSC 223 -­‐-­‐ Fall 2010 15

Mergesort

CPSC 223 -­‐-­‐ Fall 2010 16 29 10 14 13 Ini6al list ( n =4) 29 10 14 13 Split 29 10 14 13 Split ( 1-­‐element lists are sorted by defini2on ) 10 29 13 14 Merge 10 13 14 29 Merge

Mergesort

The merge step

  • Assume number of elements to be merged is n
    • n = length of first half + length of second half
  • The merge step requires
    • n – 1 comparisons
    • n moves from the original array to the temp array
    • n moves from the temp array to the original array
  • So merge costs 3 n – 1
  • Which is O ( n ) CPSC 223 -­‐-­‐ Fall 2010 19