Compare commits
24 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
aa78d2efea | |
|
|
27d0058226 | |
|
|
2271da82c5 | |
|
|
7bb5e63f97 | |
|
|
c9de4120b1 | |
|
|
b91dd9f0fd | |
|
|
3cfcc9d9c1 | |
|
|
0dc80b3b47 | |
|
|
c349e21796 | |
|
|
0d33b585c4 | |
|
|
5e6c583719 | |
|
|
0c179912de | |
|
|
719520bb7d | |
|
|
f9282ab522 | |
|
|
a73d42271e | |
|
|
10438f9e7a | |
|
|
44d1dd85c2 | |
|
|
0a74c20f07 | |
|
|
2906cd48f6 | |
|
|
57d9834299 | |
|
|
a0d41fb658 | |
|
|
1c3ee04b4a | |
|
|
390eca6e8d | |
|
|
2cf90939f6 |
|
|
@ -1,3 +0,0 @@
|
||||||
public static void main(String[] args){
|
|
||||||
System.out.println("Test");
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>PR2-Hummel-3024753</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
||||||
|
|
@ -0,0 +1,13 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="module" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="src" path="src"/>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
|
||||||
|
<classpathentry kind="output" path="bin"/>
|
||||||
|
</classpath>
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
/bin/
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>Bank-Beispiel</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
||||||
|
|
@ -0,0 +1,52 @@
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class Bubblesort {
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
public static int[] bubblesort(int[] arr) {
|
||||||
|
|
||||||
|
boolean ready = false;
|
||||||
|
|
||||||
|
while (!ready) {
|
||||||
|
ready = true;
|
||||||
|
|
||||||
|
for (int i = 0; i < arr.length -1; i++) {
|
||||||
|
|
||||||
|
if (arr[i] > arr[i+1]) {
|
||||||
|
ready = false;
|
||||||
|
|
||||||
|
int m = arr[i];
|
||||||
|
arr[i] = arr[i+1];
|
||||||
|
arr[i+1] = m;
|
||||||
|
} // if
|
||||||
|
|
||||||
|
} // for
|
||||||
|
|
||||||
|
System.out.println(Arrays.toString(arr));
|
||||||
|
} // while
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} // End of Class
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,94 @@
|
||||||
|
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
public class InsertionSort {
|
||||||
|
public static final Logger log = Logger.getLogger(InsertionSort.class.getName());
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int[] arr={44,55,22,33,11,44,2135,2341325,9876,0};
|
||||||
|
insertionSortLSG(arr);
|
||||||
|
|
||||||
|
for(int i = 0; i < arr.length; i++) System.out.println(arr[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static int[] insertionSortLSG(int[] sortieren) {
|
||||||
|
log.info("dann mal los!");
|
||||||
|
int count = 0;
|
||||||
|
int temp;
|
||||||
|
for (int i = 1; i < sortieren.length; i++) {
|
||||||
|
temp = sortieren[i];
|
||||||
|
int j = i;
|
||||||
|
while (j > 0 && sortieren[j - 1] > temp) {
|
||||||
|
count++;
|
||||||
|
sortieren[j] = sortieren[j - 1];
|
||||||
|
j--; }
|
||||||
|
|
||||||
|
sortieren[j] = temp;
|
||||||
|
log.info("getauscht");
|
||||||
|
}
|
||||||
|
log.info("n = " + sortieren.length + " -> Vergleiche: " + count);
|
||||||
|
System.out.println(count);
|
||||||
|
return sortieren;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static void insertionsorttest(int[] arr){
|
||||||
|
int tmp;
|
||||||
|
for(int i =1; i <arr.length; i++){
|
||||||
|
tmp = arr[i];
|
||||||
|
int j = i;
|
||||||
|
|
||||||
|
while(j > 0 && arr[j-1]>tmp){
|
||||||
|
arr[j] = arr[j-1];
|
||||||
|
}
|
||||||
|
arr[j]=tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void insertsort(int[] arr){
|
||||||
|
int tmp;
|
||||||
|
for(int i = 0; i < arr.length-1; i++){
|
||||||
|
if(arr[i+1] < arr[i]){
|
||||||
|
tmp=arr[i+1];
|
||||||
|
|
||||||
|
int j=i;
|
||||||
|
while(j>0 && arr[j]>tmp){
|
||||||
|
arr[j+1]= arr[j];
|
||||||
|
j--;
|
||||||
|
}
|
||||||
|
arr[j]=tmp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static int[] insertSort(int[] arr){
|
||||||
|
int tmp;
|
||||||
|
for(int pos = 0; pos < arr.length-1; pos++){
|
||||||
|
tmp=arr[pos+1]; // +1 da sonst im durchlaufen / while eine Array Out of Bounds Acception kommt
|
||||||
|
int durch=pos;
|
||||||
|
while(durch>=0 && arr[durch]>tmp){
|
||||||
|
arr[durch+1]=arr[durch];
|
||||||
|
durch--;
|
||||||
|
}
|
||||||
|
arr[durch+1]=tmp;
|
||||||
|
}
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,22 @@
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
|
||||||
|
public class InsertionSortTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() {
|
||||||
|
int[] arr = {9, 7, 6, 5, 4, 2, 1, 0};
|
||||||
|
int[] fin = {0, 1, 2, 4, 5, 6, 7, 9};
|
||||||
|
|
||||||
|
int[] komp={111111,94854,555,444,123};
|
||||||
|
int[] kompSort={123,444,555,94854,111111};
|
||||||
|
|
||||||
|
Assert.assertArrayEquals(InsertionSort.insertionSortLSG(arr), fin);
|
||||||
|
Assert.assertArrayEquals(InsertionSort.insertSort(arr), fin);
|
||||||
|
Assert.assertArrayEquals(InsertionSort.insertSort(komp), kompSort);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,70 @@
|
||||||
|
|
||||||
|
public class Knoten {
|
||||||
|
|
||||||
|
private int wert;
|
||||||
|
private Knoten nachfolger;
|
||||||
|
|
||||||
|
Knoten(int wert){
|
||||||
|
this.wert = wert;
|
||||||
|
this.nachfolger=null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void einfuegenAmEnde(int wert) {
|
||||||
|
if(this.nachfolger == null) {
|
||||||
|
Knoten k = new Knoten(wert);
|
||||||
|
this.nachfolger=k;
|
||||||
|
}else {
|
||||||
|
nachfolger.einfuegenAmEnde(wert);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public int auslesenAnPos(int pos) throws Exception {
|
||||||
|
|
||||||
|
int r = 0;
|
||||||
|
// sind wir bereits an der Pos angekommen?
|
||||||
|
if(pos == 0) {
|
||||||
|
r= this.wert;
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if(pos >0 && nachfolger !=null) {
|
||||||
|
// nein! zähler verringern und an Nachfolger weitergeben
|
||||||
|
pos--;
|
||||||
|
// haben wir noch Nachfolger an die wir es weitergeben können?
|
||||||
|
|
||||||
|
if(nachfolger==null) {
|
||||||
|
|
||||||
|
throw new IndexOutOfBoundsException("Illegal Statement");
|
||||||
|
}else {
|
||||||
|
nachfolger.auslesenAnPos(pos);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return r;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void loeschenAnPos(int pos) throws Exception{
|
||||||
|
if(this.nachfolger==null) {
|
||||||
|
throw new IndexOutOfBoundsException();
|
||||||
|
}
|
||||||
|
if(pos>1) {
|
||||||
|
this.nachfolger = nachfolger.nachfolger;
|
||||||
|
}else {
|
||||||
|
pos --;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getWert() {return this.wert;}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// End of Class
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,24 @@
|
||||||
|
|
||||||
|
public class LinkedList {
|
||||||
|
private Knoten kopf;
|
||||||
|
|
||||||
|
LinkedList(int wert){
|
||||||
|
kopf = new Knoten(wert);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void einfuegenAmEnde(int wert) {
|
||||||
|
kopf.einfuegenAmEnde(wert);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int auslesenAnPos(int pos) throws Exception {
|
||||||
|
|
||||||
|
return kopf.auslesenAnPos(pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void loeschenAnPos(int pos) throws Exception {
|
||||||
|
kopf.loeschenAnPos(pos);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,41 @@
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.function.ThrowingRunnable;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class LinkedListTest {
|
||||||
|
|
||||||
|
private static final ThrowingRunnable IndexOutOfBoundsException = null;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void test() throws Exception {
|
||||||
|
LinkedList L = new LinkedList(11);
|
||||||
|
L.einfuegenAmEnde(10);
|
||||||
|
L.einfuegenAmEnde(9);
|
||||||
|
L.einfuegenAmEnde(8);
|
||||||
|
L.einfuegenAmEnde(7);
|
||||||
|
|
||||||
|
Assert.assertNotSame(3, L.auslesenAnPos(3));
|
||||||
|
Assert.assertNotSame(3, L.auslesenAnPos(5));
|
||||||
|
Assert.assertNotSame(3, L.auslesenAnPos(7));
|
||||||
|
Assert.assertNotSame(4, L.auslesenAnPos(6));
|
||||||
|
|
||||||
|
|
||||||
|
LinkedList R = new LinkedList(10);
|
||||||
|
R.einfuegenAmEnde(3);
|
||||||
|
R.einfuegenAmEnde(4);
|
||||||
|
R.einfuegenAmEnde(5);
|
||||||
|
R.einfuegenAmEnde(6);
|
||||||
|
R.einfuegenAmEnde(7);
|
||||||
|
|
||||||
|
Assert.assertNotSame(3, R.auslesenAnPos(0));
|
||||||
|
Assert.assertNotSame(3, R.auslesenAnPos(2));
|
||||||
|
Assert.assertNotSame(3, R.auslesenAnPos(5));
|
||||||
|
Assert.assertNotSame(4, R.auslesenAnPos(2));
|
||||||
|
|
||||||
|
Assert.assertEquals(10, R.auslesenAnPos(0));
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,51 @@
|
||||||
|
|
||||||
|
public class QuickSort {
|
||||||
|
|
||||||
|
public static void quickSort(int[] arr, int low, int high) {
|
||||||
|
if (low < high) {
|
||||||
|
// Pivot-Index bestimmen
|
||||||
|
int pivotIndex = partition(arr, low, high);
|
||||||
|
|
||||||
|
// Linke Seite sortieren
|
||||||
|
quickSort(arr, low, pivotIndex - 1);
|
||||||
|
|
||||||
|
// Rechte Seite sortieren
|
||||||
|
quickSort(arr, pivotIndex + 1, high);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int partition(int[] arr, int low, int high) {
|
||||||
|
int pivot = arr[high]; // letztes Element als Pivot
|
||||||
|
int i = low - 1;
|
||||||
|
|
||||||
|
for (int j = low; j < high; j++) {
|
||||||
|
if (arr[j] <= pivot) {
|
||||||
|
i++;
|
||||||
|
|
||||||
|
// Tauschen
|
||||||
|
int temp = arr[i];
|
||||||
|
arr[i] = arr[j];
|
||||||
|
arr[j] = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pivot an richtige Position setzen
|
||||||
|
int temp = arr[i + 1];
|
||||||
|
arr[i + 1] = arr[high];
|
||||||
|
arr[high] = temp;
|
||||||
|
|
||||||
|
return i + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int[] arr = {8, 3, 1, 7, 0, 10, 2};
|
||||||
|
|
||||||
|
quickSort(arr, 0, arr.length - 1);
|
||||||
|
|
||||||
|
for (int num : arr) {
|
||||||
|
System.out.print(num + " ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//End of Class
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,77 @@
|
||||||
|
import java.util.LinkedList;
|
||||||
|
public class RadixSort {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int[] arr = {10,9,8,7,6,5,4,3,2,1};
|
||||||
|
radixSort(arr);
|
||||||
|
for(int i : arr) System.out.println(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
//int Arr
|
||||||
|
public static int[] radixSort(int[] arr) {
|
||||||
|
int größteZ=arr[0];
|
||||||
|
|
||||||
|
//Buckets als Listen anlegen
|
||||||
|
LinkedList<Integer> B0 = new LinkedList<>();
|
||||||
|
LinkedList<Integer> B1 = new LinkedList<>();
|
||||||
|
LinkedList<Integer> B2 = new LinkedList<>();
|
||||||
|
LinkedList<Integer> B3 = new LinkedList<>();
|
||||||
|
LinkedList<Integer> B4 = new LinkedList<>();
|
||||||
|
LinkedList<Integer> B5 = new LinkedList<>();
|
||||||
|
LinkedList<Integer> B6 = new LinkedList<>();
|
||||||
|
LinkedList<Integer> B7 = new LinkedList<>();
|
||||||
|
LinkedList<Integer> B8 = new LinkedList<>();
|
||||||
|
LinkedList<Integer> B9 = new LinkedList<>();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
for(int i = 0; i < arr.length; i++) {
|
||||||
|
if(arr[i] > größteZ) größteZ=arr[i];
|
||||||
|
}
|
||||||
|
int cnt=1;
|
||||||
|
while(größteZ / 10 >0) {
|
||||||
|
größteZ= größteZ%10;
|
||||||
|
cnt++;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
for(int i = 0; i< cnt; i++) {
|
||||||
|
//Befuellen der Buckets;
|
||||||
|
for(int d = 0; d< arr.length; d++ ) {
|
||||||
|
int r = arr[i] % 10;
|
||||||
|
switch(r) {
|
||||||
|
case 0: B0.add(arr[i]);
|
||||||
|
case 1: B1.add(arr[i]);
|
||||||
|
case 2: B2.add(arr[i]);
|
||||||
|
case 3: B3.add(arr[i]);
|
||||||
|
case 4: B4.add(arr[i]);
|
||||||
|
case 5: B5.add(arr[i]);
|
||||||
|
case 6: B6.add(arr[i]);
|
||||||
|
case 7: B7.add(arr[i]);
|
||||||
|
case 8: B8.add(arr[i]);
|
||||||
|
case 9: B9.add(arr[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Buckets durchlaufen und wieder einsortierten in die ursprüngliche Arr
|
||||||
|
int e =0;
|
||||||
|
for(int in : B0) {arr[e]= in; e++;}
|
||||||
|
for(int in : B1) {arr[e]= in; e++;}
|
||||||
|
for(int in : B2) {arr[e]= in; e++;}
|
||||||
|
for(int in : B3) {arr[e]= in; e++;}
|
||||||
|
for(int in : B4) {arr[e]= in; e++;}
|
||||||
|
for(int in : B5) {arr[e]= in; e++;}
|
||||||
|
for(int in : B6) {arr[e]= in; e++;}
|
||||||
|
for(int in : B7) {arr[e]= in; e++;}
|
||||||
|
for(int in : B8) {arr[e]= in; e++;}
|
||||||
|
for(int in : B9) {arr[e]= in; e++;}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,22 @@
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class RadixSortTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void test() {
|
||||||
|
int[] sort = {9,8,7,6,5,4,3,2,1,0};
|
||||||
|
int[] sortS = {0,1,2,3,4,5,6,7,8,9};
|
||||||
|
|
||||||
|
int[] sort2 = {9,27,10,8,7,6,5,4,3,2,1,0};
|
||||||
|
int[] sortS2 = {0,1,2,3,4,5,6,7,8,9,10,27};
|
||||||
|
|
||||||
|
Assert.assertEquals(sortS, RadixSort.radixSort(sort));
|
||||||
|
Assert.assertEquals(sortS2, RadixSort.radixSort(sort2));
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,96 @@
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
public class SelectionSort {
|
||||||
|
public static final Logger log = Logger.getLogger(SelectionSort.class.getName());
|
||||||
|
|
||||||
|
|
||||||
|
private boolean change;
|
||||||
|
private int temp;
|
||||||
|
|
||||||
|
public static void main(String[] args){
|
||||||
|
int[] arr = {89,6,5,435,6734,234,5,3467,68,65};
|
||||||
|
selectionsortUebungcgpt(arr);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sortLSG(int data[])
|
||||||
|
{
|
||||||
|
for (int i = 0; i < data.length - 1; i++) {
|
||||||
|
System.out.println(i + 1 + ". iteration");
|
||||||
|
// Find the index of the minimum in unsorted part of array
|
||||||
|
int minIndex = i;
|
||||||
|
for (int j = i + 1; j < data.length; j++) {
|
||||||
|
if (data[j] < data[minIndex])
|
||||||
|
minIndex = j;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Swap the minimum element with first element from unsorted part, unless they are the same
|
||||||
|
if(i != minIndex) {
|
||||||
|
System.out.print("Found minimum: " + data[minIndex] + ", swapping with " + data[i] + " => ");
|
||||||
|
temp = data[minIndex];
|
||||||
|
data[minIndex] = data[i];
|
||||||
|
data[i] = temp;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
System.out.print("Minimum: " + data[minIndex] + " is on the right spot, no swapping => ");
|
||||||
|
printArray(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to print an array
|
||||||
|
public void printArray(int data[])
|
||||||
|
{
|
||||||
|
for (int i = 0; i < data.length; i++)
|
||||||
|
System.out.print(data[i] + " ");
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void selectionsortUebungcgpt(int[] arr) {
|
||||||
|
log.info("Los gehts's!");
|
||||||
|
int count =0;
|
||||||
|
for (int i = 0; i < arr.length; i++) {
|
||||||
|
int minIndex = i;
|
||||||
|
|
||||||
|
int k = i;
|
||||||
|
while (k < arr.length) {
|
||||||
|
if (arr[k] < arr[minIndex]) {
|
||||||
|
minIndex = k;
|
||||||
|
}
|
||||||
|
count++;
|
||||||
|
k++; // ❗ ganz wichtig
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tauschen
|
||||||
|
int temp = arr[minIndex];
|
||||||
|
arr[minIndex] = arr[i];
|
||||||
|
arr[i] = temp;
|
||||||
|
log.info(i + ". Element getauscht mit " + arr[minIndex]);
|
||||||
|
}
|
||||||
|
count += arr.length;
|
||||||
|
log.info("n = " + arr.length + " -> Vergleiche: " + count);
|
||||||
|
System.out.println(count);
|
||||||
|
}
|
||||||
|
// |||
|
||||||
|
// Richtig!!! VVV
|
||||||
|
|
||||||
|
public static void selectsort(int[] arr){
|
||||||
|
int tmp;
|
||||||
|
for(int i = 0; i < arr.length-1; i++){
|
||||||
|
tmp=arr[i];
|
||||||
|
int pos = i;
|
||||||
|
int j = i;
|
||||||
|
while(j < arr.length){
|
||||||
|
if(arr[j]<tmp){
|
||||||
|
tmp=arr[j];
|
||||||
|
pos=j;
|
||||||
|
}
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
arr[pos]=arr[i];
|
||||||
|
arr[i]=tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class bubbletest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() {
|
||||||
|
|
||||||
|
int arr[] = {3,2,5};
|
||||||
|
int fin[]= {2,3,5};
|
||||||
|
|
||||||
|
Assert.assertArrayEquals(Bubblesort.bubblesort(arr), fin);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue