Showing posts with label Sorting/Searching Techniques. Show all posts
Showing posts with label Sorting/Searching Techniques. 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

Binary Search Program Source Code in Java

The Java Program given below can be used to find an Element in array using Binary Search Technique.
The Main steps Involved here are
1)A Pivot Element is Found
2)The array is sorted in such a way that elements greater than the pivot lies to the right and elements lesser lies to the left of the pivot.
3)Linear search is applied by taking the appropriate limits.,depending upon whether the element to be searched is greater or lesser than the pivot. 
 class array  
 {  
  DataInputStream get;  
  int a[];  
  int i,j,n,key;  
  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");  
   for(i=0;i<n;i++)  
   a[i]=Integer.parseInt(get.readLine());  
  }  
  catch(Exception e)  
  {  
   System.out.println(e.getMessage());  
  }  
  }  
 void sorting()  
 {  
 int t,j;  
 for(j=0;j<n;j++)  
 {  
  for(i=0;i<n-1;i++)  
  {  
  if(a[i]>a[i+1])  
   {  
   t=a[i];  
   a[i]=a[i+1];  
   a[i+1]=t;  
   }  
  }  
  }  
  System.out.println("Elements in ascending order is:");  
  for(i=0;i<n;i++)  
  System.out.print(a[i]+" ");  
  System.out.println();  
  try  
  {  
  System.out.println("Enter the key element");  
  key=Integer.parseInt(get.readLine());  
  }  
  catch(Exception e)  
  {  
   System.out.println(e.getMessage());  
  }  
  }  
  void search()  
  {  
  int m,flag=0,l,u,p=0;  
  l=0;  
  u=n-1;  
  while(l<=u)  
  {  
   m=(l+u)/2;  
   if(a[m]==key)  
   {  
   flag=1;  
   p=m+1;  
   break;  
   }  
   else if(a[m]<key)  
   l=m+1;  
   else  
   u=m-1;  
  }  
  if(flag==0)  
   System.out.println("The number is not found");  
  else  
   System.out.println("The number is found in:"+p);  
  }  
 }   
 class binarysearch  
 {  
 public static void main(String arg[])  
 {  
  array obj=new array();  
  obj.getdata();  
  obj.sorting();  
  obj.search();  
  }  
 }  


Saturday, July 30, 2011

Various Sorting Techniques used to sort the Elements of an Array:Bubble Sort-Selection Sort -Insertion Sort-Quick Sort-Merge Sort-Heap Sort

Various Sorting techniques can be implemented to sort an Integer array .Some of them are
  • Bubble Sort
  • Selection Sort
  • Insertion Sort 
  • Quick Sort
  • Merge Sort
  • Heap Sort


Bubble Sort:

Bubble Sorting is the Most Basic Sorting technique.The various steps in technique

1. Compare the two  adjacent elements of the array
2. Check if one on the one on the left is large than one on the right, swap them.
3. Move to the right by one position.

The Code used for Implementing Bubble Sort is given Below
--------------------------------------------------------------

void sort() 
{
int x=l;
int y=0;
int a,b=0;
for (int rep=0;rep<l*4;rep++)
{
for(int i=0;i<x-1;i++)
{
a=ar[i];
b=ar[i+1];
if(a>b)
{

y=a;
a=b;
b=y;
ar[i]=a;
ar[i+1]=b;
}
}
}
}


----------------------------------------------------------------
Other Sorting Techniques will discussed in the future posts.

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