private void HitungActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//input
String masuk = input.getText();
String []data= masuk.split(",");
int a=0;
int index=0;
int hasil=0;
//proses
while(a<data.length){
hasil=hasil+Integer.parseInt(data[index]);
a++;
index++;
}
//output
hasil=hasil/data.length;
String sout=Integer.toString(hasil);
hasil1.setText(sout);
hasil2.setText(Integer.toString(data.length));
}
2. Perhitungan Nilai Maximum & Minimum
private void ProsesActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String masuk =input1.getText();
String []data=masuk.split(",");
int terkecil = 0;
for (int i=0;i<data.length;i++) {
int n = Integer.parseInt(data[i]);
if(i == 0){
int n1 = Integer.parseInt(data[i+1]);
if(n < n1) {
terkecil = n;
}
else {
terkecil = n1;
}
}else{
if(n < terkecil) {
terkecil=n;
}}}
int tertinggi = 0;
for (int i=0;i<data.length;i++) {
int n = Integer.parseInt(data[i]);
if(i == 0){
int n1 = Integer.parseInt(data[i+1]);
if(n > n1) {
tertinggi = n;
}
else {
tertinggi = n1;
}}
else{
if(n > tertinggi) {
tertinggi=n;
}}}
String angka1 = Integer.toString(tertinggi);
Max.setText(angka1);
String angka2 = Integer.toString(terkecil);
Min.setText(angka2);
}
3. Squensial Search
private void SearchActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//input
String masuk =input2.getText();
String []data=masuk.split(",");
int []dataint=new int[data.length];
//input bilangan yg dicari
String goleki = cari.getText();
int cariint=Integer.parseInt(goleki);
//proses
int indexberhasil = -1;
boolean berhasil = false;
for(int i = 0; i < data.length; i++){
dataint[i] = Integer.parseInt(data[i]);
}
for (int i=0; i<dataint.length; i++){
if (dataint[i] == cariint) {
berhasil = true;
if (berhasil== true){
indexberhasil = i;
}}}
//output
if(berhasil == true && indexberhasil > -1){
out.setText("Data yang anda cari ada di index ke "+ (indexberhasil+1));
}else{
out.setText("Data yang anda cari tidak ada");
}
}
4. Binary Search
private void Search1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//input
String masuk=input3.getText();
String []data=masuk.split(",");
int []dataint=new int[data.length];
int awal=0;//variabel bantu untuk index awal
int akhir=0;//variabel bantu untuk index akhir
int nt=0;//nilai tengah
int flag=0;//penanda suatu kondisi
//input bilangan yg dicari
String search=cari1.getText();
int goleki=Integer.parseInt(search);
//proses pencarian
for(int i = 0; i < data.length; i++){
dataint[i] = Integer.parseInt(data[i]);
}
akhir=dataint.length-1;
while(awal<=akhir && flag==0){
nt=Math.abs((awal+akhir)/2);
if(dataint[nt]==goleki) {
flag=1;
}
else if(goleki<dataint[nt]) {
akhir=nt-1;
}
else {
awal=nt+1;
}}
//output
if(flag==1) {
out1.setText("Data yang anda cari ada di index ke "+ (nt+1));
}
else {
out1.setText("Data yang anda cari tidak ada");
}
}
5. Selection Sort
private void SortingActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//input
String masuk =input4.getText();
String []data= masuk.split(",");
String output="";
int []datainputan=new int[data.length];
int temp;
//proses sorting
for(int i = 0; i < data.length; i++){
datainputan[i] = Integer.parseInt(data[i]);
}
for (int y = 0; y < datainputan.length; y++) {
int min = y;
for (int x = y; x < datainputan.length; x++) {
if (datainputan[x] < datainputan[min]) {
min = x;
}
}
temp = datainputan[y];
datainputan[y] = datainputan[min];
datainputan[min] = temp;
}
//output
for (int i = 0; i < datainputan.length; i++) {
if(i == datainputan.length-1 ) {
output += Integer.toString(datainputan[i]);
}
else {
output += Integer.toString(datainputan[i])+" , ";
}}
hasil3.setText(output);
}
kalau masih belum jelas, silahkan download projectnya disini
Nama : Bagus Dermawan
NIM : 2012420050
Posted by 05.54 and have
0
komentar
, Published at
Tidak ada komentar:
Posting Komentar