Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

Here you will find the basics of data structures and algorithem

Typology: Lecture notes

2019/2020

Uploaded on 08/09/2020

unknown user
unknown user 🇮🇳

1 document

1 / 50

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction: Analysis of
Algorithms, Insertion Sort,
Merge Sort
Lecture 1
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

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: