Aggiunto TestSameValues

This commit is contained in:
2023-01-26 22:55:11 +01:00
parent cc375e10fe
commit b566fbb8c8
20 changed files with 84815 additions and 0 deletions

View File

@ -0,0 +1,95 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package testsamevalues;
import java.util.Arrays;
/**
*
* @author gicorada
*/
public class Elenco {
int[] values;
public Elenco(int maxDim) {
values = new int[maxDim];
}
public int size() {
return values.length;
}
public void set(int i, int value) {
values[i] = value;
}
public int get(int i) {
return values[i];
}
public boolean sameValuesGiacomo(Elenco other) {
if(other.size() != this.size()) return false;
int dim = this.size();
int[] tmp = Arrays.copyOf(other.values, dim);
boolean trovato = false;
for(int x : values) {
trovato = false;
for(int i = 0; i < dim; i++) {
if(x == tmp[i]) {
trovato = true;
tmp[i] = Integer.MAX_VALUE;
break;
}
}
if(!trovato) return false;
}
return true;
}
public boolean sameValuesSort(Elenco other) {
if(other == null) return false;
if(other.size() != this.size()) return false;
int[] t = this.values.clone();
int[] o = other.values.clone();
Arrays.sort(t);
Arrays.sort(o);
for(int i = 0; i < this.size(); i++) {
if(t[i] != o[i]) return false;
}
return true;
}
public boolean sameValuesProf(Elenco other) {
if(other.size() != this.size()) return false;
int dim = this.size();
int[] tmp = Arrays.copyOf(other.values, dim);
boolean trovato = false;
for(int i = 0; i < dim; i++) {
for(int j = i; j < dim; j++) {
if(values[i] == tmp[j]) {
int k = tmp[j];
tmp[j] = tmp[i];
tmp[i] = k;
trovato = true;
break;
}
}
if(!trovato) return false;
}
return true;
}
}

View File

@ -0,0 +1,69 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package testsamevalues;
/**
*
* @author gicorada
*/
public class TestSameValues {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int dim = Integer.parseInt(args[0]);
Elenco elenco = new Elenco(dim + 1);
Elenco other = new Elenco(dim + 1);
for(int i = 0; i <= dim; i++) {
elenco.set(i, dim-i);
other.set(i, dim-i);
}
/*long startTimeG = System.nanoTime();
boolean giacomo = elenco.sameValuesGiacomo(other);
long endTimeG = System.nanoTime();*/
/*System.out.println("Giacomo: " + giacomo);*/
/*long timeElapsedG = endTimeG - startTimeG;*/
/*System.out.println("Giacomo time: " + timeElapsedG + " ns");*/
/*System.out.print(timeElapsedG);
System.out.print(",");*/
long startTimeS = System.nanoTime();
boolean withSort = elenco.sameValuesSort(other);
long endTimeS = System.nanoTime();
/*System.out.println("Sort: " + withSort);*/
long timeElapsedS = endTimeS - startTimeS;
/*System.out.println("Sort time: " + timeElapsedS + " ns");*/
System.out.print(timeElapsedS);
System.out.print(",");
long startTimeP = System.nanoTime();
boolean prof = elenco.sameValuesProf(other);
long endTimeP = System.nanoTime();
/*System.out.println("Prof: " + prof);*/
long timeElapsedP = endTimeP - startTimeP;
/*System.out.println("Prof time: " + timeElapsedP + " ns");*/
System.out.print(timeElapsedP);
System.out.println("");
}
}