5
Introduzione agli algoritmi e strutture dati 2/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2005 – The McGraw-Hill Companies srl Algoritmi come soluzioni di problemi computazionali. INTRODUZIONE Esempio1: problema dell’ordinamento. Input: a 1 ,a 2 ,...,a n Output: a' 1 ,a' 2 ,...,a' n permutazione (riarrangiamento) di

Introduzione agli algoritmi e strutture dati 2/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2005 – The McGraw-Hill Companies srl

Embed Size (px)

Citation preview

Page 1: Introduzione agli algoritmi e strutture dati 2/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2005 – The McGraw-Hill Companies srl

Introduzione agli algoritmi e strutture dati 2/edT. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein

Copyright © 2005 – The McGraw-Hill Companies srl

Algoritmi come soluzioni di problemi computazionali.

INTRODUZIONE

Esempio1: problema dell’ordinamento.

Input: a1,a2,...,an

Output: a'1,a'2,...,a'n permutazione (riarrangiamento) di a1,a2,...,an tale che a'1 a'2 ... a'n.

Page 2: Introduzione agli algoritmi e strutture dati 2/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2005 – The McGraw-Hill Companies srl

Introduzione agli algoritmi e strutture dati 2/edT. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein

Copyright © 2005 – The McGraw-Hill Companies srl

Soluzione1: Algoritmo InsertionSort.

InsertionSort (A) n lunghezza[A] for j 2 to n do

inserisce A[j] nella sequenza ordinata A[1..j-1]x A[j]

i j - 1while i 1 and x < A[i] do A[i+1] A[i] i i – 1A[i+1] x

Page 3: Introduzione agli algoritmi e strutture dati 2/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2005 – The McGraw-Hill Companies srl

Introduzione agli algoritmi e strutture dati 2/edT. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein

Copyright © 2005 – The McGraw-Hill Companies srl

InsertionSort (A)

n lunghezza[A] for j 2 to n do

inserisce A[j] nella sequenza ordinata A[1..j-1] x A[j] i j – 1 while i 1 and x < A[i] do

A[i+1] A[i] i i – 1

A[i+1] x

void InsertionSort (vector<tipo> A) { int i,j,n = A.size(); tipo x; for (j = 1; j < n; j++) { // inserisce A[j] nella sequenza // ordinata A[0..j-1] x = A[j]; i = j – 1; while (i >= 0 && x < A[i]) { A[i+1] = A[i]; i--; } A[i+1] = x; } }

Page 4: Introduzione agli algoritmi e strutture dati 2/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2005 – The McGraw-Hill Companies srl

Introduzione agli algoritmi e strutture dati 2/edT. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein

Copyright © 2005 – The McGraw-Hill Companies srl

x 5 8 4 7 1 3 6

5 x 8 4 7 1 3 6 2

2 5 x 4 7 1 3 6 8

2 5 8 x 7 1 3 6 4

2 x 5 8 7 1 3 6

2 5 x 4 7 1 3 6

2 4 5 8 x 1 3 6 7

2 4 5 x 8 1 3 6

5 2 8 4 7 1 3 6

2 5 8 4 7 1 3 6

2 5 8 4 7 1 3 6

2 4 5 8 7 1 3 6

InsertionSort (A) n lunghezza (A) for j 2 to n do inserisce A[j] nella sequenza ordinata A[1..j-1] x A[j] i j – 1 while i 1 and x < A[i] do A[i+1] A[i] i i – 1 A[i+1] x

Page 5: Introduzione agli algoritmi e strutture dati 2/ed T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein Copyright © 2005 – The McGraw-Hill Companies srl

Introduzione agli algoritmi e strutture dati 2/edT. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein

Copyright © 2005 – The McGraw-Hill Companies srl

2 4 5 7 8 x 3 6 1

x 2 4 5 7 8 3 6

1 2 4 5 7 8 x 6 3

1 2 x 4 5 7 8 6

1 2 3 4 5 7 8 x 6

1 2 3 4 5 x 7 8

1 2 3 4 5 6 7 8

2 4 5 7 8 1 3 6

1 2 4 5 7 8 3 6

1 2 3 4 5 7 8 6

InsertionSort (A) n lunghezza (A) for j 2 to n do inserisce A[j] nella sequenza ordinata A[1..j-1] x A[j] i j – 1 while i 1 and x < A[i] do A[i+1] A[i] i i – 1 A[i+1] x

2 4 5 x 8 1 3 6

Continua…