Compare commits

..

1 Commits
main ... master

Author SHA1 Message Date
Thomas Smits 352045e28b Updated examples 2026-02-02 16:41:48 +01:00
1141 changed files with 88 additions and 76 deletions

View File

@ -1,5 +1,7 @@
package pr2.algorithmen.sorting;
import pr2.algorithmen.sort.Sorter;
public class BubbleSort implements Sorter {
@Override

View File

@ -1,5 +1,7 @@
package pr2.algorithmen.sorting;
import pr2.algorithmen.sort.Sorter;
public class InsertionSort implements Sorter {
@Override

View File

@ -1,9 +1,11 @@
package pr2.algorithmen.sorting;
import pr2.algorithmen.sort.Sorter;
public class MergeSort implements Sorter {
void msort(int[] array, int le, int ri, int[] helper) {
int i, j, k;
int i, j;
if (ri > le) {
// zu sortierendes Feld teilen
int mid = (ri + le) / 2;
@ -13,17 +15,17 @@ public class MergeSort implements Sorter {
msort(array, mid + 1, ri, helper);
// Hilfsfeld aufbauen
for (k = le; k <= mid; k++) {
for (int k = le; k <= mid; k++) {
helper[k] = array[k];
}
for (k = mid; k < ri; k++) {
for (int k = mid; k < ri; k++) {
helper[ri + mid - k] = array[k + 1];
}
// Ergebnisse mischen über Hilfsfeld
i = le; j = ri;
for (k = le; k <= ri; k++) {
for (int k = le; k <= ri; k++) {
if (helper[i] < helper[j]) {
array[k] = helper[i++];
} else {

View File

@ -1,5 +1,7 @@
package pr2.algorithmen.sorting;
import pr2.algorithmen.sort.Sorter;
public class QuickSort implements Sorter {
int partition(int[] array, int u, int o, int p) {

View File

@ -1,21 +1,23 @@
package pr2.algorithmen.sorting;
import pr2.algorithmen.sort.Sorter;
public class SelectionSort implements Sorter {
@Override
public void sort(int[] data) {
int marker = data.length - 1;
while (marker >= 0) {
int pos = data.length - 1;
while (pos >= 0) {
// bestimme größtes Element
int max = 0;
for (int i = 1; i <= marker; i++) {
if (data[i] > data[max]) {
max = i;
int indexMax = 0;
for (int i = 0; i <= pos; i++) {
if (data[i] > data[indexMax]) {
indexMax = i;
}
}
// tausche array[marker] mit diesem Element
swap(data, marker, max);
marker--;
// tausche array[pos] mit diesem Element
swap(data, pos, indexMax);
pos--;
}
}

View File

@ -5,6 +5,10 @@ public interface Sorter {
void sort(int[] data);
default void swap(int[] data, int i1, int i2) {
if (i1 == i2) {
return;
}
int tmp = data[i1];
data[i1] = data[i2];
data[i2] = tmp;

View File

@ -1,5 +1,12 @@
package pr2.algorithmen.sorting;
import pr2.algorithmen.sort.impl.BubbleSort;
import pr2.algorithmen.sort.impl.InsertionSort;
import pr2.algorithmen.sort.impl.MergeSort;
import pr2.algorithmen.sort.impl.QuickSort;
import pr2.algorithmen.sort.impl.SelectionSort;
import pr2.algorithmen.sort.Sorter;
import java.util.*;
public class TestSorting {
@ -15,9 +22,9 @@ public class TestSorting {
}
}
static long testAlgorithm(Class clazz, int size, int runs) {
static long testAlgorithm(Class<?> clazz, int size, int runs) {
Sorter s;
pr2.algorithmen.sort.Sorter s;
var data = new int[size];
for (int i = 0; i < data.length; i++) {
@ -40,15 +47,13 @@ public class TestSorting {
}
long runtime = System.nanoTime() - start;
return runtime;
return System.nanoTime() - start;
}
public static void measure(int size_steps, int max_size, int runs) {
public static void measure(int sizeSteps, int maxSize, int runs) {
System.out.println("Runs\tBS\tIS\tMS\tQS\tSS");
for (int size = 0; size <= max_size; size += size_steps) {
for (int size = 0; size <= maxSize; size += sizeSteps) {
long bs = testAlgorithm(BubbleSort.class, size, runs);
long is = testAlgorithm(InsertionSort.class, size, runs);
long ms = testAlgorithm(MergeSort.class, size, runs);
@ -58,7 +63,6 @@ public class TestSorting {
}
}
public static void main(String[] args) {
measure(10, 1000, 100);
final int size = 1000;

View File

@ -1,4 +1,3 @@
package pr2.exceptions.callstack;
import java.io.FileNotFoundException;
@ -11,26 +10,23 @@ import java.sql.SQLException;
public class HandleOrDeclare {
public void openFile(String dateiName)
throws IOException, FileNotFoundException {
throws IOException, FileNotFoundException {
// Datei öffnen
}
public void datenSchreiben(String dateiName, String sqlStatement)
throws FileNotFoundException, IOException, SQLException {
throws FileNotFoundException, IOException, SQLException {
openFile(dateiName);
// Mit Datenbank arbeiten
}
public void dateiAnlegen(String dateiName)
throws FileNotFoundException, IOException {
throws FileNotFoundException, IOException {
try {
datenSchreiben(dateiName, "SELECT * FROM x");
}
catch (SQLException ex) {
} catch (SQLException ex) {
// Datenbank Problem beheben ;-)
}
}
}
public void userInterface() {
@ -38,12 +34,10 @@ public class HandleOrDeclare {
try {
dateiAnlegen(dateiName);
}
catch (FileNotFoundException ex) {
// Benutzer erneut nach namen Fragen
}
catch (IOException ex) {
// Benutzer auf Problem hinweisen
} catch (FileNotFoundException ex) {
// Benutzer:in erneut nach namen Fragen
} catch (IOException ex) {
// Benutzer:in auf Problem hinweisen
}
}

View File

@ -3,13 +3,13 @@
*/
package pr2.exceptions.junit;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Disabled;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Assertions;
public class BrokenTest {
@Test
@Disabled
public void testKaputt() {
String s1 = new String("Hallo");
assertEquals("Hallo", s1);

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Some files were not shown because too many files have changed in this diff Show More