Versionsvergleich
Aus ProgrammingWiki
Vergleich: 2015-06-02T10:51:38+00:00 → 2015-06-02T11:54:54+00:00
| − | <language>Java</language> |
| + | <language>Javascript</language> |
== Definition und Ausgabe eines eindimensionalen Arrays == | |
<code id="4d2c2c1420cf"> | |
var Zahlen = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | |
function ZahlenAusgabe(liste) { | |
var s = ""; | |
for (var i = 0; i < liste.length; i++) | |
if (i == liste.length-1) | |
s += (liste[i])+"\n"; | |
else | |
s += (liste[i]+", ")+"\n"; | |
return s; | |
} | |
</code> | |
<run id="4d2c2ec1429d"> | |
ZahlenAusgabe(Zahlen); | |
</run> | |
== Zufallszahlen == | |
<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="4d2c2ec14224"> | |
var ZahlenListe = ZufallsZahlen(1, 1000, 20); | |
ZahlenAusgabe(ZahlenListe); | |
</run> | |
== BubbleSort == | |
<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="556d8ab979532"> |
| − | int[] ZahlenListe = ZufallsZahlen(1, 1000, 20); |
| − | alert("Unsortiert:" + ZahlenAusgabe(ZahlenListe)); |
| − | alert("Sortiert:" + BubbleSort(ZahlenListe)); |
| − | ZahlenAusgabe(ZahlenListe); |
| + | <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="4d2761b6ac73"> |
| + | <code id="4d2761b6ac7"> |
function BalkenDiagramm(canvas, turtle) { | |
this.Rand = 50; | |
| − | this.MaxBreite = 400-2*Rand; |
| − | this.MaxHoehe = 300-2*Rand; |
| + | this.MaxBreite = 400-2*this.Rand; |
| + | this.MaxHoehe = 300-2*this.Rand; |
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; | |
}; | |
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); | |
| − | } |
| + | }; |
| − | this.Darstellung(liste) { |
| − | var ExtremWert = MinMax(liste); |
| + | this.Darstellung = function(liste) { |
| + | var ExtremWert = this.MinMax(liste); |
var Min = ExtremWert[0]; | |
var Max = ExtremWert[1]; | |
| − | var Breite = MaxBreite/liste.length; |
| + | 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++) |
| − | this.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="4d0d0793d307"> | |
var ZahlenListe = ZufallsZahlen(1, 1000, 20); | |
var BalkenDiagramm1 = new BalkenDiagramm(canvas1, turtle1); | |
BalkenDiagramm1.Darstellung(ZahlenListe); | |
BubbleSort(ZahlenListe); | |
BalkenDiagramm2 = new BalkenDiagramm(canvas2, turtle2); | |
BalkenDiagramm2.Darstellung(ZahlenListe); | |
</run> |