Sunday, 1 November 2015

Friends here is a link for java Text Book    :                                                                                                                                          https://docs.google.com/file/d/0BxbayAAcS8Iid1RfMFRGYklBRlE/edit
                            http://www.pdfdrive.net/java-the-complete-reference-7th-edition-wee3625514.html

Wednesday, 28 October 2015



//This is a java program to search an element using Fibonacci search 


import java.util.Scanner

public class Fibonacci_Search
{
    static int kk = -1, nn = -1;
    static int fib[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377, 610, 998, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368,
            75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986,
102334155, 165580141 };


    static int fibsearch(int a[], int n, long x)
    {
        int inf = 0, pos, k;
        if (nn != n)
        {
            k = 0;
            while (fib[k] < n)
                k++;
            kk = k;
            nn = n;
        }
        else
            k = kk;

        while (k > 0)
        {
            pos = inf + fib[--k];

            if ((pos >= n) || (x < a[pos]))
                ;
            else if (x > a[pos])
            {
                inf = pos + 1;
                k--;
            }
            else
                return pos;
        }
        return -1;
    }


    public static void main(String args[])
    {
        int arr[] = { 2, 3, 45, 56, 67, 78, 89, 99, 100, 101 };
        int num, pos;
        Scanner scan = new Scanner(System.in);

System.out.println("Array elements are :  ");
for(int i=0;i<arr.length;i++)
System.out.print("\t"+arr[i]);

       System.out.println("\nEnter an element to search: ");
        num = scan.nextInt();

        pos = fibsearch(arr, 10, num);

        if (pos >= 0)
            System.out.println("\nElement is at index : "
                    + fibsearch(arr, 10, num));
        else
            System.out.println("\nElement NOT found!! ");
        scan.close();
    }


}

output :-

Sunday, 11 October 2015

Merge Sort program in Data Structures through Java



import java.util.Scanner;
class mergesort
{
public static void main(String args[])
{
Scanner sc=new Scanner (System.in);
    int i,j,k,temp;

System.out.println("Enter size of the first list: ");
    int n1=sc.nextInt();

int[] a = new int[n1];

System.out.println("Enter "+ n1 +" first list elements : ");
for(i=0;i<n1;i++)
a[i]=sc.nextInt();

System.out.println("Enter size of the second list: ");
    int n2=sc.nextInt();
   
int[] b = new int[n2];
int[] c = new int[n1+n2];


System.out.println("Enter "+ n2 +" second list elements : ");
for(i=0;i<n2;i++)
b[i]=sc.nextInt();


for(i=0;i<=n1-2;i++)
{
for(j=i+1;j<=n1-1;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}

for(i=0;i<=n2-2;i++)
{
for(j=i+1;j<=n2-1;j++)
{
if(b[i]>b[j])
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
}
}

for(i=j=k=0;i<(n1+n2);)
{
if(a[j]<=b[k])
c[i++]=a[j++];
else
c[i++]=b[k++];

if(j==n1 || k==n2)
break;

}

while(j<n1)
c[i++]=a[j++];

while(k<n2)
c[i++]=b[k++];

System.out.println("\nArray elements after sorting are : ");

for(i=0;i<(n1+n2);i++)
System.out.print("\t"+c[i]);


sc.close();
  }

}


Output :-







Monday, 5 October 2015

Binary search program using java

/* To implement Binary Search on sorted list  */

import java.util.Scanner;
public class BinarySearch
{
 public static void main(String args[])
 {
  Scanner sc = new Scanner(System.in);
  int i,flag=0,lower=0,upper,mid;

  System.out.println("Enter size of the list");
  int n=sc.nextInt();
 
  int a[]=new int[n];
  upper=n-1;
  System.out.println("Enter sorted list elements : ");
  for(i=0;i<n;i++)
  {
   a[i]=sc.nextInt();
  }
   
  System.out.println("sorted list elements : ");
  for(i=0;i<n;i++)
  {
   System.out.println("\t"+a[i]);
  }
  System.out.println("Enter element to search ");
  int num=sc.nextInt(); 
  for(mid=(lower+upper)/2;lower<=upper;mid=(lower+upper)/2)
  {
   if(a[mid]==num)
   {
    flag=1;
    break;
   }
  
   if(num > a[mid])
    lower=mid+1;
   else
    upper=mid-1;
  }
  if(flag==1)
   System.out.println(num+" is found at position : "+(mid+1));
  else
   System.out.println("Element is not presetn in the list");
  sc.close();
 }
}

output :