Saturday, 18 June 2016

Program to prove that String Object is immutable or an example of String Pooling.

Code:

public class StringPooling
{
    public static void main( String args[])
    {
        String s1="a";
        String s2="a";
        System.out.println(s1==s2);//true
        System.out.println(s1.equals(s2));//true
       
        String s3=new String("a");
        String s4=new String("a");
        System.out.println(s3==s4);//false
        System.out.println(s3.equals(s4));//true
       
        String s5="a";
        String s6=new String("a");
        System.out.println(s5==s6);//false
        System.out.println(s5.equals(s6));//true
    }
}

Program to print the Words of a Given String ending with a Particular Letter.

Explanation :
If String= "All cars are supercars until they have tires".
1)If Letter = "s" then output will be "cars,supercars,tires".
2)If Letter = "l" then output will be "All,until".
3)If letter = "v" then output will be "There are no words ending with Letter v".

Code:

import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringTokenizer;
public class EndsWithLetter
{   
     public static void main(String args[])
     {
        int count=0;
        ArrayList<String> ar= new ArrayList<String>();
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter any String");
        String x=sc.nextLine();
        System.out.println("Enter a letter");
        String y=sc.nextLine();
        StringTokenizer st=new StringTokenizer(x,",/-@ ");
        while(st.hasMoreTokens())
        {
            String st1 = (String) st.nextToken();
            if(st1.endsWith(y))
            {
                ar.add(st1);//Adding Words in ArrayList
                count+=1;
            }
        }
        if(count!=0)
        {
                System.out.println("Words ending with letter "+y+" are:");
                for(String z:ar)
                {
                    System.out.println(z);
                }
        }
        else System.out.println("There are no words ending with Letter "+y);
        sc.close();
    }
}

Program to find the frequency of a SubString in a given String.

Explanation :
If String ="For each rose,a rose is a rose" and SubString ="rose" then,
output will be "rose has occured 3 times".

Code:

import java.util.Scanner;
import java.util.StringTokenizer;
public class SubStringCount
{
      public static void main(String args[])
     {
         int count=0;
         Scanner sc = new Scanner(System.in);
         System.out.println("Enter any String");
         String x=sc.nextLine();
         System.out.println("Enter Sub String");
         String y=sc.nextLine();
         StringTokenizer st=new StringTokenizer(x,", ");
        while(st.hasMoreTokens())
       {
             String st1 = (String) st.nextToken();
               if(st1.equals(y))
              {
                   count ++;
               }
        }
             System.out.println(y+" has occured "+ count+ " times in "+x);
             sc.close();
}
}

Program to perform Selection Sort.

Code:

public class MySelectionSort
{
    public static int[] doSelectionSort(int[] arr)
    {    
        for (int i = 0; i < arr.length - 1; i++)
        {
            int index = i;
            for (int j = i + 1; j < arr.length; j++)
                if (arr[j] < arr[index])
                    index = j;
     
            int smallerNumber = arr[index]; 
            arr[index] = arr[i];
            arr[i] = smallerNumber;
        }
        return arr;
    }
    
    public static void main(String a[])
    {        
        int[] arr1 = {10,34,2,56,7,67,88,42};
        int[] arr2 = doSelectionSort(arr1);
        for(int i:arr2)
        {
            System.out.println(i);
           
        }
    }
}

Program to perform Insertion Sort.

Code:

public class MyInsertionSort
{
    public static void main(String a[])
    {
        int[] arr1 = {10,34,2,56,7,67,88,42};
        int[] arr2 = doInsertionSort(arr1);
        for(int i:arr2)
        {
            System.out.print(i);
            System.out.print(", ");
        }
    }
    
    public static int[] doInsertionSort(int[] input)
    {    
        int temp;
        for (int i = 1; i < input.length; i++)
        {
            for(int j = i ; j > 0 ; j--)
            {
                if(input[j] < input[j-1])
                {
                    temp = input[j];
                    input[j] = input[j-1];
                    input[j-1] = temp;
                }
            }
        }
        return input;
    }
}

Program to perform Quick Sort.

Code:

public class QuickSort
{    
    private int array[];
    private int length;
    public void sort(int[] inputArr)
    {        
        if (inputArr == null || inputArr.length == 0)
        {
            return;
        }
        this.array = inputArr;
        length = inputArr.length;
        quickSort(0, length - 1);
    }

    private void quickSort(int lowerIndex, int higherIndex)
    {    
        int i = lowerIndex;
        int j = higherIndex;
        // calculate pivot number, I am taking pivot as middle index number
        int pivot = array[lowerIndex+(higherIndex-lowerIndex)/2];
        // Divide into two arrays
        while (i <= j)
        {        
            while (array[i] < pivot)
            {
                i++;
            }
            while (array[j] > pivot)
            {
                j--;
            }
            if (i <= j)
            {
                exchangeNumbers(i, j);
                //move index to next position on both sides
                i++;
                j--;
            }
        }
        // call quickSort() method recursively
        if (lowerIndex < j)
            quickSort(lowerIndex, j);
        if (i < higherIndex)
            quickSort(i, higherIndex);
    }

    private void exchangeNumbers(int i, int j)
    {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
    
    public static void main(String a[])
    {        
        QuickSort sorter = new QuickSort();
        int[] input = {24,2,45,20,56,75,2,56,99,53,12};
        sorter.sort(input);
        for(int i:input)
        {
            System.out.print(i);
            System.out.print(" ");
        }
    }
}

Program to perform Merge Sort.

Code:

public class MergeSort
{    
    private int[] array;
    private int[] tempMergArr;
    private int length;
    public static void main(String args[])
    {        
        int[] inputArr = {45,23,11,89,77,98,4,28,65,43};
        MergeSort mms = new MergeSort();
        mms.sort(inputArr);
        for(int i:inputArr)
        {
            System.out.print(i);
            System.out.print(" ");
        }
    }
    
    public void sort(int inputArr[])
    {
        this.array = inputArr;
        this.length = inputArr.length;
        this.tempMergArr = new int[length];
        doMergeSort(0, length - 1);
    }

    private void doMergeSort(int lowerIndex, int higherIndex)
    {        
        if (lowerIndex < higherIndex)
        {
            int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
            // Below step sorts the left side of the array
            doMergeSort(lowerIndex, middle);
            // Below step sorts the right side of the array
            doMergeSort(middle + 1, higherIndex);
            // Now merge both sides
            mergeParts(lowerIndex, middle, higherIndex);
        }
    }

    private void mergeParts(int lowerIndex, int middle, int higherIndex)
    {
        for (int i = lowerIndex; i <= higherIndex; i++)
        {
            tempMergArr[i] = array[i];
        }
        int i = lowerIndex;
        int j = middle + 1;
        int k = lowerIndex;
        while (i <= middle && j <= higherIndex)
        {
            if (tempMergArr[i] <= tempMergArr[j])
            {
                array[k] = tempMergArr[i];
                i++;
            }
            else
            {
                array[k] = tempMergArr[j];
                j++;
            }
            k++;
        }
        while (i <= middle)
        {
            array[k] = tempMergArr[i];
            k++;
            i++;
        }
     }
}

Program to perform Bubble Sort.

Code:

public class Bubble
{
   public static void main(String args[])
   {         
            int[] a = {4, 85, 7, 1, 0, 36, -5, 48};
           for (int i = 0; i < a.length - 1; i++)
           {
                   for (int j = 0; j < a.length - 1 - i; j++)
                  {
                        if (a[j + 1] < a[j])
                       {
                             int temp = a[j];
                             a[j] = a[j + 1];
                             a[j + 1] = temp;
                       }
                  }
             }
            for (int i = 0; i < a.length - 1; i++)
           {
               System.out.println(a[i]);
           }
     }
}

Program to perform Binary Search.

Code:

public class binary
{
   public static void main(String args[])
   {         int[] a = {3, 7, 10, 15, 91, 110, 150};
         int target = 91; // the element to be searched
         int left = 0;
         int middle;
         int right = a.length - 1;
         while (left < = right)
        {
              middle = (left + right) / 2;
              if (a[middle] == target)
            {
                 System.out.println("Element found at index " + middle);
                 break;
             }
             else if (a[middle] < target)
            {
                 left = middle + 1;
             }
             else if (a[middle] > target)
            {
                right = middle - 1;
             }
        }
  }
}

Program to perform Linear Search.

Code:

class linear
{
   public static void main(String args[])
   {
        int[] a = { 3, 34, 5,91, 100};
        int target = 91,flag=0;
        for( int i=0; i<a.length; i++)
       {
               if(a[i] == target)
              {
                   flag=1;
                   System.out.println ( "Element found at index "+i);
                   break;
              }
       }
       if(flag==0)  System.out.println ( "Element not found");
   }
}