Sunday, May 13, 2012

N-Queen


public class NQueens {
 
    int[] x;
 
    public NQueens(int N) {
        x = new int[N];
 
    }
 
    public boolean canPlaceQueen(int r, int c) {
      
        for (int i = 0; i < r; i++) {
            if (x[i] == c || (Math.abs(i - r) == Math.abs(x[i] - c)) ) 
            {
                return false;
            }
        }
        return true;
    }
 
    public void printQueens(int[] x) {
        int N = x.length;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (x[i] == j) {
                    System.out.print("Q ");
                } else {
                    System.out.print("* ");
                }
            }
            System.out.println();
        }
        System.out.println();
    }
 
    public void placeNqueens(int r, int n) {
       
        for (int c = 0; c < n; c++) {
            if (canPlaceQueen(r, c)) {
                x[r] = c;
                if (r == n - 1) {
                   printQueens(x);
                } else {
                    placeNqueens(r + 1, n);
                }
            }
        }
    }
 
    public void callplaceNqueens() {
        placeNqueens(0, x.length);
    }
 
    public static void main(String args[]) {
        NQueens Q = new NQueens(5);
        Q.callplaceNqueens();
 
      
    }
}

output

Q * * * *
* * Q * *
* * * * Q
* Q * * *
* * * Q *
Q * * * *
* * * Q *
* Q * * *
* * * * Q
* * Q * *
* Q * * *
* * * Q *
Q * * * *
* * Q * *
* * * * Q
* Q * * *
* * * * Q
* * Q * *
Q * * * *
* * * Q *
* * Q * *
Q * * * *
* * * Q *
* Q * * *
* * * * Q
* * Q * *
* * * * Q
* Q * * *
* * * Q *
Q * * * *
* * * Q *
Q * * * *
* * Q * *
* * * * Q
* Q * * *
* * * Q *
* Q * * *
* * * * Q
* * Q * *
Q * * * *
* * * * Q
* Q * * *
* * * Q *
Q * * * *
* * Q * *
* * * * Q
* * Q * *
Q * * * *
* * * Q *
* Q * * *

Tuesday, February 28, 2012

• Read value from a file and sort the values use merge sort and quick sort in java

import java.io.*;
import java.util.*;
public class Arraysort{
int[] list;
int n;
public Arraysort(int n){
this.n=n;
list=new int[n];
}
public void Mergesort(){
System.out.println("merge sort");
 mergesort(0,n-1);
 print();
}
public void QuickSort(){
System.out.println("Quick sort");
 quickSort(0,n-1);
 print();
}
public void readFile() throws IOException{
 Scanner filesean=new Scanner(new FileReader("inputtext.txt"));
 int i=0;
 while(filesean.hasNext() && i<n){
 list[i]=filesean.nextInt();
 i++;
 }
 System.out.println("input array");
 for(int j=0;j<n;j++){
  System.out.print(list[j]+" ");
 }
 System.out.println();
 filesean.close();
}


   public void mergesort(int l,int r){
       if(l<r){
           int mid=(l+r)/2;
           mergesort(l,mid);
           mergesort(mid+1,r);
           merge(l,mid,r);
       }
   }
    private void merge(int l, int m, int r) {
       int i=l; int j=m+1; int k=0;
       int[] temp=new int[r-l+1];
       while(i<=m && j<=r){
           if(list[i]<list[j])temp[k++]=list[i++];
           else temp[k++]=list[j++];
       }
       while(i<=m) temp[k++]=list[i++];
       while(j<=r) temp[k++]=list[j++];
       for(k=0;k<r-l+1;k++) list[l+k]=temp[k];
    }

  public void quickSort(int p,int q){
     int j;
     if(p<q){
            j=partition(p,q);
                quickSort(p,j-1);
                quickSort(j+1,q);
     }
 }
    private int partition(int p, int q) {
       int i=p;
       int j=q+1;
       int pivot=list[p];
       do{
           do{ i++;
           }while(list[i]<pivot);
        do{j--;
       }while(list[j]>pivot);
  
         if(i<j){
             swap(i,j);}
         }while(i<j);
       swap(p,j);
     
    
     return j;
}
    private void swap(int i, int i0){
        int temp=list[i];
        list[i]=list[i0];
        list[i0]=temp;
    }
public void print(){
 for(int j=0;j<n;j++){
  System.out.print(list[j]+" ");
 }
 System.out.println();
}
 public static void main(String[] args) throws IOException{
  Arraysort s=new Arraysort(15);
  s.readFile();
  s.Mergesort();
  System.out.println();
  s.QuickSort();
 
}
}
/* Output
input array
1 5 3 2 6 4 9 1 6 8 22 78 25 46 11
merge sort
1 1 2 3 4 5 6 6 8 9 11 22 25 46 78
Quick sort
1 1 2 3 4 5 6 6 8 9 11 22 25 46 78
*/

Sunday, January 29, 2012

Finding Maximum and Minimum

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package serch;

/**
 *
 * @author KAPILAN
 */
public class MaxMin {
int [] array;
int n,max,min;
public MaxMin(int n){
    this.n=n;
    array=new int[n];
    for(int i=0;i<n;i++){
        array[i]=(int) Math.round(Math.random()*22+1000);
    }
}
public void findmaxmin(){
    max=array[0];
    min=array[0];
    for(int i=0;i<n;i++){
        if(array[i]<min)min=array[i];
        else if(array[i]>max)max=array[i];
       
    }
  }
    public void print(){
        findmaxmin();
        for(int i=0;i<n;i++){
            System.out.print(array[i]+" ");

        }
        System.out.println();
        System.out.println("max = "+max);
        System.out.print("min = "+min);
    }
    public static void main(String[] a){
        MaxMin s1=new MaxMin(10);
        s1.print();
    }
}
/*
 * 1002 1020 1012 1007 1016 1012 1001 1021 1020 1001
max = 1021
min = 1001
 */

binary search

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package serch;

/**
 *
 * @author KAPILAN
 */
public class BSearch {
    int [] array={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
    int n;
    int key;
  public BSearch(){
      //this.key=key;
     // array={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
      n=array.length;

  }
  public int serch(int key){
     return serch(key,0,n-1);
  }

    public int serch(int key,int s, int e) {
        int mid=(s+e)/2;
       if(e<s){return -1;}
     
       else if(key==array[mid]){return mid;}

        else if(key<array[mid]){return serch(key,s,mid-1);}
    else{return serch(key,mid+1,e);}
    }
public void print(int key){
    System.out.println("index of "+key+"  =   "+serch(key));
 
}
public static void main(String[] a){
BSearch s1=new BSearch();
s1.print(20);
}
}
/*
 index of 10  =   9
 * index of 22  =   -1
 * index of 2  =   1
 * index of 0  =   -1
 * index of 20  =   19
 */

MergeSort

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mergesort;

import javax.swing.JOptionPane;

/**
 *
 * @author KAPILAN
 */
public class MergeSort {
    int[] array;
    int size;
   public MergeSort(int n){
       size=n;
       array=new int[size];
       for(int i=0;i<size;i++){
           array[i]=(int) Math.round(Math.random()*900+100);
       }
   }
   public void mergesort(){
       mergesort(0,size-1);
   }
   public void mergesort(int l,int r){
       if(l<r){
           int mid=(l+r)/2;
           mergesort(l,mid);
           mergesort(mid+1,r);
           merge(l,mid,r);
       }
   }

    private void merge(int l, int m, int r) {
       int i=l; int j=m+1; int k=0;
       int[] temp=new int[r-l+1];
       while(i<=m && j<=r){
           if(array[i]>array[j])temp[k++]=array[i++];
           else temp[k++]=array[j++];
       }
       while(i<=m) temp[k++]=array[i++];
       while(j<=r) temp[k++]=array[j++];
       for(k=0;k<r-l+1;k++) array[l+k]=temp[k];
    }
public void display(){
    int psize=15;
    String ds="";
    int rows=(size-1)/psize+1;
    int k=0;
    int i,row;
            for(row=1;row<rows;row++){
                for(i=0;i<psize;i++)
                    ds=ds+" : "+array[k++];
                    ds=ds+"\n";

            }
    for(i=k;i<size;i++)ds=ds+" : "+array[i];
    JOptionPane.showMessageDialog(null,ds,"contents of the array",1);
    System.out.print(ds);
}
public static void main(String[] a){
     MergeSort s1=new  MergeSort(20);
     System.out.println("Before Sort");
     s1.display();
     System.out.println();
     s1.mergesort();
     System.out.println("After Sort");
     s1.display();
}
}
/*Before Sort
 : 594 : 646 : 503 : 606 : 156 : 333 : 574 : 878 : 284 : 884 : 742 : 941 : 768 : 517 : 269
 : 563 : 853 : 783 : 989 : 117
After Sort
 : 989 : 941 : 884 : 878 : 853 : 783 : 768 : 742 : 646 : 606 : 594 : 574 : 563 : 517 : 503
 : 333 : 284 : 269 : 156 : 117*/

QuickSort

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sort;

/**
 *
 * @author KAPILAN
 */
public class QuickSort {
    int[] num;
    int n;
    public QuickSort(int n){
        this.n=n;
       // p=0;q=n;
        num=new int[n+1];
        for(int i=0;i<n;i++){
            num[i]= (int) Math.round(Math.random()*n/2+1000);

        }
        num[n]=99999;
       // quickSort(0,n-1);
    }
    public void quickSort(){
     quickSort(0,n-1);
    }
 public void quickSort(int p,int q){
     int j;
     if(p<q){
            j=partition(p,q);
                quickSort(p,j-1);
                quickSort(j+1,q);

     }
 }

    private int partition(int p, int q) {
       int i=p;
       int j=q+1;
       int pivot=num[p];
       do{
           do{ i++;
           }while(num[i]<pivot);
        do{j--;
       }while(num[j]>pivot);
   
         if(i<j){
             swap(i,j);}
         }while(i<j);
       swap(p,j);
     
     
     return j;
}

    private void swap(int i, int i0){
        int temp=num[i];
        num[i]=num[i0];
        num[i0]=temp;
    }
    public void print(){
       
       
        for(int i=0;i<n;i++){
            System.out.print(num[i]+" ");
        }
    }
 
 public static void main(String []a){
      QuickSort s=new QuickSort(100);
       System.out.println("Before Sort");
       s.print();
       s.quickSort();
       System.out.println();
       System.out.println("after Sort");
       s.print();
 }
}
/*Before Sort
1033 1006 1047 1045 1004 1050 1008 1014 1004 1034 1044 1040 1016 1033 1038 1018 1004 1046 1002 1027 1026 1017 1023 1026 1049 1022 1013 1050 1046 1033 1009 1002 1026 1003 1047 1005 1003 1006 1042 1042 1029 1044 1003 1021 1025 1036 1009 1040 1047 1024 1007 1041 1044 1047 1004 1021 1025 1039 1007 1024 1043 1030 1017 1039 1043 1029 1019 1047 1049 1040 1013 1001 1039 1016 1017 1007 1018 1028 1010 1047 1033 1007 1042 1013 1050 1018 1005 1016 1028 1046 1022 1008 1039 1020 1040 1002 1024 1043 1042 1030
after Sort
1001 1002 1002 1002 1003 1003 1003 1004 1004 1004 1004 1005 1005 1006 1006 1007 1007 1007 1007 1008 1008 1009 1009 1010 1013 1013 1013 1014 1016 1016 1016 1017 1017 1017 1018 1018 1018 1019 1020 1021 1021 1022 1022 1023 1024 1024 1024 1025 1025 1026 1026 1026 1027 1028 1028 1029 1029 1030 1030 1033 1033 1033 1033 1034 1036 1038 1039 1039 1039 1039 1040 1040 1040 1040 1041 1042 1042 1042 1042 1043 1043 1043 1044 1044 1044 1045 1046 1046 1046 1047 1047 1047 1047 1047 1047 1049 1049 1050 1050 1050 */

Wednesday, January 4, 2012

palindrome

public class Stack{
private Object element[];
private int top;
int maxSize;
private String x,y;

public Stack(int s,String x){
maxSize=s;
element=new Object[maxSize];
top=-1;
this.x=x;
y="";
}

public boolean isEmpty(){
return(top==-1);
}

public void push(Object elt){
top=top+1;
element[top]=elt;
}

public Object pop(){
Object e=element[top];
top=top-1;
return e;
}

public Object peek(){
return element[top];
}

public void change(){

for(int i=0;i<x.length();i++){
push(x.charAt(i));
}
}

public void set(){
while(!isEmpty()){
y=y+pop();
}
System.out.println(y);

}

public boolean eguals(){
return(x.compareTo(y)==0);
}

public static void main(String a[]){
Stack s=new Stack(5,"amma");
s.change();
s.set();
System.out.println("palindrome  "+s.eguals());

Stack s2=new Stack(5,"ammaa");
s2.change();
s2.set();
System.out.println("palindrome  "+s2.eguals());
}
}


Output

/*C:\Users\KAPILAN\Desktop>java Stack
amma
palindrome  true
aamma
palindrome  false

C:\Users\KAPILAN\Desktop>*/