Sunday, January 29, 2012

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>*/

Thursday, December 8, 2011

LinearSearch

import javax.swing.*;
public class LinearSearch{
    int [] array;
int n;
   
   
    public LinearSearch(int n){
        this.n = n;
        array = new int[n];
        for(int i = 0; i< n; i++){
            array[i]=getRandom(0,2*n);
        }
    }
   
    public int search(int key){
        int index=0;
        while (index < n && key!=array[index]) index++;
       
        if (index==n) return -1;
        return index;
    }
   
    public static int getRandom(int from, int to){
        return (int)Math.round(Math.random()*(to-from)+from);
    }

public void shortarray(int arraySize){
int temp=0;
for(int i=0;i<arraySize;i++){
for(int j=i;j<arraySize;j++){
if(array[i]>array[j]){
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
/*System.out.println();

for(int i=0;i<2000;i++){
System.out.print(array[i]+" ");
}*/
}


     public static void main(String args[]){
        int arraySize=2000;
LinearSearch ls = new LinearSearch(arraySize);

ls.shortarray(arraySize);

        for(int repeat=0; repeat< 100; repeat++){
   int numberOfTries=5000;
            int key, result;
            int successCount=0;
            int failureCount=0;
            int averageComparisonForSuccess=0;
            for(int i=0; i< numberOfTries; i++){
                key=ls.getRandom(0,2*arraySize);
                result=ls.search(key);
               
                if (result==-1) failureCount++;
                else{successCount++;
                    averageComparisonForSuccess+=result+1;
                }
            }
            if (successCount>0) averageComparisonForSuccess=averageComparisonForSuccess/successCount;
            String analysisResult="---Search Analysis Report "+(repeat+1)+"---\n"+
            "Array Size="+ls.array.length+
            "\nNumber of Tries="+numberOfTries+
            "\nSuccessfull Searches="+successCount+"\n"+
            "Unsuccessfull Searches="+failureCount+"\n"+
            "Average Count of Comparisons in Successfull Searches="+ averageComparisonForSuccess+
            "\n---End of Report--\n";
           
         
     
            System.out.println(analysisResult);

        }

    }


}

Wednesday, November 30, 2011

convert the 1D array to 2D array

public class darry{
int[] d={1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7};
int[][] td=new int[4][4];

public void put(int k,int e){
int i=k/4;
int j=k%4;
td[i][j]=e;
}
public void stor(){
for(int k=0;k<16;k++){
int e=d[k];
put(k,e);
}
}
public int get(int k){
int i=k/4;
int j=k%4;
return td[i][j];
}
public void print(){
for(int k=0;k<16;k++){
System.out.print(d[k]+" ");
}
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
System.out.print(td[i][j]+" ");
}
System.out.println();
}
}
public static void main(String[] a){
darry s=new darry();
s.stor();
s.print();
System.out.println(s.get(5));
System.out.println(s.get(10));
System.out.println(s.get(15));
}
}

convert the 2D array to 1D array

import java.util.*;
public class tDarry{
int n,c,r,e;

int[] d=new int[16];
int td[][]={{1,2,3,4},{5,4,6,8},{9,8,7,5},{3,6,4,7}};

public void put(int i,int j,int e){
int k=i*4+j;
  d[k]=e;

}
public int get(int i,int j){
int[] d=new int[16];
int k=i*4+j;
return d[k];
}
public void stor(){
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){

int e=td[i][j];
put(i,j,e);

}
}

}
public void print(){

for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
System.out.print(td[i][j]+" ");
}
System.out.println();
}
for(int k=0;k<16;k++){
System.out.print(d[k]+" ");
}
}
public static void main(String[] a){

tDarry s=new tDarry();
s.stor();
s.print();

}
}

Wednesday, November 16, 2011

find next odd,even,prime and Composite numbers

import java.util.*;
public class mynumber{
int num,odd,even,comp,prime;
public mynumber(){
num=0;
odd=1;
even=1;
comp=4;
prime=3;
}

public int next_number(){
num=num+1;
return num;
}

public boolean isodd(int n){
if(n%2==1)
return true;
else return false;
}

public int next_odd(){
odd=odd+1;
while(!isodd(odd)){
odd=odd+1;
}
return odd;
}

public int next_even(){
even=even+1;
while(isodd(even)){
even=even+1;
}
return even;
}
public int next_comp(){
comp=comp+1;

for(int i=2;i<comp;i++){
if(comp%i==0) break;
else comp=comp+1;
}
return comp;
}
public int next_prime(){
prime=prime+1;
int x=0;
for(int i=2;i<prime;i++){
if(prime%i==0)  prime=prime+1;
else break;
}
return prime;
}

public static void main(String[] a){
mynumber n=new mynumber();

System.out.println("next odd number= "+n.next_odd());
System.out.println("next even number= "+n.next_even());
System.out.println("next Composite number= "+n.next_comp());
System.out.println("next prime number= "+n.next_prime());
}
}
/*C:\Users\KAPILAN\Desktop>javac mynumber.java

C:\Users\KAPILAN\Desktop>java mynumber
next odd number= 3
next even number= 2
next Composite number= 6
next prime number= 5*/