merge sort gefailed
parent
bd8e30f9c3
commit
0f90ace1f1
|
@ -0,0 +1,41 @@
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
public class BubbleSort
|
||||||
|
{
|
||||||
|
static Random r = new Random();
|
||||||
|
static int[] a = new int[10000000];
|
||||||
|
// gibt eine zufällige Zahl von 1-50 zurück
|
||||||
|
public static int Zufallszahl(){
|
||||||
|
return r.nextInt(10);
|
||||||
|
}
|
||||||
|
public static void main(){
|
||||||
|
// array mit 20 zufallszahlen von 1-50 belegen
|
||||||
|
for (int i = 0; i < a.length; i++){
|
||||||
|
a[i] = Zufallszahl();
|
||||||
|
}
|
||||||
|
// ausgeben des arrays "a"
|
||||||
|
for (int i = 0; i < a.length; i++){
|
||||||
|
System.out.print(a[i]);
|
||||||
|
System.out.print(",");
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
for (int j = 0; j < a.length;j++){
|
||||||
|
boolean vertauscht = false;
|
||||||
|
// Ein Durchgang
|
||||||
|
for (int i = 0; i < a.length - 1 -j; i++){
|
||||||
|
if (a[i] > a[i+1]){
|
||||||
|
int tmp = a[i];
|
||||||
|
a[i] = a[i+1];
|
||||||
|
a[i+1] = tmp;
|
||||||
|
vertauscht = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!vertauscht) break;
|
||||||
|
}
|
||||||
|
// ausgeben des arrays "a"
|
||||||
|
for (int i = 0; i < a.length; i++){
|
||||||
|
System.out.print(a[i]);
|
||||||
|
System.out.print(",");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,65 @@
|
||||||
|
import java.util.Random;
|
||||||
|
public class MergeSort
|
||||||
|
{
|
||||||
|
static Random r = new Random();
|
||||||
|
static int[] a = new int[100];
|
||||||
|
// gibt eine zufällige Zahl von 1-50 zurück
|
||||||
|
public static int Zufallszahl(){
|
||||||
|
return r.nextInt(10);
|
||||||
|
}
|
||||||
|
public static void main(){
|
||||||
|
// array mit 20 zufallszahlen von 1-50 belegen
|
||||||
|
for (int i = 0; i < a.length; i++){
|
||||||
|
a[i] = Zufallszahl();
|
||||||
|
}
|
||||||
|
// ausgeben des arrays "a"
|
||||||
|
for (int i = 0; i < a.length; i++){
|
||||||
|
System.out.print(a[i]);
|
||||||
|
System.out.print(",");
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
|
||||||
|
a = sort(a);
|
||||||
|
|
||||||
|
// ausgeben des arrays "a"
|
||||||
|
for (int i = 0; i < a.length; i++){
|
||||||
|
System.out.print(a[i]);
|
||||||
|
System.out.print(",");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static int[] sort(int[] a){
|
||||||
|
// 0. Schritt: Abbruchbedingung
|
||||||
|
if (a.length == 1) return a;
|
||||||
|
|
||||||
|
// 1. Schritt: auf 2 Arrays aufteilen
|
||||||
|
int[] l = new int[a.length/2];
|
||||||
|
int[] r = new int[a.length -l.length];
|
||||||
|
for (int i = 0; i < a.length; i++){
|
||||||
|
if (i <= a.length/2)l[i] = a[i];
|
||||||
|
else r[i-l.length] = a[i];
|
||||||
|
}
|
||||||
|
// 2. Schritt Arrays sortieren (rekursiv)
|
||||||
|
l = sort(l);
|
||||||
|
r = sort(r);
|
||||||
|
int links = 0;
|
||||||
|
int rechts = 0;
|
||||||
|
// 3. Schritt Arrays zusammenfügen
|
||||||
|
for (int i = 0; i<a.length; i++){
|
||||||
|
if(links >= l.length){ //links "leer"
|
||||||
|
a[i] = r[rechts];
|
||||||
|
rechts++;
|
||||||
|
}else if (rechts >= r[rechts]){ //rechts "leer"
|
||||||
|
a[i] = l[links];
|
||||||
|
links++;
|
||||||
|
}else if (l[links] <= r[rechts]){ // links größer
|
||||||
|
a[i] = l[links];
|
||||||
|
links++;
|
||||||
|
}
|
||||||
|
else{ // rechts größer
|
||||||
|
a[i] = r[rechts];
|
||||||
|
rechts++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue