Versionsvergleich
Aus ProgrammingWiki
Vergleich: 2012-03-15T22:30:20+00:00 → 2012-03-15T22:33:51+00:00
<language>Java</language> | |
== Definition und Ausgabe eines eindimensionalen Arrays == | |
| − | <code id="4f626d7c52dcd"> |
| + | <code id="4f626e4f01959"> |
int[] Zahlen = {-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; | |
public void ZahlenAusgabe(int[] liste) { | |
for (int i = 0; i < liste.length; i++) | |
if (i == liste.length-1) | |
System.out.println(liste[i]); | |
else | |
System.out.print(liste[i]+", "); | |
} | |
</code> | |
| − | <run id="4f626d7c52f87"> |
| + | <run id="4f626e4f01b19"> |
ZahlenAusgabe(Zahlen); | |
</run> | |
== Zufallszahlen == | |
| − | <code id="4f626d7c52e3b"> |
| − | int min = 1; |
| − | int max = 1000; |
| − | int n = 20; |
| − | int[] ZahlenListe = new int[n]; |
| − | |
| + | <code id="4f626e4f019cc"> |
public int[] ZufallsZahlen(int min, int max, int n) { | |
int[] Zahlen = new int[n]; | |
for (int i = 0; i < n; i++) | |
| − | Zahlen[i] = (int)(Math.random()*(max-min+2)+min-1); |
| + | Zahlen[i] = (int)Math.floor(Math.random()*(max-min+1)+min); |
return Zahlen; | |
} | |
</code> | |
| − | <run id="4f626d7c52feb"> |
| − | ZahlenListe = ZufallsZahlen(min, max, n); |
| + | <run id="4f626e4f01b7d"> |
| + | int[] ZahlenListe = ZufallsZahlen(1, 1000, 20); |
ZahlenAusgabe(ZahlenListe); | |
</run> | |
== BubbleSort == | |
| − | <code id="4f626d7c52ea0"> |
| − | int[] SortListe = new int[n]; |
| − | |
| + | <code id="4f626e4f01a33"> |
public void BubbleSort(int[] liste) { | |
int h; | |
for (int j = 0; j < liste.length-1; j++) | |
for (int i = j; i < liste.length; i++) | |
if (liste[i] < liste[j]) { | |
h = liste[i]; liste[i] = liste[j]; liste[j] = h; | |
} | |
} | |
</code> | |
| − | <run id="4f626d7c53048"> |
| − | System.arraycopy(ZahlenListe, 0, SortListe, 0, ZahlenListe.length); |
| − | BubbleSort(SortListe); |
| − | ZahlenAusgabe(SortListe); |
| + | <run id="4f626e4f01be1"> |
| + | int[] ZahlenListe = ZufallsZahlen(1, 1000, 20); |
| + | ZahlenAusgabe(ZahlenListe); |
| + | System.out.println("Sortiert:"); |
| + | BubbleSort(ZahlenListe); |
| + | ZahlenAusgabe(ZahlenListe); |
</run> | |
| − | == Darstellung im Balkendiagramm == |
| + | ==Darstellung im Balkendiagramm == |
| − | <code id="4f626d7c52f04"> |
| + | <code id="4f626e4f01a95"> |
public Object BalkenDiagramm(Object canvas, Object turtle) { | |
double Rand = 50; | |
double MaxBreite = 400-2*Rand; | |
double MaxHoehe = 300-2*Rand; | |
private int[] MinMax(int[] liste) { | |
int[] Ergebnis = {0, 0}; | |
for (int i = 0; i < liste.length; i++) { | |
if (Ergebnis[0] > liste[i]) Ergebnis[0] = liste[i]; | |
if (Ergebnis[1] < liste[i]) Ergebnis[1] = liste[i]; | |
} | |
return Ergebnis; | |
} | |
private void Balken(double hoehe, double breite) { | |
turtle.forward(breite); turtle.left(90); | |
turtle.forward(hoehe); turtle.left(90); | |
turtle.forward(breite); turtle.left(90); | |
turtle.forward(hoehe); turtle.left(90); | |
turtle.forward(breite); | |
} | |
public void Darstellung(int[] liste) { | |
int[] ExtremWert = new int[2]; | |
ExtremWert = MinMax(liste); | |
double Min = ExtremWert[0]; | |
double Max = ExtremWert[1]; | |
double Breite = MaxBreite/liste.length; | |
canvas.clear(); | |
turtle.hide(); | |
turtle.move(Rand, Rand+Max/(Max-Min)*MaxHoehe); | |
turtle.right(90); | |
for (int i = 0; i < liste.length; i++) | |
Balken(liste[i]/(Max-Min)*MaxHoehe, Breite); | |
} | |
return this; | |
} | |
</code> | |
<table border="0" cellpadding="0" cellspacing="0"> | |
<tr> | |
<td align="center"> | |
'''Ungeordnete Zufallszahlen:''' | |
<canvas w="400" h="300"></canvas> | |
</td> | |
<td> </td> | |
<td align="center"> | |
'''Geordnete Zufallszahlen:''' | |
<canvas w="400" h="300"></canvas> | |
</td> | |
</tr> | |
</table> | |
| − | <run id="4f626d7c530f4"> |
| + | <run id="4f626e4f01c42"> |
| + | int[] ZahlenListe = ZufallsZahlen(1, 1000, 20); |
Object BalkenDiagramm1 = BalkenDiagramm(canvas1, turtle1); | |
| − | Object BalkenDiagramm2 = BalkenDiagramm(canvas2, turtle2); |
BalkenDiagramm1.Darstellung(ZahlenListe); | |
| − | BalkenDiagramm2.Darstellung(SortListe); |
| + | |
| + | BubbleSort(ZahlenListe); |
| + | Object BalkenDiagramm2 = BalkenDiagramm(canvas2, turtle2); |
| + | BalkenDiagramm2.Darstellung(ZahlenListe); |
| + | |
System.out.println("ok"); | |
</run> |