<html>
<head>
<script LANGUAGE = "Javascript">
function Urutkan (form)
{
DataKosong= false;
DataInputan = form.Data.value;
inputData = DataInputan.split (",");
for (var i = 0; i<inputData.length; i++)
{
inputData [i] = parseInt (inputData[i], 10);
if (isNaN (inputData [i]) )
{
DataKosong = true;
break;
}
}
inputData = bubbleSort (inputData, 0, inputData.length-1);
if (DataKosong)
{
alert ("Silakan Entri Dulu Data, Pisahkan dengan tanda Koma");
form.Data.focus();
}
else
form.Hasil.value = DataString (inputData,0);
}
function DataString (ArrayData, Angka)
{
if ( (ArrayData.length - 1) >= Angka)
return (ArrayData[Angka] + "," + DataString (ArrayData, (Angka + 1) ) );
else
return "";
}
function bubbleSort (ArrayData, Mulai, MulaiBaru)
{
for (var i=MulaiBaru-1; i>=Mulai; i--)
{
for (var j=Mulai; j<=i; j++)
{
if (ArrayData[j+1] > ArrayData [j] )
{
var DataTemporer = ArrayData [j];
ArrayData [j] = ArrayData [j+1];
ArrayData[j+1] = DataTemporer;
}
}
}
return ArrayData;
}
</script>
</head>
<body>
<center>
<form>
Masukan Deretan Angka, Pisahkan denga Koma :
<hr>
<input type = text name=Data size=30 Value="5,6,8,5,12,4,8,4,20">
<br>
<br>
<input type=button value="Urutkan" onClick="Urutkan (this.form)">
<br>
<br>
<br>
Hasil Pengurutan :
<hr>
<input type=text name=Hasil size=30>
</form>
</center>
</body>
</html>
Rabu, 24 Juni 2009
Pengurutan (Sort) Ascending
<html>
<head>
<script LANGUAGE = "Javascript">
function Urutkan (form)
{
DataKosong= false;
DataInputan = form.Data.value;
inputData = DataInputan.split (",");
for (var i = 0; i<inputData.length; i++)
{
inputData [i] = parseInt (inputData[i], 10);
if (isNaN (inputData [i]) )
{
DataKosong = true;
break;
}
}
inputData = bubbleSort (inputData, 0, inputData.length-1);
if (DataKosong)
{
alert ("Silakan Entri Dulu Data, Pisahkan dengan tanda Koma");
form.Data.focus();
}
else
form.Hasil.value = DataString (inputData,0);
}
function DataString (ArrayData, Angka)
{
if ( (ArrayData.length - 1) >= Angka)
return (ArrayData[Angka] + "," + DataString (ArrayData, (Angka + 1) ) );
else
return "";
}
function bubbleSort (ArrayData, Mulai, MulaiBaru)
{
for (var i=MulaiBaru-1; i>=Mulai; i--)
{
for (var j=Mulai; j<=i; j++)
{
if (ArrayData[j+1] < ArrayData [j] )
{
var DataTemporer = ArrayData [j];
ArrayData [j] = ArrayData [j+1];
ArrayData[j+1] = DataTemporer;
}
}
}
return ArrayData;
}
</script>
</head>
<body>
<center>
<form>
Masukan Deretan Angka, Pisahkan denga Koma :
<hr>
<input type = text name=Data size=30 malue="5,6,8,5,12,4,8,4,20">
<br>
<br>
<input type=button value="Urutkan" onClick="Urutkan (this.form)">
<br>
<br>
<br>
Hasil Pengurutan :
<hr>
<input type=text name=Hasil size=30>
</form>
</center>
</body>
</html>
<head>
<script LANGUAGE = "Javascript">
function Urutkan (form)
{
DataKosong= false;
DataInputan = form.Data.value;
inputData = DataInputan.split (",");
for (var i = 0; i<inputData.length; i++)
{
inputData [i] = parseInt (inputData[i], 10);
if (isNaN (inputData [i]) )
{
DataKosong = true;
break;
}
}
inputData = bubbleSort (inputData, 0, inputData.length-1);
if (DataKosong)
{
alert ("Silakan Entri Dulu Data, Pisahkan dengan tanda Koma");
form.Data.focus();
}
else
form.Hasil.value = DataString (inputData,0);
}
function DataString (ArrayData, Angka)
{
if ( (ArrayData.length - 1) >= Angka)
return (ArrayData[Angka] + "," + DataString (ArrayData, (Angka + 1) ) );
else
return "";
}
function bubbleSort (ArrayData, Mulai, MulaiBaru)
{
for (var i=MulaiBaru-1; i>=Mulai; i--)
{
for (var j=Mulai; j<=i; j++)
{
if (ArrayData[j+1] < ArrayData [j] )
{
var DataTemporer = ArrayData [j];
ArrayData [j] = ArrayData [j+1];
ArrayData[j+1] = DataTemporer;
}
}
}
return ArrayData;
}
</script>
</head>
<body>
<center>
<form>
Masukan Deretan Angka, Pisahkan denga Koma :
<hr>
<input type = text name=Data size=30 malue="5,6,8,5,12,4,8,4,20">
<br>
<br>
<input type=button value="Urutkan" onClick="Urutkan (this.form)">
<br>
<br>
<br>
Hasil Pengurutan :
<hr>
<input type=text name=Hasil size=30>
</form>
</center>
</body>
</html>
Rabu, 17 Juni 2009
Program Graph/Graf
<html>
<head><title>Dijkstra Algoritma</title>
<script>
<!--
alert ("Taet Dijkstra");
//-->
</script>
</head>
<body>
<script language = "javascript">
var NilaiAcuan = 10000;
var TakTerdefinisi = -1;
var NamaVerteks = new Array ('A','B','C','D','E','F');
var Matriks = new Array (6);
function Bobot (a,b)
{
return Matriks [a] [b]
}
function Dijkstra (JumlahVerteks,Awal,d)
{
var Posisi = new Array (JumlahVerteks);
var i;
var Kunjungan = new Array (JumlahVerteks);
var Sebelum = new Array (JumlahVerteks);
for (i=0; i<JumlahVerteks; i++)
{
Posisi [i] = NilaiAcuan;
Sebelum [i] = TakTerdefinisi;
Kunjungan[i] = false;
}
Posisi [Awal] = 0;
var Verteks;
for (Verteks =0; Verteks<JumlahVerteks; Verteks++)
{
var JarakTerpendek = NilaiAcuan, Berhenti = -1;
for (i=0; i<JumlahVerteks; i++)
{
if (!Kunjungan[i])
{
if (Posisi[i] <= JarakTerpendek)
{
JarakTerpendek = Posisi[i]; Berhenti = 1;
}
}
}
Kunjungan[Berhenti] = true;
for (i=0; i<JumlahVerteks; i++)
{
if (!Kunjungan[i])
{
var w = Bobot (Berhenti, i);
if (Posisi [Berhenti]+ w <Posisi[i])
{
Posisi[i] = Posisi[Berhenti] + w;
Sebelum [i] = Berhenti;
}
}
}
}
i=d;
if (Posisi [i] <NilaiAcuan)
{
var Lintasan = NamaVerteks [i];
var Verteks = i;
while (Verteks>0)
{
Verteks = Sebelum [Verteks];
if (Verteks>0) Lintasan = NamaVerteks[Verteks] + '->' + Lintasan;
{
message ("Jarak :" +Posisi[i]+' ('+Lintasan+')'+'<br>');
}
else
{
message ("Tidak ada Jalur");
}
}
function init () {
var X = NilaiAcuan;
Matriks [0] = new Array (0,2,3,X,x,x);
Matriks [1] = new Array (2,o,3,6,x,x);
Matriks [2] = new Array (3,3,0,3,5,x);
Matriks [3] = new Array (x,6,3,0,1,3);
Matriks [4] = new Array (x,x,5,1,0,1);
Matriks [5] = new Array (x,x,x,3,1,0);
var Awal='<pre>';
Awal = Awal + 'A--2-B-6--D--3-F'+'<br>'
Awal = Awal + ' \ | /| / '+'<br>'
Awal = Awal + ' 3 3 3 1 1 '+'<br>'
Awal = Awal + ' \ | / | / '+''<br>'
Awal = Awal + ' \|/ |/ '+'<br>'
Awal = Awal + ' C--5-E '+'<br>;
Awal = Awal +'</pre>'
message (Awal.replace (/(\d)+/g, '<font color="red">$1</font>'));
}
init ();
var Dari=0 // A;
var Ke =5 // F;
Dijkstra(Matriks.length,Dari,Ke);
//-->
</script>
</body>
</html>
<head><title>Dijkstra Algoritma</title>
<script>
<!--
alert ("Taet Dijkstra");
//-->
</script>
</head>
<body>
<script language = "javascript">
var NilaiAcuan = 10000;
var TakTerdefinisi = -1;
var NamaVerteks = new Array ('A','B','C','D','E','F');
var Matriks = new Array (6);
function Bobot (a,b)
{
return Matriks [a] [b]
}
function Dijkstra (JumlahVerteks,Awal,d)
{
var Posisi = new Array (JumlahVerteks);
var i;
var Kunjungan = new Array (JumlahVerteks);
var Sebelum = new Array (JumlahVerteks);
for (i=0; i<JumlahVerteks; i++)
{
Posisi [i] = NilaiAcuan;
Sebelum [i] = TakTerdefinisi;
Kunjungan[i] = false;
}
Posisi [Awal] = 0;
var Verteks;
for (Verteks =0; Verteks<JumlahVerteks; Verteks++)
{
var JarakTerpendek = NilaiAcuan, Berhenti = -1;
for (i=0; i<JumlahVerteks; i++)
{
if (!Kunjungan[i])
{
if (Posisi[i] <= JarakTerpendek)
{
JarakTerpendek = Posisi[i]; Berhenti = 1;
}
}
}
Kunjungan[Berhenti] = true;
for (i=0; i<JumlahVerteks; i++)
{
if (!Kunjungan[i])
{
var w = Bobot (Berhenti, i);
if (Posisi [Berhenti]+ w <Posisi[i])
{
Posisi[i] = Posisi[Berhenti] + w;
Sebelum [i] = Berhenti;
}
}
}
}
i=d;
if (Posisi [i] <NilaiAcuan)
{
var Lintasan = NamaVerteks [i];
var Verteks = i;
while (Verteks>0)
{
Verteks = Sebelum [Verteks];
if (Verteks>0) Lintasan = NamaVerteks[Verteks] + '->' + Lintasan;
{
message ("Jarak :" +Posisi[i]+' ('+Lintasan+')'+'<br>');
}
else
{
message ("Tidak ada Jalur");
}
}
function init () {
var X = NilaiAcuan;
Matriks [0] = new Array (0,2,3,X,x,x);
Matriks [1] = new Array (2,o,3,6,x,x);
Matriks [2] = new Array (3,3,0,3,5,x);
Matriks [3] = new Array (x,6,3,0,1,3);
Matriks [4] = new Array (x,x,5,1,0,1);
Matriks [5] = new Array (x,x,x,3,1,0);
var Awal='<pre>';
Awal = Awal + 'A--2-B-6--D--3-F'+'<br>'
Awal = Awal + ' \ | /| / '+'<br>'
Awal = Awal + ' 3 3 3 1 1 '+'<br>'
Awal = Awal + ' \ | / | / '+''<br>'
Awal = Awal + ' \|/ |/ '+'<br>'
Awal = Awal + ' C--5-E '+'<br>;
Awal = Awal +'</pre>'
message (Awal.replace (/(\d)+/g, '<font color="red">$1</font>'));
}
init ();
var Dari=0 // A;
var Ke =5 // F;
Dijkstra(Matriks.length,Dari,Ke);
//-->
</script>
</body>
</html>
Program Double Linked List
<html>
<head>
<title>Struktur Data Linked List</title>
</head>
<body>
<script language = "JavaScript">
<!--
function DoublyLinkedList()
{
this._Kepala = null;
this._Ekor = null;
this._Panjang = 0;
}
DoublyLinkedList.prototype =
{
constructor: DoublyLinkedList,
Tambah: function (data)
{
var node = {data: data, next: null, prev: null};
if (this._Panjang == 0)
{
this._Kepala = node;
this._Ekor = node;
}
else
{
this._Ekor.next = node;
node.prev = this._Ekor;
this._Ekor = node;
}
this._Panjang++;
},
ItemList: function(index)
{
if (index > -1 && index < this._Panjang)
{
var NodeTanda = this._Kepala,
i = 0;
while(i++ < index)
{
NodeTanda = NodeTanda.next;
}
return NodeTanda.data;
}
else
{
return null;
}
},
Buang: function(index){
if (index > -1 && index < this._Panjang)
{
var NodeTanda = this._Kepala, i = 0;
if (index === 0)
{
this._Kepala = NodeTanda.next;
if (!this._Kepala)
{
this._Ekor = null;
}
else
{
this._Kepala.prev = null;
}
}
else
if (index === this._Panjang -1)
{
NodeTanda = this._Ekor;
this._Ekor = NodeTanda.prev;
this._Ekor.next = null;
}
else
{
while(i++ < index)
{
NodeTanda = NodeTanda.next;
}
NodeTanda.prev.next = NodeTanda.next;
}
this._Panjang--;
return NodeTanda.data;
}
else
{
return null;
}
},
Ukuran: function(){
return this._Panjang;
},
toArray: function(){
var result = [],
NodeTanda = this._Kepala;
while(NodeTanda){
result.push(NodeTanda.data);
NodeTanda = NodeTanda.next;
}
return result;
},
toString: function(){
return this.toArray().toString();
}
}
var List = new DoublyLinkedList();
List.Tambah("Teknik Informatika");
List.Tambah("Teknik Komputer");
List.Tambah("Komputerisasi Akuntansi");
List.Tambah("Computer Networking");
document.write("Isi LinkedList <br>");
document.write("--------------- <br>");
for (i=0; i<4; i++)
{
document.write(List.ItemList(i) + "<br>") ;
}
document.write("--------------- <br>");
alert("Isi LinkedList Awal =" + List.ItemList(0));
alert("ItemList 1 pada Linked List diBuang, yaitu = " +List.ItemList(1));
List.Buang(1);
document.write("<br>Isi LinkedList Terakhir <br>");
document.write("--------------- <br>");
for (i=0; i<4; i++)
{
document.write(List.ItemList(i) + "<br>") ;
}
document.write("--------------- <br>");
//-->
</script>
</body>
</html>
<head>
<title>Struktur Data Linked List</title>
</head>
<body>
<script language = "JavaScript">
<!--
function DoublyLinkedList()
{
this._Kepala = null;
this._Ekor = null;
this._Panjang = 0;
}
DoublyLinkedList.prototype =
{
constructor: DoublyLinkedList,
Tambah: function (data)
{
var node = {data: data, next: null, prev: null};
if (this._Panjang == 0)
{
this._Kepala = node;
this._Ekor = node;
}
else
{
this._Ekor.next = node;
node.prev = this._Ekor;
this._Ekor = node;
}
this._Panjang++;
},
ItemList: function(index)
{
if (index > -1 && index < this._Panjang)
{
var NodeTanda = this._Kepala,
i = 0;
while(i++ < index)
{
NodeTanda = NodeTanda.next;
}
return NodeTanda.data;
}
else
{
return null;
}
},
Buang: function(index){
if (index > -1 && index < this._Panjang)
{
var NodeTanda = this._Kepala, i = 0;
if (index === 0)
{
this._Kepala = NodeTanda.next;
if (!this._Kepala)
{
this._Ekor = null;
}
else
{
this._Kepala.prev = null;
}
}
else
if (index === this._Panjang -1)
{
NodeTanda = this._Ekor;
this._Ekor = NodeTanda.prev;
this._Ekor.next = null;
}
else
{
while(i++ < index)
{
NodeTanda = NodeTanda.next;
}
NodeTanda.prev.next = NodeTanda.next;
}
this._Panjang--;
return NodeTanda.data;
}
else
{
return null;
}
},
Ukuran: function(){
return this._Panjang;
},
toArray: function(){
var result = [],
NodeTanda = this._Kepala;
while(NodeTanda){
result.push(NodeTanda.data);
NodeTanda = NodeTanda.next;
}
return result;
},
toString: function(){
return this.toArray().toString();
}
}
var List = new DoublyLinkedList();
List.Tambah("Teknik Informatika");
List.Tambah("Teknik Komputer");
List.Tambah("Komputerisasi Akuntansi");
List.Tambah("Computer Networking");
document.write("Isi LinkedList <br>");
document.write("--------------- <br>");
for (i=0; i<4; i++)
{
document.write(List.ItemList(i) + "<br>") ;
}
document.write("--------------- <br>");
alert("Isi LinkedList Awal =" + List.ItemList(0));
alert("ItemList 1 pada Linked List diBuang, yaitu = " +List.ItemList(1));
List.Buang(1);
document.write("<br>Isi LinkedList Terakhir <br>");
document.write("--------------- <br>");
for (i=0; i<4; i++)
{
document.write(List.ItemList(i) + "<br>") ;
}
document.write("--------------- <br>");
//-->
</script>
</body>
</html>
Langganan:
Postingan (Atom)