Fixup
This commit is contained in:
parent
cc0e9bfb77
commit
9d57ebb781
38
Algorithm.java
Normal file
38
Algorithm.java
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
public class Algorithm {
|
||||||
|
// Nr 2
|
||||||
|
public int binarySearch(int[] arr, int left, int right, int target) {
|
||||||
|
if (left > right) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mid = left + (right - left) / 2;
|
||||||
|
|
||||||
|
if (arr[mid] == target) {
|
||||||
|
return mid;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (target < arr[mid]) {
|
||||||
|
return binarySearch(arr, left, mid - 1, target);
|
||||||
|
}
|
||||||
|
|
||||||
|
return binarySearch(arr, mid + 1, right, target);
|
||||||
|
}
|
||||||
|
|
||||||
|
// NR 1
|
||||||
|
public int ggt(int m, int n) {
|
||||||
|
while (n != 0) {
|
||||||
|
int rest = m % n;
|
||||||
|
m = n;
|
||||||
|
n = rest;
|
||||||
|
}
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
// NR 3
|
||||||
|
public long potenz(long a, int b) {
|
||||||
|
if (b == 0) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return a * potenz(a, b - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
38
Algorithms
38
Algorithms
@ -1,38 +0,0 @@
|
|||||||
public class Algorithm {
|
|
||||||
// Nr 2
|
|
||||||
public int binarySearch(int[] arr, int left, int right, int target) {
|
|
||||||
if (left > right) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int mid = left + (right - left) / 2;
|
|
||||||
|
|
||||||
if (arr[mid] == target) {
|
|
||||||
return mid;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (target < arr[mid]) {
|
|
||||||
return binarySearch(arr, left, mid - 1, target);
|
|
||||||
}
|
|
||||||
|
|
||||||
return binarySearch(arr, mid + 1, right, target);
|
|
||||||
}
|
|
||||||
|
|
||||||
// NR 1
|
|
||||||
public int ggt(int m, int n) {
|
|
||||||
while (n != 0) {
|
|
||||||
int rest = m % n;
|
|
||||||
m = n;
|
|
||||||
n = rest;
|
|
||||||
}
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
|
|
||||||
// NR 3
|
|
||||||
public long potenz(long a, int b) {
|
|
||||||
if (b == 0) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return a * potenz(a, b - 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -5,5 +5,12 @@ public class Main {
|
|||||||
System.out.println("Iterativ" + countDigits.countDigitsIterativ(index));
|
System.out.println("Iterativ" + countDigits.countDigitsIterativ(index));
|
||||||
System.out.println("Rekursiv" + countDigits.countDigitsRekursiv(index));
|
System.out.println("Rekursiv" + countDigits.countDigitsRekursiv(index));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
System.out.println("Nr 1 2 & 3:");
|
||||||
|
int[] arr = {1, 2, 4, 7, 9, 12, 15, 18, 20};
|
||||||
|
Algorithm algorithms = new Algorithm();
|
||||||
|
System.out.println("ggt: " + algorithms.ggt(8, 4));
|
||||||
|
System.out.println("binarySearch: " + algorithms.binarySearch(arr, 0, arr.length, 12));
|
||||||
|
System.out.println("potenz: " + algorithms.potenz(4, 4));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user