Aggiornato Ricorsione

This commit is contained in:
Giacomo R. 2023-05-29 20:56:56 +02:00
parent 76aa0080c6
commit 4c8c038eec
1 changed files with 80 additions and 17 deletions

View File

@ -12,13 +12,15 @@ public class Ricorsione {
* @param pos posizione dell'ultimo elemento
* @return quanti numeri dispari sono presenti nell'array
*/
public static int contaDispari(int[] x, int pos) {
private static int contaDispari(int[] x, int pos) {
if(pos < 0) return 0;
int contatore = (x[pos] % 2 == 1) ? 1 : 0;
return contatore + contaDispari(x, pos - 1);
return ((x[pos] % 2 == 1) ? 1 : 0) + contaDispari(x, pos - 1);
}
public static int contaDispari(int[] x) {
return contaDispari(x, x.length - 1);
}
/**
* metodo statico ricorsivo che restituisce la somma degli elementi
* di un array di interi
@ -26,13 +28,15 @@ public class Ricorsione {
* @param pos posizione dell'ultimo elemento
* @return somma degli elementi dell'array
*/
public static int somma(int[] x, int pos) {
private static int somma(int[] x, int pos) {
if(pos < 0) return 0;
int contatore = x[pos];
return contatore + somma(x, pos - 1);
return x[pos] + somma(x, pos - 1);
}
public static int somma(int[] x) {
return somma(x, x.length - 1);
}
/**
* metodo statico ricorsivo che verifica se una stringa x è contenuta
* nella stringa y
@ -41,7 +45,7 @@ public class Ricorsione {
* @param pos posizione iniziale
* @return x è contenuta in y
*/
public static boolean cerca(String x, String y, int pos) {
private static boolean cerca(String x, String y, int pos) {
if (y.length() - pos < x.length()) return false;
if (y.substring(pos, pos + x.length()).equals(x)) {
@ -50,12 +54,71 @@ public class Ricorsione {
return cerca(x, y, pos + 1);
}
public static boolean cerca(String x, String y) {
return cerca(x, y, 0);
}
public static int potenzaS(int a, int b) {
if(b==0) return 1;
else if(a%b == 0) {
int parz = potenzaS(a, b/2);
return parz * parz;
}
return potenzaS(a, b-1);
}
public static int potenzaD(int a, int b) {
if(b == 0) return 1;
return a * potenzaD(a, b-1);
}
public static <T extends Comparable> int binSearch(T[] x, T el) {
int min = 0;
int max = x.length -1;
while(min <= max) {
int med = (max+min)/2;
int tmp = el.compareTo(x[med]);
if(tmp == 0) return med;
if(tmp < 0) max = med - 1;
if(tmp > 0) min = med + 1;
}
return -1;
}
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5, 6};
System.out.println(contaDispari(array, array.length - 1));
System.out.println(somma(array, array.length - 1));
System.out.println(cerca("Test", "Testare", 0));
int[] array = {1, 6, 4, 5};
System.out.println(contaDispari(array));
System.out.println(somma(array));
System.out.println(cerca("Test", "Testare"));
System.out.println(binSearch());
int base = 2, esponente = 30;
////////
long startTimeD = System.nanoTime();
potenzaD(base, esponente);
long endTimeD = System.nanoTime();
long timeElapsedD = endTimeD - startTimeD;
System.out.print("D time: " + timeElapsedD + " ns");
////////
long startTimeS = System.nanoTime();
potenzaS(base, esponente);
long endTimeS = System.nanoTime();
long timeElapsedS = endTimeS - startTimeS;
System.out.println(" - S time: " + timeElapsedS + " ns");
System.out.println("Best time: " + ((timeElapsedD > timeElapsedS) ? "Smart" : "Dumb"));
////////
}
}