SlideServe
Menu
Préférences
© SlideServe · v2026

Array Ricerca Ordinamento Fusione

adelio avatar
20 diapositives · 356 Views · Oct 03, 2014

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

Array Ricerca Ordinamento Fusione - slide 1
01 / 20Array Ricerca Ordinamento Fusione
1 / 17

Télécharger la présentation

Array Ricerca Ordinamento Fusione
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. ArrayRicerca Ordinamento Fusione Nicola Fanizzi Laboratorio - Corso di Programmazione (B) C.d.L. in Informatica DIB - Università degli Studi di Bari

  2. Array • costruttore di tipo strutturato di elementi omogenei di uno stesso tipo base raggiungibili tramite un indicea: 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. ArrayDichiarazioni • forma generale dichiarazione variabili array<nome-array>: array[<indice1>..<indice2>] of <tipo>; • dichiarazione anonima:a, b, vett: array[1..100] of integer; • tipi di Array definiti dall'utentetype <nome-tipo-array> = array[<indice1>..<indice2>] of <tipo>;sarà quindi possibile dichiarare variabili array:var <nome-variabile-array>: <nome-tipo-array>;

  4. 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. ArrayStatistiche Voti

  5. 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. ArrayConteggio Punteggi

  6. Tipi Sottointervallo • tipi di Array definiti dall'utentetype <nome-tipo-subrange> = <const1>..<const2>; • dove <const1> e <const2> 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. Array Multidimensionali • forma generale dichiarazione variabili array<var-array>: array[<range1>,<range2>] of <tipo>; • tipi di Array bidimensionali definiti dall'utentetype <nome-tipo-array> = array [<range1>,<range2>] of <tipo>;sarà quindi possibile dichiarare variabili array:var <nome-variabile-array>: <nome-tipo-array>; • utilizzo:const n = 4; m = 3; type TipoMatricearray [1..n,1..m] of real;var a,b,c: TipoMatrice;

  8. Array MultidimensionaliImmissione 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. 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<=MAXLINEE) and (n>=1); repeat write('Num. colonne: '); readln(m); until (m<=MAXCOLONNE) and (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. Array MultidimensionaliImmissione+Stampa

  10. 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. Array MultidimensionaliProdotto di Matrici

  11. 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. . ArrayRicercaCompleta(Sequenziale)

  12. 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. ArrayOrdinamento a Bolle (bubblesort)

  13. 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. ArrayOrdinamento e Ricerca Binaria

  14. 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; ArrayFusione di Vettori

  15. 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. ArrayFusione di Vettori (2)

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

  17. Insertion SortRealizzazione 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. 20 35 18 8 14 41 3 39 3 3 8 8 14 14 18 35 20 18 35 39 20 39 41 41 14 35 3 8 20 41 18 39 Shell Sort • idea: muovere elementi disordinati a grandi distanze verso la posizione che compete • ordinare elementi che si trovano a distanza n/2 poi n/4, n/8,… e così via fino ad 1 n/2=44 catene di lunghezza 2 n/4=22 catene di lunghezza 4 n/8=11 catene di lunghezza 8

  19. Shell Sort (2) • 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 end end

  20. Shell SortRealizzazione 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 begin inc := 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 end end.