Sort Algorithmen wurden implementiert

main
student 2026-03-25 14:07:57 +01:00
parent 2906cd48f6
commit 0a74c20f07
4 changed files with 84 additions and 46 deletions

View File

@ -33,47 +33,7 @@ public class Bubblesort {
public static int[] insertionSort(int[] sortieren) {
int temp = 0;
for(int i = 1; i < sortieren.length; i++) {
if(sortieren[i-1] > sortieren[i]) {
temp=sortieren[i];
sortieren[i]=sortieren[i-1];
sortieren[i-1]=temp;
int j = i;
while(j>0) {
if(sortieren[j-1]>sortieren[j]) {
temp=sortieren[j];
sortieren[j]=sortieren[j-1];
sortieren[j-1]=temp;
}
j--;
}
}
}
return sortieren;
}
public static int[] insertionSortLSG(int[] sortieren) {
int temp;
for (int i = 1; i < sortieren.length; i++) {
temp = sortieren[i];
int j = i;
while (j > 0 && sortieren[j - 1] > temp) {
sortieren[j] = sortieren[j - 1];
j--;
}
sortieren[j] = temp;
}
return sortieren;
}

View File

@ -1,4 +1,47 @@
public class InsertionSort { public class InsertionSort {
public static void main(String[] args) {}
public static int[] insertionSort(int[] sortieren) {
int temp = 0;
for(int i = 1; i < sortieren.length; i++) {
if(sortieren[i-1] > sortieren[i]) {
temp=sortieren[i];
sortieren[i]=sortieren[i-1];
sortieren[i-1]=temp;
int j = i;
while(j>0) {
if(sortieren[j-1]>sortieren[j]) {
temp=sortieren[j];
sortieren[j]=sortieren[j-1];
sortieren[j-1]=temp;
}
j--;
}
}
}
return sortieren;
}
public static int[] insertionSortLSG(int[] sortieren) {
int temp;
for (int i = 1; i < sortieren.length; i++) {
temp = sortieren[i];
int j = i;
while (j > 0 && sortieren[j - 1] > temp) {
sortieren[j] = sortieren[j - 1];
j--;
}
sortieren[j] = temp;
}
return sortieren;
}
} }

View File

@ -1,12 +1,14 @@
public class SelectionSort { public class SelectionSort {
private boolean change;
private int temp;
public static int[] selectionSort(int[] arr) { public static int[] selectionSort(int[] arr) {
int tmp; int tmp;
int index = 0; int index = 0;
for(int i=0; i < arr.length; i++) { for(int i=0; i < arr.length; i++) {
tmp = arr[i]; tmp = arr[i];
for(int j = 0; j < arr.length; j++) { for(int j = 0; j < arr.length; j++) {
if(arr[j] < tmp) tmp = arr[j]; if(arr[j] < tmp) tmp = arr[j];
index = j; index = j;
@ -15,8 +17,42 @@ public class SelectionSort {
arr[i]=tmp; arr[i]=tmp;
} }
return arr; return 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();
}
}

View File

@ -9,9 +9,8 @@ class bubbletest {
int arr[] = {3,2,5}; int arr[] = {3,2,5};
int fin[]= {2,3,5}; int fin[]= {2,3,5};
int erg[] = Bubblesort.insertionSort(arr);
assertArrayEquals(erg, fin);
} }
} }