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; package pr2.algorithmen.sorting;
import pr2.algorithmen.sort.Sorter;
public class BubbleSort implements Sorter { public class BubbleSort implements Sorter {
@Override @Override

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,5 +1,12 @@
package pr2.algorithmen.sorting; 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.*; import java.util.*;
public class TestSorting { 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]; var data = new int[size];
for (int i = 0; i < data.length; i++) { for (int i = 0; i < data.length; i++) {
@ -40,15 +47,13 @@ public class TestSorting {
} }
long runtime = System.nanoTime() - start; return System.nanoTime() - start;
return runtime;
} }
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"); 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 bs = testAlgorithm(BubbleSort.class, size, runs);
long is = testAlgorithm(InsertionSort.class, size, runs); long is = testAlgorithm(InsertionSort.class, size, runs);
long ms = testAlgorithm(MergeSort.class, size, runs); long ms = testAlgorithm(MergeSort.class, size, runs);
@ -58,7 +63,6 @@ public class TestSorting {
} }
} }
public static void main(String[] args) { public static void main(String[] args) {
measure(10, 1000, 100); measure(10, 1000, 100);
final int size = 1000; final int size = 1000;

View File

@ -1,53 +1,47 @@
package pr2.exceptions.callstack;
package pr2.exceptions.callstack;
import java.io.FileNotFoundException;
import java.io.FileNotFoundException; import java.io.IOException;
import java.io.IOException; import java.sql.SQLException;
import java.sql.SQLException;
/**
/** * @author Thomas Smits
* @author Thomas Smits */
*/ public class HandleOrDeclare {
public class HandleOrDeclare {
public void openFile(String dateiName)
public void openFile(String dateiName) throws IOException, FileNotFoundException {
throws IOException, FileNotFoundException { // Datei öffnen
// Datei öffnen }
}
public void datenSchreiben(String dateiName, String sqlStatement)
public void datenSchreiben(String dateiName, String sqlStatement) throws FileNotFoundException, IOException, SQLException {
throws FileNotFoundException, IOException, SQLException { openFile(dateiName);
// Mit Datenbank arbeiten
openFile(dateiName); }
// Mit Datenbank arbeiten
} public void dateiAnlegen(String dateiName)
throws FileNotFoundException, IOException {
public void dateiAnlegen(String dateiName) try {
throws FileNotFoundException, IOException { datenSchreiben(dateiName, "SELECT * FROM x");
} catch (SQLException ex) {
try { // Datenbank Problem beheben ;-)
datenSchreiben(dateiName, "SELECT * FROM x"); }
} }
catch (SQLException ex) {
// Datenbank Problem beheben ;-) public void userInterface() {
} String dateiName = askUser();
}
try {
public void userInterface() { dateiAnlegen(dateiName);
String dateiName = askUser(); } catch (FileNotFoundException ex) {
// Benutzer:in erneut nach namen Fragen
try { } catch (IOException ex) {
dateiAnlegen(dateiName); // Benutzer:in auf Problem hinweisen
} }
catch (FileNotFoundException ex) { }
// Benutzer erneut nach namen Fragen
} public String askUser() {
catch (IOException ex) { return null;
// Benutzer auf Problem hinweisen }
} }
}
public String askUser() {
return null;
}
}

View File

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