InsertionSort nochmals geübt
parent
a73d42271e
commit
f9282ab522
|
|
@ -1,25 +1,38 @@
|
|||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class InsertionSort {
|
||||
public static final Logger log = Logger.getLogger(InsertionSort.class.getName());
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hier ist Insertionsort");
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -37,6 +50,45 @@ public static void insertionsorttest(int[] arr){
|
|||
|
||||
}
|
||||
|
||||
void main() {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -8,13 +8,15 @@ 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};
|
||||
|
||||
Assert.assertArrayEquals(InsertionSort.insertionSort(arr), fin);
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,26 +1,17 @@
|
|||
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 int[] selectionSort(int[] arr) {
|
||||
int tmp;
|
||||
int index = 0;
|
||||
|
||||
for(int i=0; i < arr.length; i++) {
|
||||
tmp = arr[i];
|
||||
|
||||
for(int j = 0; j < arr.length; j++) {
|
||||
if(arr[j] < tmp) tmp = arr[j];
|
||||
index = j;
|
||||
}
|
||||
arr[index] = arr[i];
|
||||
arr[i]=tmp;
|
||||
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
|
||||
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[])
|
||||
{
|
||||
|
|
@ -54,25 +45,9 @@ public class SelectionSort {
|
|||
System.out.println();
|
||||
}
|
||||
|
||||
|
||||
public static void selectionsortUebung(int[] arr){
|
||||
int temp;
|
||||
|
||||
for(int i = 0; i<arr.length; i++){
|
||||
temp = arr[i];
|
||||
|
||||
int k=i;
|
||||
while(k<arr.length){
|
||||
if(arr[k] < temp) temp = arr[k];
|
||||
k++;
|
||||
}
|
||||
arr[k]=arr[i];
|
||||
arr[i]=temp;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
|
|
@ -81,6 +56,7 @@ public class SelectionSort {
|
|||
if (arr[k] < arr[minIndex]) {
|
||||
minIndex = k;
|
||||
}
|
||||
count++;
|
||||
k++; // ❗ ganz wichtig
|
||||
}
|
||||
|
||||
|
|
@ -88,7 +64,32 @@ public class SelectionSort {
|
|||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -121,8 +122,4 @@ public class SelectionSort {
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue