import java.io.*;
public class soalpalindrom{
static String kata;
static char[] array;
static int top=0;
static char stack[];
public static void main(String[] args) throws Exception{
BufferedReader now = new BufferedReader (new InputStreamReader (System.in));
System.out.println (" ||pengecekkan kata palindrom|| ");
System.out.print("Masukkan kata : ");
kata = now.readLine();
array = kata.toCharArray();
stack = new char[array.length];
for(int i=0; i<array.length; i++){
char a = array[i];
push(a);
}
boolean akhir = true;
for (int i=0; i<array.length; i++){
if (pop() == array[i])
akhir = true;
else akhir = false;
}
if (akhir == true)
System.out.println("Kata "+kata+" adalah Kata Polindrom");
else
System.out.println("Kata "+kata+" Bukan Kata Polindrom");
}
static void push(char a){
stack[top] = a;
top=top+1;
}
static char pop(){
top = top -1;
return stack[top];
}}
Kamis, 11 Desember 2014
Kamis, 02 Oktober 2014
Struktur Data (Pemrograman Java) : REFERENCE
TUGAS LAPORAN STRUKTUR DATA
REFERENCE
DISUSUN
OLEH :
ANDAM DEWI ASHARI
RAMADHANI FITRI
TEKNIK INFORMATIKA
FAKULTAS SAINS DAN TEKNOLOGI
UIN
SUSKA RIAU
TAHUN
AKADEMIK 2014/2015
REFERENCE
Pada tipe data reference deklarasi variabel dimaksudkan untuk
“memesan”
Selasa, 24 Juni 2014
Pemrograman Java : selection sort (pengurutan pilih)
public class selektionsort {
//property
private int [] data;
//konstruktor
public selektionsort (int n){
this.data=new int[n];
}
//fungsi set data
public void SetData(int index, int value){
if (index >= 0 && index < this.data.length){
this.data[index]=value;
}
}
//fungsi print data
public void PrintData(){
String out="Data = ";
for(int a=0; a<this.data.length; a++){
out +=this.data[a]+"-";
}
System.out.println(out);
}
//fungsi tukar nilai
private void Tukar(int i, int j){
int temp = this.data[i];
this.data[i]=this.data[j];
this.data[j]=temp;
}
//fungsi selektion sort
public void Sort(){
for(int a=0; a<this.data.length; a++){
int min=a;
for(int b=a; b<this.data.length; b++){
if(this.data[b]<this.data[min]){
min=b;
}
}
Tukar(a, min);
}
}
public static void main(String[]dianti){
//buat objek
selektionsort ss=new selektionsort(10);
//input isi
ss.SetData(0, 5);
ss.SetData(1, 3);
ss.SetData(2, 7);
ss.SetData(3, 6);
ss.SetData(4, 9);
ss.SetData(5, 4);
ss.SetData(6, 8);
ss.SetData(7, 2);
ss.SetData(8, 1);
ss.SetData(9, 4);
//print
System.out.println("selektion sort");
System.out.println("--------------");
ss.PrintData();
//sort
ss.Sort();
//print
ss.PrintData();
}
}
//property
private int [] data;
//konstruktor
public selektionsort (int n){
this.data=new int[n];
}
//fungsi set data
public void SetData(int index, int value){
if (index >= 0 && index < this.data.length){
this.data[index]=value;
}
}
//fungsi print data
public void PrintData(){
String out="Data = ";
for(int a=0; a<this.data.length; a++){
out +=this.data[a]+"-";
}
System.out.println(out);
}
//fungsi tukar nilai
private void Tukar(int i, int j){
int temp = this.data[i];
this.data[i]=this.data[j];
this.data[j]=temp;
}
//fungsi selektion sort
public void Sort(){
for(int a=0; a<this.data.length; a++){
int min=a;
for(int b=a; b<this.data.length; b++){
if(this.data[b]<this.data[min]){
min=b;
}
}
Tukar(a, min);
}
}
public static void main(String[]dianti){
//buat objek
selektionsort ss=new selektionsort(10);
//input isi
ss.SetData(0, 5);
ss.SetData(1, 3);
ss.SetData(2, 7);
ss.SetData(3, 6);
ss.SetData(4, 9);
ss.SetData(5, 4);
ss.SetData(6, 8);
ss.SetData(7, 2);
ss.SetData(8, 1);
ss.SetData(9, 4);
System.out.println("selektion sort");
System.out.println("--------------");
ss.PrintData();
//sort
ss.Sort();
ss.PrintData();
}
}
Pemrograman Java : merge sort (Pengurutan Gabung)
public class mergesort {
//property
private int [] data;
//konstruktor
public mergesort (int n){
this.data=new int[n];
}
//fungsi set data
public void SetData(int index, int value){
if (index >= 0 && index < this.data.length){
this.data[index]=value;
}
}
//fungsi print data
public void PrintData(){
String out="Data = ";
for(int a=0; a<this.data.length; a++){
out +=this.data[a]+"_";
}
System.out.println(out);
}
//fungsi merge sort
public void Sort(){
//tentukan posisi awal dan akhir
int i=0;
int j = this.data.length-1;
//urutkan
Mersort(i, j);
}
private void Mersort(int start, int end){
//temtukan jumlah data
int n=(end-start+1);
if (n>1){
//hitung nilai tengah
int mid = (start+end)/2;
//rekursif mergesort
Mersort(start, mid);
Mersort(mid+1, end);
//merge
int[]temp=new int[n];
int x=start;
int y=mid+1;
for(int a=0; a<n; a++){
if(x<=mid && y<=end){
if(this.data[x]<this.data[y]){
temp[a]=this.data[x];
x++;
} else {
temp[a]=this.data[y];
y++;
}
} else if (x <= mid){
temp[a]=this.data[x];
x++;
} else {
temp[a]=this.data[y];
y++;
}
}
//salin hasil pengurutan
for(int a=0; a<n; a++){
this.data[start+a]=temp[a];
}
}
}
public static void main(String[]args){
//buat objek
mergesort ms=new mergesort(10);
//input isi
ms.SetData(0, 5);
ms.SetData(1, 3);
ms.SetData(2, 7);
ms.SetData(3, 6);
ms.SetData(4, 9);
ms.SetData(5, 4);
ms.SetData(6, 8);
ms.SetData(7, 2);
ms.SetData(8, 1);
ms.SetData(9, 4);
//print
System.out.println("merge sort");
System.out.println("----------");
ms.PrintData();
//sort
ms.Sort();
//print
ms.PrintData();
}
}
//property
private int [] data;
//konstruktor
public mergesort (int n){
this.data=new int[n];
}
//fungsi set data
public void SetData(int index, int value){
if (index >= 0 && index < this.data.length){
this.data[index]=value;
}
}
//fungsi print data
public void PrintData(){
String out="Data = ";
for(int a=0; a<this.data.length; a++){
out +=this.data[a]+"_";
}
System.out.println(out);
}
//fungsi merge sort
public void Sort(){
//tentukan posisi awal dan akhir
int i=0;
int j = this.data.length-1;
//urutkan
Mersort(i, j);
}
private void Mersort(int start, int end){
//temtukan jumlah data
int n=(end-start+1);
if (n>1){
//hitung nilai tengah
int mid = (start+end)/2;
//rekursif mergesort
Mersort(start, mid);
Mersort(mid+1, end);
//merge
int[]temp=new int[n];
int x=start;
int y=mid+1;
for(int a=0; a<n; a++){
if(x<=mid && y<=end){
if(this.data[x]<this.data[y]){
temp[a]=this.data[x];
x++;
} else {
temp[a]=this.data[y];
y++;
}
} else if (x <= mid){
temp[a]=this.data[x];
x++;
} else {
temp[a]=this.data[y];
y++;
}
}
//salin hasil pengurutan
for(int a=0; a<n; a++){
this.data[start+a]=temp[a];
}
}
}
public static void main(String[]args){
//buat objek
mergesort ms=new mergesort(10);
//input isi
ms.SetData(0, 5);
ms.SetData(1, 3);
ms.SetData(2, 7);
ms.SetData(3, 6);
ms.SetData(4, 9);
ms.SetData(5, 4);
ms.SetData(6, 8);
ms.SetData(7, 2);
ms.SetData(8, 1);
ms.SetData(9, 4);
System.out.println("merge sort");
System.out.println("----------");
ms.PrintData();
//sort
ms.Sort();
ms.PrintData();
}
}
Pemrograman Java : Insertion Sort (pengurutan sisip)
public class insertionsort {
//property
private int [] data;
//konstruktor
public insertionsort (int n){
this.data=new int[n];
}
//fungsi set data
public void SetData(int index, int value){
if (index >= 0 && index < this.data.length){
this.data[index]=value;
}
}
//fungsi print data
public void PrintData(){
String out="Data = ";
for(int a=0; a<this.data.length; a++){
out +=this.data[a]+"|";
}
System.out.println(out);
}
//fungsi tukar nilai
private void Tukar(int i, int j){
int temp = this.data[i];
this.data[i]=this.data[j];
this.data[j]=temp;
}
//fungsi insertion sort
public void Sort(){
for(int a=1; a<this.data.length-1; a++){
int b=a;
while(b>0 && this.data[b]< this.data[b-1]){
Tukar(b, b-1);
b--;
}
}
}
public static void main(String[]args){
//buat objek
insertionsort is=new insertionsort(10);
//input isi
is.SetData(0, 5);
is.SetData(1, 3);
is.SetData(2, 7);
is.SetData(3, 6);
is.SetData(4, 9);
is.SetData(5, 4);
is.SetData(6, 8);
is.SetData(7, 2);
is.SetData(8, 1);
is.SetData(9, 4);
//print
System.out.println("insertion sort");
System.out.println("--------------");
is.PrintData();
//sort
is.Sort();
//print
is.PrintData();
}
}
//property
private int [] data;
//konstruktor
public insertionsort (int n){
this.data=new int[n];
}
//fungsi set data
public void SetData(int index, int value){
if (index >= 0 && index < this.data.length){
this.data[index]=value;
}
}
//fungsi print data
public void PrintData(){
String out="Data = ";
for(int a=0; a<this.data.length; a++){
out +=this.data[a]+"|";
}
System.out.println(out);
}
//fungsi tukar nilai
private void Tukar(int i, int j){
int temp = this.data[i];
this.data[i]=this.data[j];
this.data[j]=temp;
}
//fungsi insertion sort
public void Sort(){
for(int a=1; a<this.data.length-1; a++){
int b=a;
while(b>0 && this.data[b]< this.data[b-1]){
Tukar(b, b-1);
b--;
}
}
}
public static void main(String[]args){
//buat objek
insertionsort is=new insertionsort(10);
//input isi
is.SetData(0, 5);
is.SetData(1, 3);
is.SetData(2, 7);
is.SetData(3, 6);
is.SetData(4, 9);
is.SetData(5, 4);
is.SetData(6, 8);
is.SetData(7, 2);
is.SetData(8, 1);
is.SetData(9, 4);
System.out.println("insertion sort");
System.out.println("--------------");
is.PrintData();
//sort
is.Sort();
is.PrintData();
}
}
Langganan:
Postingan (Atom)