hub 102 - lesson 5 - algorithm: sorting & searching

Post on 16-Aug-2015

67 Views

Category:

Engineering

4 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Lesson 5Algorithm: Sorting and 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

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.

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

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

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

Growth Rates

• Growth rates of functions:

• Linear ≈ n

• Quadratic ≈ n2

• Cubic ≈ n3

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

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

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

More Big-Oh Examples

Seven Important Functions

Sort Algorithm

• Bubble Sort

• Insertion Sort

• Merge Sort

• Quick Sort

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.

Bubble Sort pseudocode

Bubble Sort performance

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

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

Bubble Sort Animation

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

Insertion Sort

Insertion Sort pseudocode

Insertion Sort performance

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

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

Insertion Sort Animation

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

Divide-and-Conquer

Merge Sort

Merging Two Sorted Sequences

Merge-Sort Execution

Merge-Sort Execution

Merge-Sort Execution

Merge-Sort Execution

Merge-Sort Execution

Merge-Sort Execution

Merge-Sort Execution

Merge-Sort Execution

Merge-Sort Execution

Merge-Sort Execution

Analysis of Merge-Sort

Quick-Sort

Quick-Sort Execution

Quick-Sort Execution

Quick-Sort Execution

Quick-Sort Execution

Quick-Sort Execution

Quick-Sort Execution

Quick-Sort Execution

Expected Running Time

Summary of Sorting Algorithms

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.

Graph

Adjacency MatrixStructure

Graph Traversal

• Depth First Search

• Breath First Search

Depth First Search

DFS Example

DFS Example

DFS Algorithm

Breadth-First Search

BFS Example

BFS Example

BFS Example

BFS Algorithm

top related