Docsity

Data Structures and algorithems, Lecture notes of Data Structures and Algorithms

Partial preview of the text

Download Data Structures and algorithems and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

Introduction: Analysis of

Algorithms, Insertion Sort,

Merge Sort

Lecture 1

Analysis of algorithms

The theoretical study of computer-program

performance and resource usage.

What’s more important than performance?

  • modularity
  • correctness
  • maintainability
  • functionality
  • robustness
    • user-friendliness
    • programmer time
    • simplicity
    • extensibility
    • reliability

The problem of sorting

Input: sequence  a 1 , a 2 , …, an  of numbers.

Example: Input: 8 2 4 9 3 6

Output: 2 3 4 6 8 9

Output: permutation  a' 1 , a' 2 , … , a'n  such

that a' 1  a' 2 …^  a'n.

Insertion Sort

INSERTION-SORT ( A , n ) ⊳ A [1.. n ] for j ← 1 to n do key ← A [ j ] i ← j – 1 while i > 0 and A [ i ] > key do A [ i+ 1] ← A [ i ] i ← i – 1 A [ i+ 1] = key

“pseudocode”

i j

key sorted

Example of Insertion Sort

Example of Insertion Sort

Example of Insertion Sort

Example of Insertion Sort

Example of Insertion Sort

Example of Insertion Sort

Example of Insertion Sort

2 3 4 6 8 9 done

Running time

  • The running time depends on the input: an already sorted sequence is easier to sort.
  • Parameterize the running time by the size of the input, since short sequences are easier to sort than long ones.
  • Generally, we seek upper bounds on the running time, because everybody likes a guarantee.

Machine-independent time

What is insertion sort’s worst-case time?

  • It depends on the speed of our computer:
    • relative speed (on the same machine),
    • absolute speed (on different machines).

BIG IDEA:

  • Ignore machine-dependent constants.
  • Look at growth of T ( n ) as n → ∞.

“Asymptotic Analysis”

Q -notation

  • Drop low-order terms; ignore leading constants.
  • Example: 3 n^3 + 90 n^2 – 5 n + 6046 = Q( n^3 )

Math: Q( g ( n )) = { f ( n ) : there exist positive constants c 1 , c 2 , and

n 0 such that 0  c 1 g ( n )  f ( n )  c 2 g ( n ) for all nn 0 }

Engineering: