Compare commits

..

1 Commits
main ... test

Author SHA1 Message Date
student f49cf045de Bubblesort implementiert aus der VL 2026-03-25 12:40:15 +01:00
7 changed files with 24 additions and 289 deletions

View File

@ -1,94 +0,0 @@
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;
}
}

View File

@ -1,22 +0,0 @@
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);
}
}

View File

@ -1,125 +0,0 @@
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;
}
}
}

View File

@ -1,17 +0,0 @@
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);
}
}

View File

@ -1,15 +1,9 @@
import java.util.Arrays;
public class Bubblesort {
public static void main(String[] args){
}
public static int[] bubblesort(int[] arr) {
public static void bubblesort(int[] arr){
/// aus der Vorlesung:
boolean ready = false;
while (!ready) {
@ -29,24 +23,5 @@ public class Bubblesort {
System.out.println(Arrays.toString(arr));
} // while
return arr;
}
} // End of Class

13
src/src/Main.java 100644
View File

@ -0,0 +1,13 @@
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
void main() {
//TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text
// to see how IntelliJ IDEA suggests fixing it.
IO.println(String.format("Hello and welcome!"));
for (int i = 1; i <= 5; i++) {
//TIP Press <shortcut actionId="Debug"/> to start debugging your code. We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint
// for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.
IO.println("i = " + i);
}
}

5
src/test.java 100644
View File

@ -0,0 +1,5 @@
public static void main(String[] args){
System.out.println("Test");
}