Ordinamento 2021
Ordinamento 2021
- Per I da 0 a N-2
- Inserisci l’elemento più piccolo tra I e N-1 in posizione I
oppure
- Per I da 0 a N-2
- Inserisci l’elemento più grande tra 0 e N - 1 - I in posizione N - 1 - I
Consideriamo l’azione:
ORDINAMENTO DI UN VETTORE 1
ORDINAMENTO CON IL METODO SELECTION SORT
- Per I da 0 a N-2
- Individua la posizione IMAX dell’elemento più grande
tra 0 e N - 1 - I
- Scambia l’elemento in posizione IMAX con quello
in posizione N - 1 - I
#define N 8
#include <stdio.h>
main(){
int IMAX;
int I,J;
int V[N]={3,9,4,7,1,6,8,5}; /* vettore */
int temp;
ORDINAMENTO DI UN VETTORE 11
ORDINAMENTO CON IL METODO BUBBLE SORT - 1A VERSIONE
Primo Ciclo esterno:
Con N-1 “scambi” il più grande tra 0 e N-1 è portato in posizione N-1
3 9 4 7 1 6 8 5 array iniziale
3 9 4 7 1 6 8 5 for (J=0;J<N-I-1;J++)
3 4 9 7 1 6 8 5 if (V[J+1]<V[J]){
temp=V[J];
3 4 7 9 1 6 8 5 V[J]=V[J+1];
3 4 7 1 9 6 8 5 V[J+1]=temp;
3 4 7 1 6 9 8 5 }
3 4 7 1 6 8 9 5
3 4 7 1 6 8 5 9
3 9 4 7 1 6 8 5 array iniziale
3 4 7 1 6 8 5 9 dopo primo ciclo con I
3 4 1 6 7 5 8 9 ...
3 1 4 6 5 7 8 9
1 3 4 5 6 7 8 9
1 3 4 5 6 7 8 9
1 3 4 5 6 7 8 9
1 3 4 5 6 7 8 9 array finale
ORDINAMENTO DI UN VETTORE 3
ORDINAMENTO CON IL METODO BUBBLE SORT - 2A VERSIONE
Nell'esempio precedente si vede che le ultime due iterazioni non hanno
effetto, poichè l'array risultava già ordinato alla quarta iterazione. Per
migliorare la prima versione dell'ordinamento bubble è sufficiente
considerare che se durante una iterazione esterna non vengono effettuati
scambi l'array è già ordinato. Si introduce quindi la variabile Scambio per
segnala se non sono avvenuti scambi e quindi il processo di ordinamento
può terminare. La terminazione anticipata può avvenire programmando il
ciclo esterno con while anziché con for.
Esempio (La parte a sfondo grigio indica il sotto-array considerato al ciclo i-esimo)
3 9 4 7 1 6 8 5 array iniziale
3 4 7 1 6 8 5 9 dopo primo ciclo con j
3 4 1 6 7 5 8 9 ...
3 1 4 6 5 7 8 9
1 3 4 5 6 7 8 9
1 3 4 5 6 7 8 9 array finale
ORDINAMENTO DI UN VETTORE 11
RICERCA DICOTOMICA (O BINARIA)
• La tecnica di ricerca dicotomica può essere utilizzata su un vettore i cui
elementi sono ordinati.
ORDINAMENTO DI UN VETTORE 5
#include <stdio.h>
#define N 8
main(){
int V[N] = {1,6,7,13,21,23,34,35}; /* vettore ordinato*/
int D; /* elemento da cercare */
int Inizio,Fine,C;
int Trovato=0;
int I;
Ricerca dell’elemento 2
1 6 7 13 21 23 34 35 array ordinato
I C F Primo passo
I C F Secondo passo
I Terzo passo
F
C
F I I > F à Fine cliclo à Trovato=0
ORDINAMENTO DI UN VETTORE 11
ORDINAMENTO DI UN VETTORE: ESERCIZI
Dato un vettore di interi, con valori ripetuti:
5. INDIVIDUA VALORI CHE SI RIPETONO PIU’ VOLTE (CIOE’ CON MASSIMA FREQUENZA)
ORDINAMENTO DI UN VETTORE 7
#include <stdio.h>
#define N 8 /* dimensione del vettore */
main(){
int V[N]={2,9,9,7,2,2,8,5}; /* vettore */
int I,J; int temp; int NumeroValoriDistinti; int Frequenza; int MaxFrequenza;
for (I=0;I<N;I++)
if (I==N-1 || V[I+1] != V[I])
NumeroValoriDistinti++;
for (I=0;I<N;I++){
}
}
}
}
printf("\nMaxFrequenza=%d\n",MaxFrequenza);
// STAMPA DEI VALORI CHE SI RIPETONO PIU’ VOLTE (CIOE’ CON MASSIMA FREQUENZA) - DA COMPLETARE
for (I=0;I<N;I++){
}
}
}
ORDINAMENTO DI UN VETTORE 11
SOLUZIONE
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define N 8 /* dimensione del vettore */
main(){
int V[N]={2,9,9,7,2,2,8,5}; /* vettore */
int I,J;
int temp;
int NumeroValoriDistinti;
int Frequenza;
int MaxFrequenza;
for (I=0;I<N-1;I++)
for (J=0;J<N-I-1;J++)
if (V[J+1]<V[J]){
temp=V[J];
V[J]=V[J+1];
V[J+1]=temp;
}
NumeroValoriDistinti=0;
for (I=0;I<N;I++)
if (I==N-1 || V[I+1] != V[I])
NumeroValoriDistinti++;
Frequenza=0;
for (I=0;I<N;I++){
Frequenza++;
if (I==N-1 || V[I+1] != V[I]){
printf("Valore %d Frequenza %d\n",V[I],Frequenza );
Frequenza=0;
}
}
Frequenza=0;
MaxFrequenza=0;
for (I=0;I<N;I++){
Frequenza++;
if (I==N-1 || V[I+1] != V[I]){
printf("Valore %d Frequenza %d\n",V[I],Frequenza );
if (Frequenza>MaxFrequenza)
MaxFrequenza=Frequenza;
Frequenza=0;
}
ORDINAMENTO DI UN VETTORE 9
}
printf("\nMaxFrequenza=%d\n",MaxFrequenza);
// STAMPA DEI VALORI CHE SI RIPETONO PIU’ VOLTE (CIOE’ CON MASSIMA FREQUENZA)
Frequenza=0;
for (I=0;I<N;I++){
Frequenza++;
if (I==N-1 || V[I+1] != V[I]){
if (Frequenza==MaxFrequenza)
printf("\n Valore %d ha max frequenza\n",V[I]);
Frequenza=0;
}
}
system("PAUSE");
}
ORDINAMENTO DI UN VETTORE 11
/* DATE DUE STRINGHE STABILIRE SE UNA E' L'ANAGRAMMA DELL'ALTRA */
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define N 8 /* dimensione massima della stringa*/
main(){
char S1[N] = "Tiro";
char S2[N] = "Rito";
int I, J; char temp;
/* CONVERTIRE S1 e S2 in MAIUSCOLO */
if (strcmp(S1, S2) == 0)
printf("\n\n OK .... una e' l'anagramma dell'altra");
system("PAUSE");
}
ORDINAMENTO DI UN VETTORE 11
VETTORI DI STRINGHE
è un (tipo) vettore con una coppia di stringhe (cioè con due stringhe);
si ipotizza che
/* l’elemento (stringa) 0 è il cognome, l’elemento (stringa) 1 è il nome */
TIPOCOGNOMENOME PERSONE[N]
0 TIPOSTRINGA TIPOSTRINGA
1 TIPOSTRINGA TIPOSTRINGA
2 TIPOSTRINGA TIPOSTRINGA
…
N_1 TIPOSTRINGA TIPOSTRINGA
ORDINAMENTO DI UN VETTORE 11
#include <stdio.h>
#include <string.h>
#define N 5
#define LS 20
main(){
typedef char TIPOSTRINGA [20];
typedef TIPOSTRINGA TIPOCOGNOMENOME[2];
/* l’elemento (stringa) 0 è il cognome, l’elemento (stringa) 1 è il nome */
int I,J;
TIPOCOGNOMENOME temp2;
strcpy(temp,PERSONE[J][1]);
strcpy(PERSONE[J][1],PERSONE[J+1][1]);
strcpy(PERSONE[J+1][1], temp);
}
ORDINAMENTO DI UN VETTORE 13
NOTE
Non è possibile neanche fare la copia di PERSONE[J] in temp2 tramite strcpy, cioè se
facciamo
strcpy(temp2, PERSONE[0]);
In temp2 viene copiato solo il contenuto di PERSONE[0] fino al fine stringa, cioè solo il
cognome. Per controllare se stampiamo i due elementi di temp2
printf("\n %-20s %-20s ", temp2[0] ,temp2[1]);
otteniamo
Verrebbe fatto solo lo scambio dei cognomi e non dei nomi, come si può verificare
printf("\n %-20s %-20s ", PERSONE[1][0] , PERSONE[1][1]);
printf("\n %-20s %-20s ", PERSONE[2][0] , PERSONE[2][1]);
si ottiene
ORDINAMENTO DI UN VETTORE 11