Prepare for interview

November 2, 2006

Kelas Untuk bilangan Prima
public class PrimeNumber {

/**
* @param args
*/
public static void main(String[] args) {

//prime number from 0 – 100
int x=100;
System.out.println(2);//2 is exception case
for (int i = 0; i < x; i++) {

for(int j = 2; j <= i-1; j++) {
if (i%j==0){
//not prime
break;
} else if( j==i-1){
//already test for all number
System.out.println(i);

}
}
}

}

}

kelas untuk insertion Algoritma
public class InsertionAlgoritme {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int [] x=new int [5];
x[0]=9;
x[1]=7;
x[2]=3;
x[3]=6;
x[4]=10;

insertionSort(x);
System.out.print(“[");
for (int i = 0; i < x.length; i++) {
if (i==x.length-1)
System.out.print(x[i]+”]”);
else
System.out.print(x[i]+”,”);

}

}

private static void insertionSort(int numbers[]){
int i, j, index;
// {8,5,3,9,1}

for (i=1; i < numbers.length; i++) {

index = numbers[i];//5 ===>3

j = i;//j=1
while ((j > 0) && (numbers[j-1] > index)) {//8>5 ===>
numbers[j] = numbers[j-1];//[1]–>{8,8,3,9,1} ==> {5,3,8,9,1}
j = j – 1;
}
numbers[j] = index;//–>{5,8,3,9,1}
}
}

}

Entry Filed under: Java. .

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Archives

Blog Stats

Meta

Visitors to this site

Blogroll

Friends