Binery Search
import javax.swing.JOptionPane;
public class Bynery_Search {
public static void main(String[] args) {
String input=JOptionPane.showInputDialog(null, "masukan Perhitungan ");
String []a=input.split(",");
String b=JOptionPane.showInputDialog(null, "Cari");
int awal = 0;
int n=a.length;
int cari=Integer.parseInt(b);
int akhir = n - 1;
int tengah = (awal + akhir)/2;
while( awal <= akhir ){
if ( Integer.parseInt(a[tengah]) < cari )
awal=tengah + 1;
else if ( Integer.parseInt(a[tengah]) == cari ) {
System.out.println("Search "+cari+ " Ada di Index " + (tengah + 1) + ".");
break;
}
else
akhir = tengah - 1;
tengah = (awal + akhir)/2;
}
if ( awal > akhir ){
System.out.println(" Not Found");
}
}
}
Kompleksitas waktu
import javax.swing.JOptionPane;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author USER
*/
public class kompleksitas_waktu {
public static void main(String[] args) {
String a = JOptionPane.showInputDialog(null, "Input Bilangan pakek koma");
String []temp=a.split(",");
int k=0;
int index=0;
double jumlah=0;
while(k<temp.length){
jumlah=jumlah+Integer.parseInt(temp[index]);
k++;
index++;
}
jumlah=jumlah/temp.length;
System.out.println("Kompleksitas waktu dari "+a+" adalah = "+temp.length);
}
}
Mencari Min Max
import javax.swing.JOptionPane;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author USER
*/
public class mencari_min_max {
public static void main(String[] args) {
String a = JOptionPane.showInputDialog(null, "Input Bilangan pakek koma");
String []temp=a.split(",");
int k=0;
int maks=0;
int min=Integer.parseInt(temp[0]);
int index=0;
while(k<temp.length){
if (maks>Integer.parseInt(temp[index])) {
k++;
index++;
}else{
maks=Integer.parseInt(temp[index]);
k++;
index++;
}
}
System.out.println("nilai terbesar dari "+a+" adalah "+maks);
k=0;
index=0;
while(k<temp.length){
if (min<Integer.parseInt(temp[index])) {
k++;
index++;
}else {
min=Integer.parseInt(temp[index]);
k++;
index++;
}
}
System.out.println("Nilai terkecil dari "+a+" adalah "+min);
}
}
Menghitung Rerata
import javax.swing.JOptionPane;
public class Menghitung_Rerata {
public static void main(String[] args) {
String a = JOptionPane.showInputDialog(null, "Input Bilangan pakek koma");
String[] temp = a.split(" ");
int k = 0;
int index = 0;
double hasil = 0;
while (k < temp.length) {
hasil = hasil + Integer.parseInt(temp[index]);
k++;
index++;
}
hasil = hasil / temp.length;
System.out.println("Nilai rata rata dari " + a + " adalah " + hasil + "\n");
}
}
Selection Sort
import javax.swing.JOptionPane;
public class Selection_Short {
public static void main(String[] args) {
String nilai= JOptionPane.showInputDialog(null,"masukan Nilai Yang Ingin Di urutkan");
String [] a=nilai.split(",") ;
int []b=new int[a.length];
for (int i = 0; i < a.length; i++) {
b[i]=Integer.parseInt(a[i]);
}
for(int i=0; i<b.length; i++){
int index_of_min = i;
for(int y=i; y<b.length; y++){
if(b[index_of_min]>b[y])
index_of_min = y;
}
int temp = b[i];
b[i] = b[index_of_min];
b[index_of_min] = temp;
}
for (int i = 0; i < b.length; i++) {
System.out.print(""+b[i]);
}
String c="";
for (int i = 0; i < b.length; i++) {
if(i==b.length-1)
c +=""+b[i];
else
c+=""+b[i]+",";
}
System.out.print(" hasil pengurutan nya adalah "+c);
}
}
Sequintial sort
import javax.swing.JOptionPane;
public class Sequantial_search {
public static void main(String[] args) {
String input = JOptionPane.showInputDialog(null, "masukan Nilai perhitungan (pisah dengan koma)");
String temp[]=input.split(",");
String cari= JOptionPane.showInputDialog(null, "cari nilai");
int index=0;
boolean a=false;
while(!a && index<temp.length){
if(Integer.parseInt(cari)==Integer.parseInt(temp[index])){
a=true;
}
index++;
}
if(a==true){
System.out.println("Nilai Perhitungan "+input);
System.out.println("angka "+cari+" ada pada index ke "+(index+1));
}else
System.out.println("not Found");
}
}
Posted by 00.20 and have
0
komentar
, Published at
Tidak ada komentar:
Posting Komentar