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

An in-depth analysis of the merge sort algorithm, focusing on its implementation, counting inversions, and time complexity. It includes the merge and count steps, the combine process for counting blue-green inversions, and the divide-and-conquer multiplication as a warm-up.

Typology: Lecture notes

2018/2019

Uploaded on 04/20/2019

kefart
kefart 🇺🇸

4.4

(11)

55 documents

1 / 92

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
The University of Sydney Page 1
Quiz 2 7:00PM Fri 12 April 2019 (Week 7)
Assignment 2 11:59PM Fri 14 April 2019
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c

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)