/**
Copyright (C) 2013 Alessandro Bugatti (alessandro.bugatti@istruzione.it)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/** file
* @brief Esempio di vettore per spiegare il concetto di oggetto
* @author Alessandro Bugatti
*
* @version 0.1
* @date Creazione 21/09/2013
* @date Ultima modifica 21/09/2013
*/
package vettoreordinamento;
import java.util.Random;
/**
* Programma per mostrare come funziona una classe con un esempio del vettore
* e dell'ordinamento
* @author Alessandro Bugatti
*/
public class VettoreOrdinamento {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Vettore a = new Vettore(10);
a.random_fill();
a.stampa();
a.sort();
a.stampa();
}
}
class Vettore{
int [] v;
int lunghezza;
/**
* Costruttore
* @param i Lunghezza del vettore che verrĂ creato
*/
Vettore(int i){
v = new int[i];
lunghezza = i;
}
/**
* Riempie il vettore di numeri casuali compresi tra 0 e 1000000
*/
void random_fill(){
Random random_generator = new Random();
for (int i = 0; i < lunghezza; i++)
v[i] = random_generator.nextInt(1000000);
}
/**
* Ordina i dati contenuti nel vettore utilizzando il bubble sort
*/
void sort()
{
int i,j;
for (i = 0; i < lunghezza; i++)
for (j=lunghezza - 1; j > i; j--)
if (v[j] < v[j-1])
{
int temp = v[j];
v[j] = v[j-1];
v[j-1] = temp;
}
}
/**
* Stampa il contenuto del vettore
*/
void stampa()
{
for ( int i = 0; i < lunghezza; i++)
System.out.println("v[" + i + "] = " + v[i]);
}
}