MergeSort
parent
7fe3e8f765
commit
7fc588c71d
|
@ -1,126 +0,0 @@
|
|||
package Algorithmus;
|
||||
|
||||
public class Algorithmus {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
int[] arr = { 4,34,2,6,8,12,21};
|
||||
int[] sortedArray =insertionSort(arr) ;
|
||||
|
||||
for (int i : sortedArray)
|
||||
System.out.print(i + " ");
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static int[] mergeSort(int[] arr) {
|
||||
/*
|
||||
* Big-O: O(n log n)
|
||||
*/
|
||||
return arr;
|
||||
}
|
||||
|
||||
public static int[] insertionSort(int[] arr) {
|
||||
/*
|
||||
* Big-O: O(n²)
|
||||
* indexes: 0 1 2 3 4 5 6
|
||||
* Beispiel array = int[] arr = { 4,34,2,6,8,10,21};
|
||||
*/
|
||||
|
||||
for (int i = 1; i < arr.length; i++) {
|
||||
// speicher den Wert an i Index nehmen an dass i = 2;
|
||||
// also z.b: Key = 2;
|
||||
int key = arr[i];
|
||||
// speicher das vorhägie Index
|
||||
int j = i - 1;
|
||||
/*
|
||||
* Also die While Loop Konzept ist:
|
||||
* - betrachtet wird die 2 an Index 2
|
||||
* - j = 1 also >= 0
|
||||
* - arr[i] ist in dem Fall = 34
|
||||
* - ist die 34 > 2, weil key = 2;
|
||||
* - ja
|
||||
*/
|
||||
while (j >= 0 && arr[j] > key) {
|
||||
//also ersetzen 34 druch die 2
|
||||
// aber die 2 wurde erst in var key abgespeichert
|
||||
arr[j + 1] = arr[j];
|
||||
// index j -1
|
||||
j = j - 1;
|
||||
}
|
||||
// hier ersetzen wir die 34 durch die 2
|
||||
arr[j + 1] = key;
|
||||
}
|
||||
|
||||
return arr; // Rückgabe des sortierten Arrays
|
||||
}
|
||||
|
||||
|
||||
public static int[] selectionSort(int[] arr) {
|
||||
|
||||
/*
|
||||
* Big-O: O(n²)
|
||||
* indexes: 0 1 2 3 4 5 6
|
||||
* Beispiel array = int[] arr = { 4,34,2,6,8,10,21};
|
||||
*
|
||||
*/
|
||||
//Äueßere Schleife beginnt bei Index(0) bis zu index (6)
|
||||
// Beispiel wert
|
||||
for (int i = 0; i < arr.length - 1; i++) { // i = index (4)
|
||||
int minIndex = i;// minIndex = index (4)
|
||||
|
||||
// überprüft ob die 4 < andere Element im array
|
||||
// es soll ab i + 1 überprüft werden
|
||||
for (int j = i + 1; j < arr.length; j++) {
|
||||
//ja, die 2 < 4
|
||||
if (arr[j] < arr[minIndex]) {
|
||||
// also minIndex = index (2) also gleich 2
|
||||
minIndex = j;
|
||||
}
|
||||
}
|
||||
// falls minIndex anderes Index bekommen hat?
|
||||
if (minIndex != i) {
|
||||
//speichere erst die 4 hier
|
||||
int temp = arr[i];
|
||||
// ersetzte die 2 durch die 4
|
||||
arr[i] = arr[minIndex];
|
||||
//ersetze die 4 durch die 2
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
return arr; // Rückgabe des sortierten Arrays
|
||||
}
|
||||
|
||||
public static int[] bubbleSort(int[] arr) {
|
||||
// Big-O: O(n²)
|
||||
for (int i = 0; i < arr.length; i++)
|
||||
for(int j = 0; j < arr.length; j++)
|
||||
if (arr[i] < arr[j]) {
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = temp;
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
public static int binarySearch(int[] arr, int value) {
|
||||
int erst = 0; // Initialisierung des Startindexes auf den ersten Index des Arrays
|
||||
int last = arr.length - 1; // Initialisierung des Endindexes auf den letzten Index des Arrays
|
||||
|
||||
while (erst <= last) { // Schleife läuft, solange der Startindex kleiner oder gleich dem Endindex ist
|
||||
int mittel = (erst + last) / 2; // Berechnung des mittleren Indexes durch Addition von Start und Ende, dann Division durch 2
|
||||
|
||||
if (arr[mittel] == value) // Überprüfung, ob der Wert im Array an der mittleren Position dem gesuchten Wert entspricht
|
||||
return mittel; // Wenn ja, wird der mittlere Index als Ergebnis zurückgegeben
|
||||
else if (arr[mittel] < value) // Überprüfung, ob der Wert im Array an der mittleren Position kleiner als der gesuchte Wert ist
|
||||
erst = mittel + 1; // Wenn ja, wird der Startindex auf die Position nach der Mitte gesetzt
|
||||
|
||||
else // Wenn der Wert im Array an der mittleren Position größer als der gesuchte Wert ist
|
||||
last = mittel - 1; // Setzt den Endindex auf die Position vor der Mitte
|
||||
}
|
||||
return -1; // Wenn der Wert nicht gefunden wird, wird -1 zurückgegeben
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
package Algorithmus.BinarySearch;
|
||||
|
||||
public class BinarySearch {
|
||||
/*
|
||||
* - Array soll sortiert werden
|
||||
*
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
//indexes: 0,1,2,3,4,5,6,7,8,9
|
||||
int array[] = {1,2,3,4,5,6,7,8,9,10};
|
||||
int low = 0;// 0
|
||||
int heigh = array.length -1;//9
|
||||
int target = 6;
|
||||
|
||||
while(low <= heigh) {
|
||||
int mid = (low + heigh) / 2; // 4
|
||||
if (array[mid] == target) {
|
||||
System.out.println(array[mid]);
|
||||
break;
|
||||
}
|
||||
|
||||
else if(array[mid] < target)
|
||||
low = mid + 1;
|
||||
|
||||
else if (array[mid] > target)
|
||||
heigh = mid - 1;
|
||||
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static int binarySearch(int[] arr, int target,int low, int height,int mid) {
|
||||
if (low <= height) {
|
||||
mid = (low + height)/2;
|
||||
return binarySearch(arr, target,low, height,mid);
|
||||
}
|
||||
|
||||
if (arr[mid] == target)
|
||||
return arr[mid];
|
||||
|
||||
else if(arr[mid] < target) {
|
||||
low = mid + 1;
|
||||
return binarySearch(arr, target,low, height,mid);
|
||||
}
|
||||
else if (arr[mid] > target) {
|
||||
height = mid - 1;
|
||||
return binarySearch(arr, target,low, height,mid);
|
||||
}
|
||||
|
||||
return -1;
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package Algorithmus.SortierAlgorithmus;
|
||||
|
||||
public class BubbleSort {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// O(n²)
|
||||
|
||||
int arr[] = { 3, 1, 4, 8, 2, 3, 5, 7, 1, 3, 8, 97 };
|
||||
|
||||
for (int i = 0; i < arr.length ; i++)
|
||||
for (int j = 0; j < arr.length -1; j++) {
|
||||
if (arr[i] < arr[j]) {
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = temp;
|
||||
}
|
||||
}
|
||||
System.out.println("End");
|
||||
for (int s : arr)
|
||||
System.out.print(s + " ");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package Algorithmus.SortierAlgorithmus;
|
||||
|
||||
public class InsertionSort {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// O(n²)
|
||||
|
||||
int arr[] = { 3, 1, 4, 8, 2, 3, 5, 7, 1, 3, 8, 97 };
|
||||
for (int i = 1; i < arr.length -1; i++ ) {
|
||||
|
||||
int j = i -1;
|
||||
int merker = arr[i];
|
||||
while(j >= 0 && arr[j] > merker) {
|
||||
arr[j +1] = arr[j];
|
||||
j -= 1;
|
||||
}
|
||||
|
||||
arr[j +1] = merker;
|
||||
}
|
||||
System.out.println("End");
|
||||
for (int s : arr)
|
||||
System.out.print(s + " ");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package Algorithmus.SortierAlgorithmus;
|
||||
|
||||
public class MergeSort {
|
||||
// O(n log n)
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package Algorithmus.SortierAlgorithmus;
|
||||
|
||||
public class SelectionSort {
|
||||
// O(n²)
|
||||
|
||||
public static void main(String[] args) {
|
||||
int arr[] = {3,4,1,23,5,19,2,8,9};
|
||||
for (int i = 0; i < arr.length -1; i++) {
|
||||
|
||||
int minIndex = i;
|
||||
for (int j = i +1; j < arr.length;j++)
|
||||
if (arr[j] < arr[minIndex])
|
||||
minIndex = j;
|
||||
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
|
||||
for (int s : arr)
|
||||
System.out.print(s + " ");
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1 +0,0 @@
|
|||
,obai,obai-HP-Laptop-15-da0xxx,28.08.2024 17:18,file:///home/obai/.config/libreoffice/4;
|
|
@ -6,13 +6,18 @@ public class Test {
|
|||
Graph graph = new Graph();
|
||||
Knote mannheim = new Knote("Mannheim");
|
||||
Knote heidelberg = new Knote("Heidelberg");
|
||||
Knote frankfourt = new Knote("Frankfourt");
|
||||
graph.addknoten(mannheim);
|
||||
graph.addknoten(heidelberg);
|
||||
graph.addknoten(frankfourt);
|
||||
|
||||
new Kanten(graph.getKnote("Mannheim"),graph.getKnote("Heidelberg"));
|
||||
new Kanten(graph.getKnote("Mannheim"),graph.getKnote("Frankfourt"));
|
||||
|
||||
|
||||
mannheim.getPartner();
|
||||
heidelberg.getPartner();
|
||||
frankfourt.getPartner();
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue