1. Perhitungan Rerata
private void proses2ActionPerformed(java.awt.event.ActionEvent evt) {
// proses input
String input = inputData.getText();
String []data=input.split("-");
int a=0;
int index=0;
int hasil=0;
//proses perhitungan
while(a<data.length){
hasil=hasil+Integer.parseInt(data[index]);
a++;
index++;
}
//proses output
hasil=hasil/data.length;
String b=Integer.toString(hasil);
output2.setText(b);
outputwaktu.setText(Integer.toString(data.length));
}
2. Perhitungan Max dan Min
private void proses3ActionPerformed(java.awt.event.ActionEvent evt) {
String input=inputData.getText();
String []data=input.split("-");
int min = 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)
min = n;
else
min = n1;
}else{
if(n < min)
min=n;
}
}
int max = 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)
max = n;
else
max = n1;
}else{
if(n > max)
max=n;
}
}
String angka1 = Integer.toString(max);
Max.setText(angka1);
String angka2 = Integer.toString(min);
Min.setText(angka2);
}
3. Sequential Search
private void proses4ActionPerformed(java.awt.event.ActionEvent evt) {
//input data
String input=inputData.getText();
String []data=input.split("-");
int []dataint=new int[data.length];
//input bilangan yg dicari
String cari=cari1.getText();
int cariint=Integer.parseInt(cari);
//proses pencarian
int indexketemu = -1;
boolean ketemu = 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) {
ketemu = true;
if (ketemu== true){
indexketemu = i;
}
}
}
if(ketemu == true && indexketemu > -1){
outSeq.setText("Data yang anda cari ada di index ke "+indexketemu);
}else{
outSeq.setText("Data yang anda cari tidak ada");
}
}
4. Binary Search
private void proses5ActionPerformed(java.awt.event.ActionEvent evt) {
//input data
String input=inputData.getText();
String []data=input.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 cari=cari2.getText();
int cariint=Integer.parseInt(cari);
//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]==cariint)
flag=1;
else if(cariint<dataint[nt])
akhir=nt-1;
else
awal=nt+1;
}
if(flag==1)
outbin.setText("Data yang anda cari ada di index ke "+ nt);
else
outbin.setText("Data yang anda cari tidak ada");
}
private void prosesSortingActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String input=inputData.getText();
String []data=input.split("-");
String out="";
int []dataint=new int[data.length];
int temp;
for(int i = 0; i < data.length; i++){
dataint[i] = Integer.parseInt(data[i]);
}
for (int y = 0; y < dataint.length; y++) {
int min = y;
for (int x = y; x < dataint.length; x++) {
if (dataint[x] < dataint[min]) {
min = x;
}
}
temp = dataint[y];
dataint[y] = dataint[min];
dataint[min] = temp;
}
for (int i = 0; i < dataint.length; i++) {
if(i == dataint.length-1 )
out += Integer.toString(dataint[i]);
else
out += Integer.toString(dataint[i])+"-";
}
inputData.setText(out);
}
5. Selection Sort
private void proses6ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String input=inputData.getText();
String []data=input.split("-");
String out="";
int []dataint=new int[data.length];
int temp;
for(int i = 0; i < data.length; i++){
dataint[i] = Integer.parseInt(data[i]);
}
for (int y = 0; y < dataint.length; y++) {
int min = y;
for (int x = y; x < dataint.length; x++) {
if (dataint[x] < dataint[min]) {
min = x;
}
}
temp = dataint[y];
dataint[y] = dataint[min];
dataint[min] = temp;
}
for (int i = 0; i < dataint.length; i++) {
if(i == dataint.length-1 )
out += Integer.toString(dataint[i]);
else
out += Integer.toString(dataint[i])+"-";
}
output4.setText(out);
}
Posted by 22.54 and have
0
komentar
, Published at
Tidak ada komentar:
Posting Komentar