Prepare for interview

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}
}
}

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: