La presentazione è in caricamento. Aspetta per favore

La presentazione è in caricamento. Aspetta per favore

Array Ricerca Ordinamento Fusione Nicola Fanizzi Laboratorio - Corso di Programmazione (B) C.d.L. in Informatica DIB - Università degli Studi di Bari.

Presentazioni simili


Presentazione sul tema: "Array Ricerca Ordinamento Fusione Nicola Fanizzi Laboratorio - Corso di Programmazione (B) C.d.L. in Informatica DIB - Università degli Studi di Bari."— Transcript della presentazione:

1 Array Ricerca Ordinamento Fusione Nicola Fanizzi Laboratorio - Corso di Programmazione (B) C.d.L. in Informatica DIB - Università degli Studi di Bari

2 2 Array costruttore di tipo strutturato di elementi omogenei di uno stesso tipo base raggiungibili tramite un indice a: array[1..6] of integer; accesso ai singoli elementi (da trattare come variabili del tipo base): max:= a[1]; for i:= 2 to 6 do if a[i] > max then max := a[i]; dichiarazione anonima: a, b, vett: array[1..100] of integer;

3 3 Array Dichiarazioni forma generale dichiarazione variabili array : array[.. ] of ; dichiarazione anonima: a, b, vett: array[1..100] of integer; tipi di Array definiti dall'utente type = array[.. ] of ; sarà quindi possibile dichiarare variabili array: var : ;

4 4 Array Statistiche Voti program magMinMed (input, output); var voti: array[1..6] of integer; i, max, min: integer; media: real; begin write('VOTI Studenti'); writeln; {immissione} for i := 1 to 6 do begin write('Voto ',i,'° studente: '); readln(voti[i]); end; max := voti[1]; for i:=2 to 6 do if voti[i]>max then max := voti[i]; min := voti[1]; for i := 2 to 6 do if voti[i] < min then min := voti[i]; media := voti[1]; for i:=2 to 6 do media:= media+voti[i]; media := media/6; writeln('Max: ', max); writeln('Min: ', min); writeln('Media: ', media:7:2); readln; end.

5 5 Array Conteggio Punteggi program punteggi (input, output); const MAX_CONC = 1000; MIN_PUN = 1; MAX_PUN = 10; var prova1, prova2, finale: array [1..MAX_CONC] of real; i, n: integer; begin repeat write('N.concorrenti:'); readln(n); until (n>=1)and(n<=MAX_CONC); for i:=1 to n do begin writeln; writeln('Concorrente', i); repeat write('Prima prova:'); readln(prova1[i]); until (prova1[i]>=MIN_PUN) and (prova1[i]<=MAX_PUN); repeat write('Seconda prova: '); readln(prova2[i]); until (prova2[i] >= MIN_PUN) and (prova2[i] <= MAX_PUN); end; for i:=1 to n do finale[i]:= (prova1[i]+prova2[i])/2; writeln('RISULTATI'); for i:=1 to n do writeln(i, '° Conc.: ', prova1[i]:7:2, prova2[i]:7:2, finale[i]:7:2); readln; end.

6 6 Tipi Sottointervallo tipi di Array definiti dall'utente type =.. ; –dove e sono di un tipo enumerabile (int, char, …) esempi: type eta = 1..150; var anni: eta; type minuscole = 'a'..'z'; var c: minuscole; type numero_concorrenti = 1..MAX_CONC; var i: numero_concorrenti;

7 7 Array Multidimensionali forma generale dichiarazione variabili array : array[, ] of ; tipi di Array bidimensionali definiti dall'utente type = array [, ] of ; sarà quindi possibile dichiarare variabili array: var : ; utilizzo: const n = 4; m = 3; type TipoMatrice array [1..n,1..m] of real; var a,b,c: TipoMatrice;

8 8 Array Multidimensionali Immissione program matrice (input, output); var mat: array[1..4, 1..3] of integer; i, j: integer; begin writeln('INIZIALIZZAZIONE DELLA MATRICE'); writeln; for i:=1 to 4 do for j:=1 to 3 do begin write('Inserisci mat[,i,,,j,]='); readln(mat[i,j]); end; for i:=1 to 4 do begin writeln; for j:=1 to 3 do write(mat[i,j]:5); end; end.

9 9 Array Multidimensionali Immissione+Stampa program matrice2 (input, output); const MAXLINEE = 100; MAXCOLONNE = 100; type matrice = array [1..MAXLINEE,1..MAXCOLONNE] of real; var mat: matrice; n, m, i, j : integer; begin repeat write('Num. linee: '); readln(n); until (n =1); repeat write('Num. colonne: '); readln(m); until (m =1); writeln('INIZIALIZZAZIONE DELLA MATRICE'); for i:=1 to n do for j:=1 to m do begin write('Inserisci mat[,i,,,j,]='); readln(mat[i,j]); end; for i:=1 to n do begin writeln; for j:=1 to m do write(mat[i,j]:5); end; end.

10 10 Array Multidimensionali Prodotto di Matrici program prodottoMatrici (input, output); const N = 4; P = 3; M = 5; var mat1: array[1..N, 1..P] of integer; mat2: array[1..P, 1..M] of integer; pmat: array[1..N, 1..M] of integer; i, j, k: integer; begin writeln('PRIMA MATRICE'); for i:=1 to N do for j:=1 to P do begin write(mat1: linea ', i, ' colonna ', j, ': '); readln(mat1[i,j]); end; writeln('SECONDA MATRICE'); for i:=1 to P do for j:=1 to M do begin write(mat2: linea ', i, ' colonna ', j, ': '); readln(mat2[i,j]); end; for i:=1 to N do for j:=1 to M do begin pmat[i, j] := 0; for k:=1 to P do pmat[i,j] := pmat[i,j] + mat1[i, k]*mat2[k,j]; end;... write('MATRICE PRODOTTO'); for i:=1 to N do begin writeln; for j:=1 to M do write(pmat[i, j]:5); end; readln; end.

11 11 Array Ricerca Completa (Sequenziale) program ricercaCompleta (input, output); const MAXEL = 1000; type vettore = array[1..MAXEL] of integer; var vet: vettore; i, n, c : integer; trovato: boolean; begin { Immissione} repeat writeln; write('Numero elementi: '); readln(n); until (n>=1) and (n<=MAXEL); { Inizializzazione} for i:=1 to n do vet[i]:=(37*i+c) mod n; write('Elemento target:'); readln(c); { Ricerca sequenziale } i := 1; trovato := FALSE; while not trovato and (i<=n) do if c=vet[i] then trovato := TRUE else i := i+1; if trovato then begin writeln; writeln(c,' presente in posizione ', i); end else begin writeln; writeln('non presente!') end; readln; end..

12 12 Array Ordinamento a Bolle (bubblesort) program ordinamentoIngenuo (input, output); const MAXEL = 1000; type vettore = array[1.. MAXEL] of integer; var vet: vettore; i, j, n: integer; aux: integer; begin repeat writeln; write('Num. elementi: '); readln(n); until (n>=1) and (n<=MAXEL); { Immissione } for i:=1 to n do begin writeln; write('Immettere elemento ',i,'°:'); readln(vet[i]); end; { Ordinamento } for j := 1 to (n-1) do for i := (j+1) to n do if vet[j] > vet[i] then begin {scambio valori}; aux := vet[j]; vet[j] := vet[i]; vet[i] := aux end; { Visualizzazione } writeln; for i:=1 to n do writeln(vet[i]); readln; end.

13 13 Array Ordinamento e Ricerca Binaria program ricercaBinaria (input, output); const L=8; var vet: array[1..L] of char; i, n, p: integer; basso, alto, pos: integer; scambio : boolean; aux,ele: char; begin n := L; for i:=1 to n do begin write(carattere ',i,:'); readln(vet[i]); end; p := n; repeat scambio := FALSE; for i:=1 to n-1 do if vet[i]>vet[i+1] then begin aux := vet[i]; vet[i] := vet[i+1]; vet[i+1] := aux; scambio := TRUE; p := i + 1; end; n := p; until scambio=FALSE; write('Elemento target: '); readln(ele); { ricerca binaria } n := L; alto := 1; basso := n; pos := -1; repeat i := (alto+basso) DIV 2; if vet[i]=ele then pos := i else if vet[i]<ele then alto := i+1 else basso := i-1; until (alto>basso) or (pos<>-1); writeln; if pos <> -1 then write('Elemento ', ele, presente in posizione ', pos) else write('Elemento non presente!'); readln; end.

14 14 Array Fusione di Vettori program fusione (input, output); const MAX = 1000; var vet1, vet2: array [1..MAX] of char; vet3: array [1..2000] of char; n, m: integer; aux: char; i, j, w, p, n1:INTEGER; m1, i1, j1: integer; k: boolean; begin repeat write('Lunghezza primo vettore: '); readln(n); until (n>=1) and (n<=MAX); for i:=1 to n do begin write('Inserire ', i, 'º elemento di vet1: '); readln(vet1[i]); end; repeat write('Lunghezza secondo vettore: '); readln(m); until (m>=1) and (m<=MAX); for i:=1 to m do begin write('Inserire ', i, 'º elemento vet2: '); readln(vet2[i]); end;

15 15 Array Fusione di Vettori (2) p := n; n1 := n; repeat k := FALSE; for i:=1 to n1-1 do if vet1[i]>vet1[i+1] then begin aux := vet1[i]; vet1[i] := vet1[i+1]; vet1[i+1] := aux; k := TRUE; p := i+1; end; n1 := p; until k = FALSE; p := m; m1 := m; repeat k := FALSE; for i:=1 to m1-1 do if vet2[i]>vet2[i+1] then begin aux := vet2[i]; vet2[i] := vet2[i+1]; vet2[i+1] := aux; k := TRUE; p := i+1; end; m1 := p; until k = FALSE; i := 1; j := 1; w := 1; repeat if vet1[i]<=vet2[j] then begin vet3[w] := vet1[i]; i := i+1; end else begin vet3[w] := vet2[j]; j := j+1; end; w := w+1; until (i>n) or (j>m); if i<n then for i1:=i to n do begin vet3[w] := vet1[i1]; w := w+1; end else for j1 := j to m do begin vet3[w] := vet2[j1]; w := w+1; end; for i:=1 to w-1 do writeln(vet3[i]); end.

16 16 Insertion Sort n idea: considerare l'array come diviso in 2 parti –parte ordinata –parte disordinata n ad ogni passata: un elemento x passa dalla parte disordinata a quella ordinata nella posizione che gli compete n x viene confrontato via via con gli elementi alla sua sinistra fino a trovare la sua posizione n quindi, gli altri elementi vengono traslati a destra parte ordinata x parte disordinata

17 17 Insertion Sort Realizzazione in Pascal program Insertion_sort (input,output); const MAX=1000; var a:array[1..MAX] of real; i,j,n,p: integer; x,min: real; begin write('Insertion Sort'); repeat write('Immetti n:'); readln(n); until n <= MAX; {immissione} for i := 1 to n do begin write('a[',i,']:'); readln(a[i]); end; {ricerca minimo} min := a[1]; p := 1; for i := 2 to n do if a[i] < min then begin min := a[i]; p := i end; a[p] := a[1]; a[1] := min; {ordinamento} for i := 3 to n do begin x := a[i]; j:=i; while x < a[j-1] do begin a[j]:=a[j-1]; j:=j-1 end; a[j]:=x; end {stampa} end.

18 18 Shell Sort n idea: muovere elementi disordinati a grandi distanze verso la posizione che compete n ordinare elementi che si trovano a distanza n/2 poi n/4, n/8,… e così via fino ad 1 20351881441339 14353820411839 38143518392041 38141820353941 n/2=4 4 catene di lunghezza 2 n/4=2 2 catene di lunghezza 4 n/8=1 1 catene di lunghezza 8

19 19 Shell Sort (2) n per ordinare una singola catena si possono usare algoritmi già visti: –insertion sort <-- pochi scambi –bubble sort inc = n; while inc > 1 do begin inc := inc div 2; for j := 1 to inc do begin k := j+inc; while k <= n do begin x := a[k]; {trova la pos. per x --> corrente} a[corrente] := x; k := k+inc; end

20 20 Shell Sort Realizzazione in Pascal program Shell_sort (input,output); const MAX=1000; var a:array[1..MAX] of real; i,j,k,n,inc: integer; prec,corr: integer; inserito: boolean; x: real; begin repeat write('Immetti n:'); readln(n); until n <= MAX; for i := 1 to n do begin write('a[',i,']:'); readln(a[i]); end; inc := n; while inc > 1 do begininc := inc DIV 2; for j := 1 to inc do begin k := j + inc; while k <= n do begin x := a[k]; corr := k; prec := corr–inc; inserito := false; while (prec>=j) and not inserito do if x<a[prec] then begin a[corr]:=a[prec]; corr:=prec; prec:=prec–inc end else inserito:=true; a[corr]:=x;k:=k+inc; end end.


Scaricare ppt "Array Ricerca Ordinamento Fusione Nicola Fanizzi Laboratorio - Corso di Programmazione (B) C.d.L. in Informatica DIB - Università degli Studi di Bari."

Presentazioni simili


Annunci Google