hub 102 - lesson 5 - algorithm: sorting & searching

60
Lesson 5 Algorithm: Sorting and Searching

Upload: tieu-ho

Post on 16-Aug-2015

67 views

Category:

Engineering


4 download

TRANSCRIPT

Page 1: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Lesson 5Algorithm: Sorting and Searching

Page 2: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Algorithm Analysis• Motivation

• What to do with algorithms?

• Programmer needs to develop a working solution

• Client wants problem solved efficiently

• Theoretician wants to understand

• Why analyze algorithms?

• To compare different algorithms for the same task

• To predict performance in a new environment

• To set values of algorithm parameters

Page 3: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Running Time• We are interested in the design of "good" data structures and

algorithms.

• Measure of "goodness":

- Running time (most important)

- Space usage

• The running time of an algorithm typically grows with the input size, and is affected by other factors:

- Hardware environments: processor, memory, disk.

- Software environments: OS, compiler.

• Focus: input size vs. running time.

Page 4: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Pseudocode• High-level description of

an algorithm

• More structured than English prose

• Less detailed than a program source code

• Preferred notation for describing algorithms

• Hides program design issues

Page 5: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Pseudocode Details• Control flow

- if … then … [else …]

- while … do …

- repeat … until …

- for … do …

- Indentation replaces braces

• Method declaration

- Algorithm method (arg [, arg…])

Input …

Output …

• Method call

var.method (arg [, arg…])

• Return value

return expression

• Expressions

Page 6: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Counting Primitive Operations

By inspecting the pseudocode, we can determine the maximum number of primitive operations executed by an algorithm, as a function of the input size

Page 7: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Growth Rates

• Growth rates of functions:

• Linear ≈ n

• Quadratic ≈ n2

• Cubic ≈ n3

Page 8: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Big-Oh Notation

• Given functions f(n) and g(n), we say that f(n) is O(g(n)) if there are positive constants c and n0 such that

f(n) ≤ cg(n) for n ≥ n0

Page 9: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Big-Oh Notation Example

• Example: 2n + 10 is O(n)

• 2n + 10 ≤ cn

• (c − 2) n ≥ 10

• n ≥ 10/(c − 2)

• Pick c = 3 and n0 = 10

Page 10: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Big-Oh Notation Example

• Example: the function n2 is not O(n)

• n2 ≤ cn

• n ≤ c

• The above inequality cannot be satisfied since c must be a constant

Page 11: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

More Big-Oh Examples

Page 12: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Seven Important Functions

Page 13: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Sort Algorithm

• Bubble Sort

• Insertion Sort

• Merge Sort

• Quick Sort

Page 14: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Bubble Sort

1. Compare each pair of adjacent elements from the beginning of an array and, if they are in reversed order, swap them

2. If at least one swap has been done, repeat step 1.

Page 15: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Bubble Sort pseudocode

Page 16: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
Page 17: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Bubble Sort performance

• Worst-case & average-case: O(n2)

• Best-case: (over an already-sorted list) :O(n)

Page 18: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Bubble Sort Animation

http://www.sorting-algorithms.com/bubble-sort

Page 19: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Insertion Sort

Page 20: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Insertion Sort pseudocode

Page 21: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
Page 22: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Insertion Sort performance

• Worst-case & average-case: O(n2)

• Best-case: (over an already-sorted list) :O(n)

Page 23: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Insertion Sort Animation

http://www.sorting-algorithms.com/insertion-sort

Page 24: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Divide-and-Conquer

Page 25: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Merge Sort

Page 26: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Merging Two Sorted Sequences

Page 27: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Merge-Sort Execution

Page 28: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Merge-Sort Execution

Page 29: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Merge-Sort Execution

Page 30: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Merge-Sort Execution

Page 31: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Merge-Sort Execution

Page 32: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Merge-Sort Execution

Page 33: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Merge-Sort Execution

Page 34: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Merge-Sort Execution

Page 35: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Merge-Sort Execution

Page 36: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Merge-Sort Execution

Page 37: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Analysis of Merge-Sort

Page 38: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Quick-Sort

Page 39: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Quick-Sort Execution

Page 40: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Quick-Sort Execution

Page 41: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Quick-Sort Execution

Page 42: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Quick-Sort Execution

Page 43: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Quick-Sort Execution

Page 44: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Quick-Sort Execution

Page 45: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Quick-Sort Execution

Page 46: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Expected Running Time

Page 47: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Summary of Sorting Algorithms

Page 48: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

HW1. Perform the mergesort algorithm with the input

sequence: 30, 40, 24, 58, 48, 26, 11, 13. Use a diagram to illustrate the sorting process.

2. Repeat above question but using quicksort instead.

3. Repeat above question but using bubble sort instead.

4. Repeat above question but using insertion sort instead.

Page 49: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Graph

Page 50: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Adjacency MatrixStructure

Page 51: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Graph Traversal

• Depth First Search

• Breath First Search

Page 52: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Depth First Search

Page 53: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

DFS Example

Page 54: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

DFS Example

Page 55: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

DFS Algorithm

Page 56: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

Breadth-First Search

Page 57: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

BFS Example

Page 58: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

BFS Example

Page 59: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

BFS Example

Page 60: Hub 102 - Lesson 5 - Algorithm: Sorting & Searching

BFS Algorithm