Gast
#6657612
Hallo,
bin gerade dabei für die Uni ein Programm zu schreiben.
Das Programm soll
-2 Arrays erstellen
- Länge und höchste zahl sollen über die Konsole eingegeben werden
- Beide arrays sollen mit zufälligen zahlen gefüllt werden
- Anschließend beide arrays in ein 3. rein und das wiederum sortieren
Hab alles bis auf den letzten Punkt und verzweifle langsam wirklich.
Vielleicht weiß jemand wie es geht.
Das 3. Array soll direkt sortiert werden und nicht ersten erstellt und
dann sortiert:
import java.util.Random;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class App {
public static void main (String[] args) throws IOException
{
BufferedReader reader = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Länge Array eingeben");
String capacity1 = reader.readLine();
int capacity = Integer.parseInt(capacity1);
System.out.println("maxvalue eingeben");
String maxValue1 = reader.readLine();
int maxValue = Integer.parseInt(maxValue1);
int [] a1 = createRandomIntegerArray(capacity, maxValue);
System.out.println();
System.out.println("Unsortiertes erstes Array:");
printArray(a1);
int [] b1 = createRandomIntegerArray(capacity, maxValue);
System.out.println();
System.out.println("Unsortiertes zweites Array:");
printArray(b1);
System.out.println();
System.out.println("Sortiertes erstes Array:");
bubbleSort (a1);
printArray(a1);
System.out.println();
System.out.println("Sortiertes zweites Array:");
bubbleSort (b1);
printArray(b1);
Zusammenfügen(a1, b1);
Zusammenfügen(c); //hier soll die Methode ausgeführt
werden
}
public static int[] createRandomIntegerArray(int capacity, int
maxValue)
{
Random rnd = new Random();
int[] result = new int[capacity];
for (int i = 0; i < result.length; i++)
{
result[i] = rnd.nextInt(maxValue) + 1;
}
return result;
}
public static void printArray(int[] Values)
{
for (int i = 0 ; i < Values.length ; i++)
{
System.out.print(Values[i]);
System.out.print(" ");
}
}
public static void bubbleSort (int[] Values)
{
{
for (int i = Values.length; i>0; i--)
{
for (int c = 1; c < i; c++)
{
if (Values[c-1] > Values[c])
{
int buf = Values[c-1];
Values[c-1] = Values [c];
Values[c] = buf;
}
}
}
}
}
private static void Zusammenfügen(int[] a1, int[] b1)
{
int Länge = a1.length + b1.length;
int [] c = new int[Länge];
boolean bool=true;
while(bool)
{
int i=0;
int j=0;
int k=0;
if(a1[i]<b1[j])
{
c[k]=a1[i];
i++;
k++;
if(i==a1.length)
{
return;
}
else
{
c[k]=b1[j];
j++;
k++;
if(j==b1.length);
}
}
//hier weiß ich nicht mehr weiter
}
}
}