|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Aug 2006
Messaggi: 1192
|
[Java]Algoritmi array
Ciao!
Approfitto ancora una volta del vostro aiuto Siccome la prof ha detto che gli algoritmi per operare su array sono fondamentali, ho pensato di farmi uno "schemino" che li raccolga tutti..solo che non sono sicura che siano giusti! Ho cercato di scrivere gli algoritmi (soprattutto i primi) senza guardare gli appunti, non so quante castronerie troverete per gli ultimi (quicksort e mergesort), invece, mi sono aiutata con delle pseudocodifiche che ho trovato in internet, altrimenti non ce ne sarei uscita più.. Se aveste voglia di darci un'occhiata, mi fareste un grande favore Codice:
ARRAY (numerici)
>> RADDOPPIO <<
int[] tmp = new int[array_vecchio.length * 2];
for (int i=0; i<array_vecchio.length;i++)
tmp[i]= array_vecchio[i];
array_vecchio = tmp;
>> RIDIMENSIONAMENTO <<
int[] tmp = new int [new_length];
for (int i=0; i<tmp.length; i++)
tmp[i]= array_vecchio[i];
array_vecchio=tmp;
>> AGGIUNGERE UN ELEMENTO
IN POSIZIONE x <<
int[] tmp = new int[array_vecchio.length +1];
for (int i = 0; i<x ;i++)
tmp[i] = array_vecchio[i];
tmp[i+1] = nuovoelem;
for (i=x; i< array_vecchio.length; i++)
tmp [i]=array_vecchio[i];
>> RICERCA LINEARE
dell'elemento x <<
for (int i= 0; i< array.length; i++)
if (array[i]==x)
System.out.println ("L'elemento è stato trovato
in posizione" + i );
>> RICERCA DICOTOMICA (in array ordinato)
dell'elemento x <<
int inizio=0;
int fine=array.length-1;
while (inizio <= fine)
{
int medio = (inizio+fine)/2;
if (x == array[medio])
System.out.println ("L'elemento è stato trovato
in posizione" + medio );
else if (x < array[medio])
fine = medio;
else if (x > medio)
inizio = medio;
}
System.out.println ("L'elemento è stato trovato
in posizione" + medio );
>> ORDINAMENTO per SELEZIONE <<
for (int i =0; i< array.length-1; i++)
if (array[i]>array[i+1])
{
int tmp = array[i+1];
array[i+1]=array[i];
array[i]= tmp;
}
>> ORDINAMENTO per INSERIMENTO <<
boh, non l'ho capito
>> BUBBLESORT <<
int j=array.length -1;
while (j>0)
{
for (int i=0; i< array.length; i++)
{
if (array[i]>array[i+1])
{
int tmp = array[i+1];
array[i+1]=array[i];
array[i]= tmp;
}
}
j--;
}
>> QUICKSORT <<
public (static?) void quickSort(int[] array, int b, int a)
{
int i = b;
int f = a;
int pivot = array[(i+f)/2];
while (i <= f)
{
while (array[i]<pivot)
i++;
while (array[f]>pivot)
f--;
if (i <= f)
{
int tmp = array[i];
array[i]=array[f];
array[f]= tmp;
i++;
f--;
}
}
quickSort(array, i, pivot);
quickSort (array, pivot+1, f);
}
>> MERGESORT <<
public static void sort (int[] array_da_ordinare)
{
mergeSort (array_da_ordinare, 0, array_da_ordinare.length-1);
}
public static void mergeSort (int[] array, int a, int b)
{
int p=a;
int u = b;
if (p<u)
{
int m = (p+u)/2;
mergesort (array, p, m);
mergesort (array, m+1, u);
merge (array, p, m, u)
}
public static void merge (array, a, m1, b)
{
int p1= a;
int u1= b;
int k = m+1;
int i = 0;
int[] s = new int[array.length];
while (k<=q && p1<=k-1)
{
if (array[p1]<array[k])
{
s[i]= array[p1];
p1++;
}
else
{
s[i]= array[k];
k++;
}
i++;
}
while (p1<=k-1)
{
s[i]=a[p1];
i++;
p1++;
}
while ( k<=u1)
{
s[i]=a[k];
i++;
k++;
}
for (i=0; i< u1; i++)
array[p1+i]=s[i];
}
PS: Se qualcuno poi ha voglia di spiegarmi l'ordinamento per inserimento ben venga!Non riesco proprio a capire il meccanismo..
__________________
blue_blue: nickname proprio di persona, femminile, singolare "Wait, before you close the curtain/There’s still another game to play/And life is beautiful that way"(Beautiful that way, Noa & Nicola Piovani) Fiore, sei un mito
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 00:30.



















