Remove all solution markdown, task images, solution PDFs, and add renamed folder structure as part of THB-wide reorganization.
This commit is contained in:
14
uebung_02/src/util/ArrayTester.java
Normal file
14
uebung_02/src/util/ArrayTester.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package util;
|
||||
|
||||
public class ArrayTester {
|
||||
public static final void main(String[] args)
|
||||
{
|
||||
Util util = new Util();
|
||||
int num = 60;
|
||||
int[] array0 = new int[num];
|
||||
int[] array1 = new int[num];
|
||||
util.fillArrayRandom(array0, num*100);
|
||||
util.fillArrayRandom(array1, num*100);
|
||||
System.out.println(util.firstMatch(array0, array1));
|
||||
}
|
||||
}
|
||||
20
uebung_02/src/util/IntArrayList.java
Normal file
20
uebung_02/src/util/IntArrayList.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package util;
|
||||
|
||||
public class IntArrayList {
|
||||
int lenght = 4;
|
||||
int lastUnfilledPos = 0;
|
||||
int[] arr = new int[4];
|
||||
|
||||
public IntArrayList(){}
|
||||
|
||||
public void add(int a){
|
||||
if (lastUnfilledPos < lenght){
|
||||
arr[lastUnfilledPos] = a;
|
||||
lastUnfilledPos ++;
|
||||
}
|
||||
else {
|
||||
int[] arrTemp = new int[lenght*2];
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
156
uebung_02/src/util/Sorter.java
Normal file
156
uebung_02/src/util/Sorter.java
Normal file
@@ -0,0 +1,156 @@
|
||||
package util;
|
||||
|
||||
public class Sorter {
|
||||
public void sort(int[] array) {
|
||||
if (array == null || array.length <= 1) return;
|
||||
|
||||
int n = array.length;
|
||||
int[] temp = new int[n];
|
||||
|
||||
while (true) {
|
||||
int i = 0;
|
||||
int merges = 0;
|
||||
|
||||
while (i < n) {
|
||||
// Find first run
|
||||
int left = i;
|
||||
while (i + 1 < n && array[i] <= array[i + 1]) i++;
|
||||
int mid = i;
|
||||
i++;
|
||||
|
||||
if (i >= n) break;
|
||||
|
||||
// Find second run
|
||||
while (i + 1 < n && array[i] <= array[i + 1]) i++;
|
||||
int right = i;
|
||||
i++;
|
||||
|
||||
// Merge both runs
|
||||
merge(array, temp, left, mid, right);
|
||||
merges++;
|
||||
}
|
||||
|
||||
if (merges == 0) break;
|
||||
}
|
||||
}
|
||||
|
||||
private void merge(int[] array, int[] temp, int left, int mid, int right) {
|
||||
System.arraycopy(array, left, temp, left, right - left + 1);
|
||||
|
||||
int i = left;
|
||||
int j = mid + 1;
|
||||
int k = left;
|
||||
|
||||
while (i <= mid && j <= right) {
|
||||
if (temp[i] <= temp[j]) {
|
||||
array[k++] = temp[i++];
|
||||
} else {
|
||||
array[k++] = temp[j++];
|
||||
}
|
||||
}
|
||||
while (i <= mid) array[k++] = temp[i++];
|
||||
while (j <= right) array[k++] = temp[j++];
|
||||
}
|
||||
|
||||
public void mergeSort(int[] array) {
|
||||
if (array == null || array.length <= 1) return;
|
||||
int[] temp = new int[array.length];
|
||||
mergeSortRecursive(array, temp, 0, array.length - 1);
|
||||
}
|
||||
|
||||
private void mergeSortRecursive(int[] array, int[] temp, int left, int right) {
|
||||
if (left >= right) return;
|
||||
int mid = (left + right) >>> 1;
|
||||
mergeSortRecursive(array, temp, left, mid);
|
||||
mergeSortRecursive(array, temp, mid + 1, right);
|
||||
mergeDescending(array, temp, left, mid, right);
|
||||
}
|
||||
|
||||
private void mergeDescending(int[] array, int[] temp, int left, int mid, int right) {
|
||||
System.arraycopy(array, left, temp, left, right - left + 1);
|
||||
|
||||
int i = left;
|
||||
int j = mid + 1;
|
||||
int k = left;
|
||||
|
||||
while (i <= mid && j <= right) {
|
||||
if (temp[i] >= temp[j]) {
|
||||
array[k++] = temp[i++];
|
||||
} else {
|
||||
array[k++] = temp[j++];
|
||||
}
|
||||
}
|
||||
while (i <= mid) array[k++] = temp[i++];
|
||||
while (j <= right) array[k++] = temp[j++];
|
||||
}
|
||||
|
||||
public void insertionSort(int[] array) {
|
||||
if (array == null || array.length <= 1) return;
|
||||
for (int i = 1; i < array.length; i++) {
|
||||
int key = array[i];
|
||||
int j = i - 1;
|
||||
while (j >= 0 && array[j] < key) {
|
||||
array[j + 1] = array[j];
|
||||
j--;
|
||||
}
|
||||
array[j + 1] = key;
|
||||
}
|
||||
}
|
||||
|
||||
public void optimizedMergeSort(int[] array) {
|
||||
if (array == null || array.length <= 1) return;
|
||||
int[] temp = new int[array.length];
|
||||
optimizedMergeSortRecursive(array, temp, 0, array.length - 1);
|
||||
}
|
||||
|
||||
private void optimizedMergeSortRecursive(int[] array, int[] temp, int left, int right) {
|
||||
if (left >= right) return;
|
||||
if (right - left + 1 <= 10) {
|
||||
insertionSortRange(array, left, right);
|
||||
return;
|
||||
}
|
||||
int mid = (left + right) >>> 1;
|
||||
optimizedMergeSortRecursive(array, temp, left, mid);
|
||||
optimizedMergeSortRecursive(array, temp, mid + 1, right);
|
||||
mergeDescending(array, temp, left, mid, right);
|
||||
}
|
||||
|
||||
private void insertionSortRange(int[] array, int left, int right) {
|
||||
for (int i = left + 1; i <= right; i++) {
|
||||
int key = array[i];
|
||||
int j = i - 1;
|
||||
while (j >= left && array[j] < key) {
|
||||
array[j + 1] = array[j];
|
||||
j--;
|
||||
}
|
||||
array[j + 1] = key;
|
||||
}
|
||||
}
|
||||
|
||||
public void testMergeSort() {
|
||||
int[] array = new int[100];
|
||||
Util util = new Util();
|
||||
util.fillArrayRandom(array, 100);
|
||||
Sorter mySorter = new Sorter();
|
||||
mySorter.mergeSort(array);
|
||||
util.printArray(array);
|
||||
}
|
||||
|
||||
public void testOptimizedMergeSort() {
|
||||
int[] array = new int[100];
|
||||
Util util = new Util();
|
||||
util.fillArrayRandom(array, 100);
|
||||
Sorter mySorter = new Sorter();
|
||||
mySorter.optimizedMergeSort(array);
|
||||
util.printArray(array);
|
||||
}
|
||||
|
||||
public static final void main(String[] args) {
|
||||
int[] array = new int[100000000];
|
||||
Util util = new Util();
|
||||
util.fillArrayRandom(array, 10000000);
|
||||
Sorter mySorter = new Sorter();
|
||||
mySorter.sort(array);
|
||||
|
||||
}
|
||||
}
|
||||
153
uebung_02/src/util/Util.java
Normal file
153
uebung_02/src/util/Util.java
Normal file
@@ -0,0 +1,153 @@
|
||||
package util;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* Write a description of class Util here.
|
||||
*
|
||||
* @author (your name)
|
||||
* @version (a version number or a date)
|
||||
*/
|
||||
public class Util
|
||||
{
|
||||
public void printArray(int[] array)
|
||||
{
|
||||
for (int i = 0; i < array.length; ++i)
|
||||
{
|
||||
System.out.print(array[i]);
|
||||
|
||||
// Prüfen, ob wir nicht beim letzten Element sind.
|
||||
// Auf das letzte Element sollte kein Komma folgen.
|
||||
if (i < array.length - 1)
|
||||
{
|
||||
System.out.print(",");
|
||||
}
|
||||
}
|
||||
|
||||
// Nach Ausgabe brechen wir die Zeile um, damit folgende Ausgaben
|
||||
// in einer neuen Zeile beginnen.
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
public int randomInt(int upperLimit)
|
||||
{
|
||||
double random = Math.random(); // Zufallszahl zwischen 0.0 und 1.0 holen.
|
||||
|
||||
// Zufallszahl per Multiplikation auf 0.0-upperLimit skalieren
|
||||
random = random * upperLimit;
|
||||
|
||||
// Nachkommastellen abschneiden und in Ganzzahlwert (int) umwandeln.
|
||||
int result = (int) random;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public void fillArrayRandom(int[] array, int upperLimit)
|
||||
{
|
||||
for (int i = 0; i < array.length; ++i)
|
||||
{
|
||||
array[i] = randomInt(upperLimit);
|
||||
}
|
||||
}
|
||||
|
||||
public int[] concatArray(int[] array0, int[] array1)
|
||||
{
|
||||
int[] result = new int[array0.length + array1.length];
|
||||
|
||||
// Manuelles Kopieren mit Schleifen.
|
||||
for (int i = 0; i < array0.length; ++i)
|
||||
{
|
||||
result[i] = array0[i];
|
||||
}
|
||||
for (int i = 0; i < array1.length; ++i)
|
||||
{
|
||||
// Um array0.length verschoben einfügen.
|
||||
result[array0.length + i] = array1[i];
|
||||
}
|
||||
|
||||
// Alternative Lösung unter Verwendung von System.arraycopy().
|
||||
// System.arraycopy(array0, 0, result, 0, array0.length);
|
||||
// System.arraycopy(array1, 0, result, array0.length, array1.length);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public int findMax(int[] array) {
|
||||
int max = 0;
|
||||
boolean found = false;
|
||||
int loopRuns = 0;
|
||||
for (int i = 0; !found && i < array.length; ++i) {
|
||||
loopRuns++;
|
||||
if (array[i] > max) {
|
||||
max = array[i];
|
||||
}
|
||||
}
|
||||
return loopRuns;
|
||||
}
|
||||
|
||||
public int firstMatch(int[] array0, int[] array1) {
|
||||
int loopRuns = 0;
|
||||
for (int i = 0; i < array0.length; i++) {
|
||||
for (int j = 0; j < array1.length; j++) {
|
||||
loopRuns++;
|
||||
if (array0[i] == array1[j]) {
|
||||
return loopRuns;
|
||||
}
|
||||
}
|
||||
}
|
||||
return loopRuns;
|
||||
}
|
||||
|
||||
public void fillArrayRandomRecursively(int[] array, int start, int upperLimit)
|
||||
{
|
||||
if (array.length - start <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
array[start] = randomInt(upperLimit);
|
||||
fillArrayRandomRecursively(array, start + 1, upperLimit);
|
||||
}
|
||||
|
||||
public void testRandomFillRecursively()
|
||||
{
|
||||
Util util = new Util();
|
||||
int[] valueArray = new int[100];
|
||||
util.fillArrayRandomRecursively(valueArray, 0, 100);
|
||||
util.printArray(valueArray);
|
||||
}
|
||||
|
||||
public int findMaxBySorting(int[] array){
|
||||
Sorter sorter = new Sorter();
|
||||
sorter.sort(array);
|
||||
return array[array.length -1];
|
||||
}
|
||||
|
||||
public Integer findValueBySorting(int @NotNull [] array, int value){
|
||||
Sorter sorter = new Sorter();
|
||||
sorter.sort(array);
|
||||
int min = array[0];
|
||||
int max = array[array.length -1];
|
||||
if (value < min || value > max){
|
||||
return null;
|
||||
}
|
||||
|
||||
int low = 0;
|
||||
int high = array.length - 1;
|
||||
|
||||
while (low <= high) {
|
||||
int mid = (low + high) >>> 1;
|
||||
if (array[mid] == value) {
|
||||
return mid;
|
||||
} else if (array[mid] < value) {
|
||||
low = mid + 1;
|
||||
} else {
|
||||
high = mid - 1;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user