Radix sort verzweiflung && Test für List

main
student 2026-04-01 13:31:22 +02:00
parent 3cfcc9d9c1
commit b91dd9f0fd
3 changed files with 102 additions and 14 deletions

View File

@ -1,7 +1,6 @@
public class Knoten {
private static final Exception IndexOutOfBoundsException = null;
private int wert;
private Knoten nachfolger;
@ -21,28 +20,30 @@ public class Knoten {
public int auslesenAnPos(int pos) throws Exception {
pos--;
if(nachfolger==null && pos>1) {
throw IndexOutOfBoundsException;
}
if(pos >0 ) {
int r = 0;
if(pos >0 && nachfolger !=null) {
pos--;
nachfolger.auslesenAnPos(pos);
}
else {
return this.wert;
r= this.wert;
return r;
}
if(nachfolger==null && pos>1) {
throw new IndexOutOfBoundsException("Illegal Statement");
}
return 0;
return r;
}
public void loeschenAnPos(int pos) throws Exception{
if(this.nachfolger==null) {
throw IndexOutOfBoundsException;
throw new IndexOutOfBoundsException();
}
if(pos>1) {
this.nachfolger = nachfolger.nachfolger;

View File

@ -20,7 +20,17 @@ class LinkedListTest {
LinkedList R = new LinkedList(10);
R.einfuegenAmEnde(3);
Assert.assertEquals(3, R.auslesenAnPos(2));
}
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.assertEquals(10, R.auslesenAnPos(0));
Assert.assertEquals(4, R.auslesenAnPos(2));
}
}

View File

@ -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 void 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++;}
}
}
}