Tuesday, April 16, 2013

Array in Ascending order

      Array is a collection of homogeneous fixed size of elements.Sorting of array is a Frequently asked question in interview. Sort the Array by using Swap of two numbers Technic.

The following is Sorting of Array in ascending order:

import java. io.*;
import java.lang.*;
class ArraySort
{
  public static void main(String[] args) 
   {
      int a[]={5,2,1,3,8,9,6,4};
     ArraySort as=new ArraySort();
     as.sArray(a);
    }
  public void sArray(int[] a)
 {
   int b=a.length;
   for(int i=0;i<b;i++)
   {
      for(int j=0;j<i;j++)
      {
         if(a[i]<a[j])
         {
            int temp=a[i];
            a[i]=a[j];
            a[j]=temp;
          }
      }
    }
   for(int i=0;i<=b;i++)
   {
     System.out.println(a[i]);
   }
  }
}


Output:
1
2
3
4
5
6
8
9


Monday, April 15, 2013

Bucket Sort

                Bucket Sort is also called as Bin Sort.Bucket sort is a sorting algorithm,it works like partitioning an array into a number of Buckets.Each Bucket is sorted individually.Bucket sort run in linear time on the average.
                The idea of bucket sort is to divide the intervel [0,1) into n equal sized subintervals, or buckets and then distributes the n inputs numbers into the buckets.After the inputs are uniformly distributed over(0,1),we donot expect  many numbers fall into each bucket.
                To produce the output,simply sort the numbers in each bucket and puts bucket in order ,listing the elements in each.
                The worst case performance of Bucket Sort is O(n2) and average case performance is O(n+k).

The following program on  Bucket Sort:

import java.util.*;
public class bucket 
{
public static void main(String[] args) 
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a positive integer as range.");
int n = sc.nextInt();

Random rnd = new Random();
                                      int[] array = new int[100];
                                     //taking input as randomly from console into array
                                      for(int i = 0; i<100; i++)
                                     {
                                           array[i] = rnd.nextInt(n) +1;
            
                                     }
                                     // displaying array elements 
                                     for(int j = 0; j < 100; j++)
                                    {
                                        System.out.print(array[j] + ",");
                    
                                    }
                                    System.out.println("");
                                  //calling bucketsort() method
                                    bucketSort(array, n);
                     }

//bucket sort method
public static void bucketSort(int [] list, int range)
{
int[] countarray = new int[range];
int[] sorted = new int[list.length];
for(int k = 0; k < list.length; k++)
{
countarray[list[k]-1] = countarray[list[k]-1] + 1;
}
for(int i=0; i < countarray.length-1; i++)
{
int count = countarray[i];
int h = i;
while (count != 0)
{
sorted[i] = i+1;
count--;
h++;
}
}
for(int i=0; i<sorted.length; i++)
{
System.out.print(sorted[i] + ",");
}
}

}

Wednesday, April 3, 2013

Selection Sort

               We can sort the array elements by using Selection Sort.We perform a search in array, staring from the first record,to locate the element with the smallest key.When this record is found, we interchange it with the first record in the table.As a result of this interchange,the smallest element is placed in the first position of the table.In the second iteration,we locate second smallest element examining the elements of the table starting from the second position onward  We then interchange this element with the second element in the table.We continue this proper of searching for the smallest element in the unsorted position of the table and placing it in its proper place until we have placed all the records in the proper order.The following example shows the above process: 

The following programme is the implementation of Selection Sort:

import java.io.*;
class SSort
{
  public static void main(String[] args)
   {
 
     int temp;
     int s=8;
     int[] a={45,25,75,15,65,55,95,35};
 
      for(int i=0;i<s;i++)
       {
         System.out.println("After sorting is  "+(i+1)+": ");
         for(int k=0;k<s;k++)
           System.out.println(a[k]);
            for(int j=i+1;j<s;j++
            {
              if(a[i]>a[j]) 
              {
               temp=a[i];
              a[i]=a[j];
              a[j]=temp;
              }
           }
      }
  }
}

Output:

 After Sorting is 1: 45 25 75 15 65 55 95 35
 After Sorting is 2: 15 25 75 45 65 55 95 35
 After Sorting is 3: 15 25 75 45 65 55 95 35
 After Sorting is 4: 15 25 35 45 65 55 95 75
 After Sorting is 5: 15 25 35 45 65 55 95 75
 After Sorting is 6: 15 25 35 45 55 65 95 75
 After Sorting is 7: 15 25 35 45 55 65 95 75
 After Sorting is 8: 15 25 35 45 55 65 75 95