Versionsvergleich
Aus ProgrammingWiki
Vergleich: 2011-01-10T19:41:15+00:00 → 2015-06-02T11:54:54+00:00
| − | <language>Java</language> |
| + | <language>Javascript</language> |
== Definition und Ausgabe eines eindimensionalen Arrays == | |
| − | <code id="4d13e8e7b9c8f"> |
| − | int[] Zahlen = {-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; |
| + | <code id="4d2c2c1420cf"> |
| + | var 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++) |
| + | function ZahlenAusgabe(liste) { |
| + | var s = ""; |
| + | for (var i = 0; i < liste.length; i++) |
if (i == liste.length-1) | |
| − | System.out.println(liste[i]); |
| + | s += (liste[i])+"\n"; |
else | |
| − | System.out.print(liste[i]+", "); |
| + | s += (liste[i]+", ")+"\n"; |
| + | return s; |
} | |
</code> | |
| − | <run id="4d040a4900dde"> |
| + | <run id="4d2c2ec1429d"> |
ZahlenAusgabe(Zahlen); | |
</run> | |
== Zufallszahlen == | |
| − | <code id="4d0dc629de547"> |
| − | 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); |
| + | <code id="4d30cfd7483"> |
| + | function ZufallsZahlen(min, max, n) { |
| + | var Zahlen = []; |
| + | for (var i = 0; i < n; i++) |
| + | Zahlen[i] = Math.floor(Math.random()*(max-min+1)+min); |
return Zahlen; | |
} | |
</code> | |
| − | <run id="4d2b28a988d7e"> |
| − | int min = 1; |
| − | int max = 1000; |
| − | int n = 20; |
| − | int[] ZahlenListe = new int[n]; |
| − | ZahlenListe = ZufallsZahlen(min, max, n); |
| + | <run id="4d2c2ec14224"> |
| + | var ZahlenListe = ZufallsZahlen(1, 1000, 20); |
ZahlenAusgabe(ZahlenListe); | |
</run> | |
== BubbleSort == | |
| − | <code id="4d06a8dde4c33"> |
| − | public void BubbleSort(int[] liste) { |
| − | int h; |
| − | for (int j = 0; j < liste.length-1; j++) |
| − | for (int i = j; i < liste.length; i++) |
| + | <code id="4d06a8ddec33"> |
| + | function BubbleSort(liste) { |
| + | var h; |
| + | for (var j = 0; j < liste.length-1; j++) |
| + | for (var i = j; i < liste.length; i++) |
if (liste[i] < liste[j]) { | |
h = liste[i]; liste[i] = liste[j]; liste[j] = h; | |
} | |
} | |
</code> | |
| − | <run id="4d06a8dde4fb5"> |
| − | int[] SortListe = new int[n]; |
| − | System.arraycopy(ZahlenListe, 0, SortListe, 0, ZahlenListe.length); |
| − | BubbleSort(SortListe); |
| − | ZahlenAusgabe(SortListe); |
| + | <run id="556d8ab97532"> |
| + | var ZahlenListe = ZufallsZahlen(1, 1000, 20); |
| + | console.log("Unsortiert:" + ZahlenAusgabe(ZahlenListe)); |
| + | BubbleSort(ZahlenListe); |
| + | console.log("Sortiert:" + ZahlenAusgabe(ZahlenListe)); |
</run> | |
==Darstellung im Balkendiagramm == | |
| − | <code id="4d2761b36ac73"> |
| − | public Object BalkenDiagramm(Object canvas, Object turtle) { |
| − | double Rand = 50; |
| − | double MaxBreite = 400-2*Rand; |
| − | double MaxHoehe = 300-2*Rand; |
| + | <code id="4d2761b6ac7"> |
| + | function BalkenDiagramm(canvas, turtle) { |
| + | this.Rand = 50; |
| + | this.MaxBreite = 400-2*this.Rand; |
| + | this.MaxHoehe = 300-2*this.Rand; |
| − | private int[] MinMax(int[] liste) { |
| − | int[] Ergebnis = {0, 0}; |
| − | for (int i = 0; i < liste.length; i++) { |
| + | this.MinMax = function (liste) { |
| + | var Ergebnis = [0, 0]; |
| + | for (var 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) { |
| + | this.Balken = function(hoehe, 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; |
| + | this.Darstellung = function(liste) { |
| + | var ExtremWert = this.MinMax(liste); |
| + | var Min = ExtremWert[0]; |
| + | var Max = ExtremWert[1]; |
| + | var Breite = this.MaxBreite/liste.length; |
canvas.clear(); | |
turtle.hide(); | |
| − | turtle.move(Rand, Rand+Max/(Max-Min)*MaxHoehe); |
| + | turtle.move(this.Rand, this.Rand+Max/(Max-Min)*this.MaxHoehe); |
turtle.right(90); | |
| − | for (int i = 0; i < liste.length; i++) |
| − | Balken(liste[i]/(Max-Min)*MaxHoehe, Breite); |
| − | } |
| + | for (var i = 0; i < liste.length; i++) |
| + | this.Balken(liste[i]/(Max-Min)*this.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="4d2761b36ad6c"> |
| − | Object BalkenDiagramm1 = BalkenDiagramm(canvas1, turtle1); |
| − | Object BalkenDiagramm2 = BalkenDiagramm(canvas2, turtle2); |
| + | <run id="4d0d0793d307"> |
| + | var ZahlenListe = ZufallsZahlen(1, 1000, 20); |
| + | var BalkenDiagramm1 = new BalkenDiagramm(canvas1, turtle1); |
BalkenDiagramm1.Darstellung(ZahlenListe); | |
| − | BalkenDiagramm2.Darstellung(SortListe); |
| − | System.out.println("ok"); |
| + | |
| + | BubbleSort(ZahlenListe); |
| + | BalkenDiagramm2 = new BalkenDiagramm(canvas2, turtle2); |
| + | BalkenDiagramm2.Darstellung(ZahlenListe); |
| + | |
</run> |