Docsity

Merge Sort and Counting Inversions: Algorithm and Complexity Analysis, Lecture notes of Algorithms and Programming

Partial preview of the text

Download Merge Sort and Counting Inversions: Algorithm and Complexity Analysis and more Lecture notes Algorithms and Programming in PDF only on Docsity!

Quiz 2 7:00PM Fri 12 April 2019 (Week 7) Assignment 2 11:59PM Fri 14 April 2019

Greedy algorithm recap:

Problems that can be solved using a greedy algorithm.

  • Coin change delivery (in some cases)
  • Conversion to Binary
  • Interval scheduling
  • Scheduling to minimize lateness
  • Shortest path
  • Minimum spanning trees
  • Clustering

The Master Theorem

If T(n) = aT(n/b) + f(n) then

COMP9007 - Algorithms Divide & Conquer

Fast Fourier Transform

Searching

Input: A sorted sequence S of n numbers a

,a

,…,a

n

stored in an array A[1..n].

Question: Given a number x, is x in S?

Binary Search

  • Compare x to the middle element of the array (A[n/2]).
  • If A[n/2] = x then “Yes”
  • Otherwise, if A[n/2] > x then recursively Search A[1…n/2-1].
  • Otherwise, if A[n/2] < x then recursively Search A[n/2+1...n] 0 1 3 4 5 7 10 13 15 18 19 23

Binary Search

  • Compare x to the middle element of the array (A[n/2]).
  • If A[n/2] = x then “Yes”
  • Otherwise, if A[n/2] > x then recursively Search A[1…n/2-1].
  • Otherwise, if A[n/2] < x then recursively Search A[n/2+1...n] 0 1 3 4 5 7 10 13 15 18 19 23

Example: x=1 (non-integers are rounded down)

Binary Search

  • Compare x to the middle element of the array (A[n/2]).
  • If A[n/2] = x then “Yes”
  • Otherwise, if A[n/2] > x then recursively Search A[1…n/2-1].
  • Otherwise, if A[n/2] < x then recursively Search A[n/2+1...n] 0 1 3 4 5 7 10 13 15 18 19 23

Example: x=1 (non-integers are rounded down)

Binary Search

  • Compare x to the middle element of the array (A[n/2]).
  • If A[n/2] = x then “Yes”
  • Otherwise, if A[n/2] > x then recursively Search A[1…n/2-1].
  • Otherwise, if A[n/2] < x then recursively Search A[n/2+1...n] 0 1 3 4 5 7 10 13 15 18 19 23

Example: x=1 (non-integers are rounded down)

Binary Search

  • Compare x to the middle element of the array (A[n/2]).
  • If A[n/2] = x then “Yes”
  • Otherwise, if A[n/2] > x then recursively Search A[1…n/2-1].
  • Otherwise, if A[n/2] < x then recursively Search A[n/2+1...n]

Example: x=1 (non-integers are rounded down)

Analyze recursion T(n)

1 (of size n)

1 (of size n/2)

1 (of size n/4)

1 (of size n/

k

1 (of size 1)

log 2 n

T(n) = T(n/2)+O(1)

T(n/2) T(n/2k) T(n/4) T(1)

Binary Search

Example: x=1 (non-integers are rounded down)

Analysis: T(n) = 1+ T(n/2) = O(log n)

  • Compare x to the middle element of the array (A[n/2]).
  • If A[n/2] = x then “Yes”
  • Otherwise, if A[n/2] > x then recursively Search A[1…n/2-1].
  • Otherwise, if A[n/2] < x then recursively Search A[n/2+1...n]

Mergesort

Mergesort Mergesort.

  • Divide array into two halves.
  • Recursively sort each half.
  • Merge two halves to make sorted whole.

merge

sort

divide

A L G O R I T H M S

A L G O R I T H M S

A G L O R H I M S T

A G H I L M O R S T

Jon von Neumann (1945)

O(n)

2T(n/2)

O(1)