Showing posts with label Data Structures and Algorithms. Show all posts
Showing posts with label Data Structures and Algorithms. Show all posts

Tuesday, May 22, 2012

Program to Implement Selection Sort in Java+Explanation



Selection sort is said to be an efficient algorithm when considering small computations involving limited memory/resource but it is quite inefficient when compared to other techniques when huge computations are required.Selection sort is said to be efficient for small list of items because of its simplicity.

The following steps Explains the working of the Selection sort Algorithm


  1. Obtain the Item with the Minimal value from a List/Collection of items
  2. Swap the item with the Item in the first position of the Collection/List
  3. Repeat the Process for the rest of the Items in the Collection


The Following is the Complete Program Source Code in Java for Implementing Selection Sort Algorithm
 import java.io.*;
import java.lang.*;
class array
{
 DataInputStream get;
 int a[];
 int i,j,n;
 void getdata()
 {
 try
  {
   get=new DataInputStream(System.in);
   System.out.println("Enter the limit");
   n=Integer.parseInt(get.readLine());
   a=new int[n];
   System.out.println("Enter the elements in the array");
   for(i=0;ia[j])
   {
    temp=a[i];
    a[i]=a[j];
    a[j]=temp;
   }
  }
 }
 System.out.println("Elements in ascending order is:");
 for(i=0;i=0;i--)
 System.out.print(" "+a[i]);
 }
}
class selectionsort
{
 public static void main(String arg[])
 {
  array obj=new array();
  obj.getdata();
  obj.sorting();
 }
}

Sunday, May 13, 2012

Quick Sort Program in Java -Explanation+Complete Source Code

Quick-sort is a sorting technique commonly called divide and conquer algorithm. Quick-sort first divides a large array of items into two smaller arrays : the low items and high items(ie:all elements in first array is less than all elements in second). 
Quick sort algorithm involves three steps
  1.  An n element, called a pivot is picked from the array.Pivot is commonly the middle element of the array
  2. Rearrange the array elements such that all elements less than the pivot come before the pivot and all elements greater than the pivot come after the pivot,this step is called array partitioning
  3. Then a recursive sorting of the partitioned arrays is done individually
Following is the complete source code for Quick-sort program in Java.

 import java.io.*;  
 import java.lang.*;  
 class array  
 {  
//c-madeeasy.blogspot.com
  DataInputStream get=new DataInputStream(System.in);  
  int a[];  
  int i,n,h,l;  
  void getdata(int n,int x,int y)  
  {  
  try  
  {  
   a=new int[n];  
   System.out.println("Enter the elements");  
   for(i=0;i<n;i++)  
   a[i]=Integer.parseInt(get.readLine());  
  }  
  catch(Exception e)  
  {  
   System.out.println(e.getMessage());  
  }  
  l=x;  
  h=y;  
  }  
  void sort(int l,int h)  
  {  
  int temp,key,low,high;  
  low=l;  
  high=h;  
  key=a[(low+high)/2];  
  while(low<=high)  
  {  
   while(key>a[low])  
   {  
   low++;  
   }  
   while(key<a[high])  
   {  
   high--;  
  }  
  if(low<=high)  
   {  
   temp=a[low];  
   a[low]=a[high];  
   a[high]=temp;  
   low++;  
   high--;  
   }  
   }  
   if(l<low-1)  
   {  
   sort(l,low-1);  
   }  
   if(low<h)  
   {  
   sort(low,h);  
   }  
  }  
  void display(int n)  
  {  
  System.out.println("Asending order is");  
  for(i=0;i<n;i++)  
  System.out.println(" "+a[i]);  
  }  
  }  
  class quicksort  
  {  
  public static void main(String arg[])  
  {  
   array obj=new array();  
   DataInputStream get=new DataInputStream(System.in);  
   int n,x,y;  
   n=0;  
  try  
   {  
   System.out.println("Enter the limit");  
   n=Integer.parseInt(get.readLine());  
   }  
  catch(Exception e)  
   {  
   System.out.println(e.getMessage());  
  }  
  x=0;  
  y=n-1;  
  obj.getdata(n,x,y);  
  obj.sort(x,y);  
  obj.display(n);  
  }  
  }  

Friday, August 19, 2011

Java Program to Impliment Circular Queue

The Java Source Code given below can be used to Implement a Circular Queue.
 import java.io.*;  
 import java.lang.*;  
 class clrqueue  
 {  
  DataInputStream get=new DataInputStream(System.in);  
  int a[];  
  int i,front=0,rear=0,n,item,count=0;  
  void getdata()  
  {  
  try  
   {  
   System.out.println("Enter the limit");  
   n=Integer.parseInt(get.readLine());  
   a=new int[n];  
   }   
  catch(Exception e)  
   {  
   System.out.println(e.getMessage());  
   }  
  }  
  void enqueue()  
  {  
   try  
   {  
   if(count<n)  
    {  
    System.out.println("Enter the element to be added:");  
    item=Integer.parseInt(get.readLine());  
    a[rear]=item;  
     rear++;  
    count++;  
    }  
   else  
    System.out.println("QUEUE IS FULL");  
   }  
  catch(Exception e)  
   {  
   System.out.println(e.getMessage());  
   }  
  }  
  void dequeue()  
  {  
   if(count!=0)  
    {  
    System.out.println("The item deleted is:"+a[front]);  
    front++;  
    count--;  
    }  
   else  
    System.out.println("QUEUE IS EMPTY");  
  if(rear==n)  
   rear=0;  
  }  
  void display()  
  {  
   int m=0;  
   if(count==0)  
   System.out.println("QUEUE IS EMPTY");  
   else  
   {  
   for(i=front;m<count;i++,m++)  
   System.out.println(" "+a[i%n]);  
   }  
  }  
 }  
 class myclrqueue  
 {  
  public static void main(String arg[])  
  {  
  DataInputStream get=new DataInputStream(System.in);  
  int ch;  
  clrqueue obj=new clrqueue();  
  obj.getdata();  
  try  
  {  
   do  
   {  
   System.out.println(" 1.Enqueue  2.Dequeue  3.Display  4.Exit");  
   System.out.println("Enter the choice");  
   ch=Integer.parseInt(get.readLine());  
   switch (ch)  
   {  
   case 1:  
       obj.enqueue();  
      break;  
   case 2:  
      obj.dequeue();  
      break;  
   case 3:  
      obj.display();  
      break;  
   }  
   }  
   while(ch!=4);  
  }  
  catch(Exception e)  
  {  
  System.out.println(e.getMessage());  
  }  
  }  
 }  

Java Program to Add two Numbers using Linked List

The Program below inserts two numbers to a Linked list and Adds them to produce the output.
 import java.io.*;  
 import java.lang.*;  
 class node  
 {  
   int data;  
   node next;  
   node prev;  
   node(int d)  
   {  
     data=d;  
     next=null;  
     prev=null;  
   }  
 }  
 class list  
 {  
   node first=null;  
   node curr=null;  
   void insert(int d)  
   {  
     node n1=new node(d);  
     curr=first;  
     if(curr==null)  
       first=n1;  
     else  
     {  
       while(curr.next!=null)  
         curr=curr.next;  
       curr.next=n1;  
     }  
   }  
   void display()  
   {  
     curr=first;  
     if(curr==null)  
       System.out.println("no list");  
     else  
     {  
       while(curr!=null)  
       {  
         System.out.print(curr.data);  
         curr=curr.next;  
       }  
     }  
   }  
 }  
 public class Sum {  
   public static void main(String[] args) {  
    DataInputStream get=new DataInputStream(System.in);  
    list l1=new list();  
    list l2=new list();  
    list l3=new list();  
    node curr1,curr2,curr3;  
    int n1,n2,n3;  
    int p,q,d,e;  
    try  
    {  
      System.out.println("Enter 1st no:");  
      n1=Integer.parseInt(get.readLine());  
      System.out.println("Enter 2nd no:");  
      n2=Integer.parseInt(get.readLine());  
      p=0;  
      while(n1>0)  
      {  
        d=n1%10;  
        p++;  
        l1.insert(d);  
        n1=n1/10;  
      }  
      n3=n2;  
      q=0;  
      while(n3>0)  
      {  
        d=n3%10;  
        q++;  
        l2.insert(d);  
        n3=n3/10;  
      }  
      while(p>q)  
      {  
        l2.insert(0);  
        p--;  
      }  
      while(q>p)  
      {  
        l1.insert(0);  
        q--;  
      }  
      e=0;  
      curr1=l1.first;  
      curr2=l2.first;  
      while(curr1!=null&&curr2!=null)  
      {  
        e=e+curr1.data+curr2.data;  
        if(e>=10)  
        {  
         d=e%10;  
         l3.insert(d);  
         e=e/10;  
        }  
        else  
        {  
          l3.insert(e);  
          e=0;  
        }  
      curr1=curr1.next;  
      curr2=curr2.next;  
      }  
      node temp=null;  
      int s=0;  
      int i,j;  
      curr3=l3.first;  
      while(curr3!=null)  
      {  
        temp=curr3;  
        s++;  
        curr3=curr3.next;  
      }  
      curr3=l3.first;  
      s=s/2;  
      for(j=0;j<s;j++)  
      {  
        i=curr3.data;  
        curr3.data=temp.data;  
        temp.data=i;  
        curr3=curr3.next;  
        temp=temp.prev;  
      }  
      System.out.println("Sum=");  
      l3.display();  
    }  
    catch(Exception k)  
    {  
      System.out.println(k.getMessage());  
    }  
   }  
 }  

Saturday, August 13, 2011

Stack using Array in Java+Complete Source Code

Stack is a Linear Data Structure.Stack can be Implemented easily by using an array in Java or C.Every Stack has two Main Operations/Methods

a)Push()-Used to Insert an Element to the Stack

b)Pop()- Used to Remove an Element from the Stack

Stack follows a LIFO (Last in First Out)approach.In this method the element inserted last is removed first.The Topmost Element of the Stack is referred to as the top of the stack.When an element is inserted it is inserted at a position above the top.Pop method is used to remove an element from the Stack and always the element at the top is removed from the Stack.
Stack may Throw two exceptions 1.Stack Overflow 2.Stack Empty these can be handled by checking if(top=array length) and if(top=0) respectively.The Complete Source Program to Implement a Stack using array is Provided Below.


 package stack;  
 import java.io.*;  
 import java.lang.*;  
 import java.util.logging.Level;  
 import java.util.logging.Logger;  
 /**  
  *  
  * @author www.c-madeeasy.blogspot.com  
  */  
  class mystack {  
  DataInputStream get=new DataInputStream(System.in);  
  int a[];  
  int i,top=0,n,item,out;  
  void getdata()  
  {  
  try  
   {  
   System.out.println("Enter the limit");  
   n=Integer.parseInt(get.readLine());  
   a=new int[n];  
   }   
  catch(Exception e)  
   {  
   System.out.println(e.getMessage());  
   }  
  }  
  void push(int item)  
  {  
    if(top==n)  
    {  
      System.out.println("STACK IS FULL");  
    }  
    else  
    {  
    a[top]=item;  
    top++;  
    }  
  }  
  void pop()  
  {  
    if(top==0)   
    {  
     System.out.println("STACK EMPTY");   
    }  
    else  
    {  
      top--;  
      out=a[top];  
    }  
    System.out.println(out);  
  }  
  void display()  
  {  
    if(top==0){  
      System.out.println("STACK EMPTY");    
    }  
    else  
   {  
   for(i=top-1;i>=0;i--)  
    System.out.println(+a[i]);  
   }  
  }  
   }  
 class Stack  
 {  
   public static void main(String[]args)  
   {  
      DataInputStream get=new DataInputStream(System.in);  
  int ch = 0,t = 0;  
  mystack obj=new mystack();  
  obj.getdata();  
  System.out.println("1.PUSH 2.POP 3.DISPLAY");  
     try {  
       ch=Integer.parseInt(get.readLine());  
     } catch (IOException ex) {  
       Logger.getLogger(Stack.class.getName()).log(Level.SEVERE, null, ex);  
     }  
     while(ch!=4)  
     {  
       System.out.println("1.PUSH 2.POP 3.DISPLAY");  
  switch(ch)  
  {  
    case 1:  
      try{  
      t=Integer.parseInt(get.readLine());  
      }  
      catch(IOException e)  
      {  
      }  
      System.out.println("value");  
       try {  
       t=Integer.parseInt(get.readLine());  
       obj.push(t);  
     } catch (IOException ex) {  
       Logger.getLogger(Stack.class.getName()).log(Level.SEVERE, null, ex);  
     }  
      break;  
    case 2:  
      obj.pop();  
      break;  
          case 3:obj.display();  
            break;  
  }  
 }  
   }  
 }  

Queue using array in Java + Complete Program Source Code to Implement a Queue

Queue is an a Linear Data Structure which follows a FIFO(First in First Out) approach.There are two operations for a Queue
a)Enqueue -Insert an Element to the Queue
b)DeQueue -Remove an Element from the Queue

The Element Inserted First is Removed first from the Queue.A Queue has two main Postions
a)Rear- The Position/Element at the End of the Queue
b)Front-The Front most Element/Position of the Queue
In a Queue elements are Enqueued to the Rear and DeQueued from the Front.
The Program given below shows the Implementation a Queue un Java using an Array.

 import java.io.*;  
 import java.lang.*;  
 class clrqueue  
 {  
  DataInputStream get=new DataInputStream(System.in);  
  int a[];  
  int i,front=0,rear=0,n,item,count=0;  
  void getdata()  
  {  
  try  
   {  
   System.out.println("Enter the limit");  
   n=Integer.parseInt(get.readLine());  
   a=new int[n];  
   }   
  catch(Exception e)  
   {  
   System.out.println(e.getMessage());  
   }  
  }  
  void enqueue()  
  {  
   try  
   {  
   if(count<n)  
    {  
    System.out.println("Enter the element to be added:");  
    item=Integer.parseInt(get.readLine());  
    a[rear]=item;  
     rear++;  
    count++;  
    }  
   else  
    System.out.println("QUEUE IS FULL");  
   }  
  catch(Exception e)  
   {  
   System.out.println(e.getMessage());  
   }  
  }  
  void dequeue()  
  {  
   if(count!=0)  
    {  
    System.out.println("The item deleted is:"+a[front]);  
    front++;  
    count--;  
    }  
   else  
    System.out.println("QUEUE IS EMPTY");  
  if(rear==n)  
   rear=0;  
  }  
  void display()  
  {  
   int m=0;  
   if(count==0)  
   System.out.println("QUEUE IS EMPTY");  
   else  
   {  
   for(i=front;m<count;i++,m++)  
   System.out.println(" "+a[i]);  
   }  
  }  
 }  
 class Myqueue  
 {  
  public static void main(String arg[])  
  {  
  DataInputStream get=new DataInputStream(System.in);  
  int ch;  
  clrqueue obj=new clrqueue();  
  obj.getdata();  
  try  
  {  
   do  
   {  
   System.out.println(" 1.Enqueue  2.Dequeue  3.Display  4.Exit");  
   System.out.println("Enter the choice");  
   ch=Integer.parseInt(get.readLine());  
   switch (ch)  
   {  
   case 1:  
       obj.enqueue();  
      break;  
   case 2:  
      obj.dequeue();  
      break;  
   case 3:  
      obj.display();  
      break;  
   }  
   }  
   while(ch!=4);  
  }  
  catch(Exception e)  
  {  
  System.out.println(e.getMessage());  
  }  
  }  
 }  

Which is the Best Photo Watermarking Software

Photo Theft is becoming more and more common in the web with the outburst of social websites like Facebook,Google Plus and Image sharing se...