Guest User

Untitled

a guest
Jan 13th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.73 KB | None | 0 0
  1. package Esercitazione1;
  2.  
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.PrintStream;
  6. import java.util.Arrays;
  7. import java.util.Random;
  8.  
  9. public class OrdinaArray {
  10.  
  11.     /*
  12.      * Algoritmo bubblesort per l'ordinamento di un array di interi A
  13.      * usando come relazione d'ordine totale "<="
  14.      * @param A array passato all'algoritmo che deve essere ordinato
  15.      */
  16.     static int bubblesort(int A[]){
  17.         int numeroConfronti=0;
  18.         for(int i = 1; i <= A.length - 1; i++) {
  19.             boolean scambiAvvenuti = false;
  20.             for(int j = 1; j <= A.length - 1; j++) {
  21.                 numeroConfronti = numeroConfronti + 1;
  22.                 if( A[j] <= A[j - 1] ) {
  23.                     swap(A, j-1, j);
  24.                     scambiAvvenuti = true;
  25.                 }
  26.             }
  27.             if (!scambiAvvenuti) {
  28.                 break;
  29.             }
  30.         }
  31.         return numeroConfronti;
  32.     }
  33.    
  34.     /*
  35.      * Algoritmo selectionsort per l'ordinamento di un array di interi A
  36.      * usando come relazione d'ordine totale "<="
  37.      * @param A
  38.      */
  39.     static int selectionsort(int A[]) {
  40.         int numeroConfronti = 0;
  41.         for (int k = -1; k <= A.length - 2; k++) {
  42.             int min = k + 1;
  43.             for (int j = k + 2; j <= A.length - 1; j++) {
  44.                 if (A[j] < A[min]) {
  45.                     min = j;
  46.                 }
  47.                 numeroConfronti = numeroConfronti + 1;
  48.             }
  49.             if (min != k + 1) {
  50.                 swap(A, min, k + 1);
  51.             }
  52.         }
  53.         return numeroConfronti;
  54.     }
  55.    
  56.     /*
  57.      * algoritmo insertionsort per l'ordinamento di un array di interi A
  58.      * usando come relazione d'ordine totale "<="
  59.      * @param A
  60.      */
  61.     static int insertionsort(int A[]) {
  62.         int numeroConfronti = A.length;
  63.         int min = 0;
  64.         for (int i = 1; i < A.length - 1; i++) {
  65.             if (A[i] < A[min]) {
  66.                 min = i;
  67.             }
  68.         }
  69.        
  70.         swap(A, min, 0);
  71.        
  72.         for (int k = 1; k <= A.length - 2; k++) {
  73.             int x = A[k + 1];
  74.             int j;
  75.             for (j = k; j >= 1; j--) {
  76.                 numeroConfronti = numeroConfronti + 1;
  77.                 if (A[j] <= x) {
  78.                     break;
  79.                 }
  80.             }
  81.             for (int h = k; h >= j + 1; h--) {
  82.                 // shift a destra
  83.                 A[h + 1] = A[h];
  84.             }
  85.             A[j + 1] = x;
  86.         }
  87.         return numeroConfronti;
  88.     }
  89.  
  90.     /*
  91.      * Inizializza l'array di interi A con numeri interi generati in maniera casuale
  92.      * con valore tra 1 e A.length
  93.      * @param A
  94.      */
  95.     static void inizializzaArrayCasuale(int A[]){
  96.         Random g = new Random();
  97.         for (int i = 1; i < A.length; i++) {
  98.             int casuale = g.nextInt(A.length) + 1;
  99.             A[i] = casuale;
  100.         }
  101.     }
  102.    
  103.     /*
  104.      * Inizializza l'array di interi A con i numeri interi 1, 2, 3,..., A.length
  105.      * @param A
  106.      */
  107.     static void inizializzaArrayCrescente(int A[]){
  108.         for (int i = 0; i < A.length; i++) {
  109.             A[i] = i + 1;
  110.         }
  111.     }
  112.     /*
  113.      * Inizializza l'array di interi A con numeri interi A.length, A.length-1,...,2, 1
  114.      * @param A
  115.      */
  116.     static void inizializzaArrayDecrescente(int A[]){
  117.         int n = A.length;
  118.         for (int i = 0; i < A.length; i++) {
  119.             A[i] = n;
  120.             n--;
  121.         }
  122.     }
  123.  
  124.     /*
  125.      * Stampa gli elementi contenuti nell'array A
  126.      * @param A
  127.      */
  128.     static void stampaArray(int A[]){
  129.         for (int i = 0; i < A.length; i++) {
  130.             System.out.print(A[i] + " ");
  131.         }
  132.     }
  133.    
  134.     private static void swap(int A[], int a, int b) {
  135.         int tmp = A[a];
  136.         A[a] = A[b];
  137.         A[b] = tmp;
  138.     }
  139.    
  140.     /**
  141.      * @param args
  142.      */
  143.     public static void main(String[] args) {
  144.        
  145.         int maxDim, step;
  146.         int contatore;
  147.         maxDim=new Integer(args[0]).intValue();
  148.         step=new Integer(args[1]).intValue();
  149.        
  150.         try {
  151.            
  152.             FileOutputStream file = new FileOutputStream("statistica.csv");
  153.             PrintStream Output = new PrintStream(file);
  154.            
  155.             Output.println("dimArray; " +
  156.                     "BubbleNroConfrontiCasuale; BubbleNroConfrontiCrescenti; BubblenroConfrontiDecrescente; " +
  157.                     "SelectionNroConfrontiCasuale; SelectionNroConfrontiCrescenti; SelectionNroConfrontiDecrescente;");
  158.        
  159.             for(int i=step; i <= maxDim; i += step)
  160.             {
  161.                 int A[]=new int[i];
  162.                 String outString = "";
  163.                
  164.                 inizializzaArrayCasuale(A);
  165.                 int copy[] = Arrays.copyOf(A, A.length);
  166.                
  167.                 System.out.println("Array di dimensione " + i + " ordinato con BUBBLESORT");
  168.                 outString += String.valueOf(i) + ";";
  169.                 System.out.println("***************************************************");
  170.                 System.out.println("Uso del generatore di numeri casuali");
  171.                 stampaArray(A);
  172.                 contatore=bubblesort(A);
  173.                 System.out.print(": " + contatore + " confronti per ottenere ");
  174.                  outString += contatore + ";";
  175.                 stampaArray(A);
  176.                 System.out.println("");
  177.                 inizializzaArrayCrescente(A);
  178.                 stampaArray(A);
  179.                 contatore=bubblesort(A);
  180.                 System.out.print(": " + contatore + " confronti per ottenere ");
  181.                  outString += contatore + ";";
  182.                 stampaArray(A);
  183.                 System.out.println("");
  184.                 inizializzaArrayDecrescente(A);
  185.                 stampaArray(A);
  186.                 contatore=bubblesort(A);
  187.                 System.out.print(": " + contatore + " confronti per ottenere ");
  188.                  outString += contatore;
  189.                 stampaArray(A);
  190.                 System.out.println("");
  191.                 //SELECTION
  192.                 System.out.println("\n");
  193.                 System.out.println("Array di dimensione " + i + " ordinato con SELECIONSORT");
  194.                 System.out.println("***************************************************");
  195.                 System.out.println("Uso del generatore di numeri casuali");
  196.                 A = Arrays.copyOf(copy, copy.length);
  197.                 stampaArray(A);
  198.                 contatore = selectionsort(A);
  199.                 System.out.print(": " + contatore + " confronti per ottenere ");
  200.                 stampaArray(A);
  201.                 System.out.println("");
  202.                 inizializzaArrayCrescente(A);
  203.                 stampaArray(A);
  204.                 contatore = selectionsort(A);
  205.                 System.out.print(": " + contatore + " confronti per ottenere ");
  206.                 stampaArray(A);
  207.                 System.out.println("");
  208.                 inizializzaArrayDecrescente(A);
  209.                 stampaArray(A);
  210.                 contatore = selectionsort(A);
  211.                 System.out.print(": " + contatore + " confronti per ottenere ");
  212.                 stampaArray(A);
  213.                 System.out.println("\n");
  214.                 //INSERTION
  215.                 System.out.println("\n");
  216.                 System.out.println("Array di dimensione " + i + " ordinato con INSERTIONSORT");
  217.                 System.out.println("***************************************************");
  218.                 System.out.println("Uso del generatore di numeri casuali");
  219.                 A = Arrays.copyOf(copy, copy.length);
  220.                 stampaArray(A);
  221.                 contatore = insertionsort(A);
  222.                 System.out.print(": " + contatore + " confronti per ottenere ");
  223.                 stampaArray(A);
  224.                 System.out.println("");
  225.                 inizializzaArrayCrescente(A);
  226.                 stampaArray(A);
  227.                 contatore = insertionsort(A);
  228.                 System.out.print(": " + contatore + " confronti per ottenere ");
  229.                 stampaArray(A);
  230.                 System.out.println("");
  231.                 inizializzaArrayDecrescente(A);
  232.                 stampaArray(A);
  233.                 contatore = insertionsort(A);
  234.                 System.out.print(": " + contatore + " confronti per ottenere ");
  235.                 stampaArray(A);
  236.                 System.out.println("\n");
  237.                
  238.                 Output.println(outString);
  239.             }
  240.             Output.close();
  241.            
  242.         } catch(IOException e) {
  243.             System.out.println("Errore: " + e);
  244.             System.exit(1);
  245.         }
  246.     }
  247. }
Add Comment
Please, Sign In to add comment