Saturday, November 1, 2014

Java implement pro



Write a program to implement linear search or Sequential search algorithm
package com.java2novice.algos;
public class MyLinearSearch
{
public static int linerSearch(int[] arr, int key)
{
        int size = arr.length;
        for(int i=0;i
        {
            if(arr[i] == key)
            {
                return i;
            }
        }
        return -1;
    } 
public static void main(String a[])
{
        int[] arr1= {23,45,21,55,234,1,34,90};
        int searchKey = 34;
        System.out.println("Key "+searchKey+" found at index: "+linerSearch(arr1, searchKey));
        int[] arr2= {123,445,421,595,2134,41,304,190};
        searchKey = 421;
        System.out.println("Key "+searchKey+" found at index: "+linerSearch(arr2, searchKey));
    }
}

Implement Binary search in java using divide and conquer technique

package com.java2novice.algos;
public class MyBinarySearch
{
public int binarySearch(int[] inputArr, int key)
{
        int start = 0;
        int end = inputArr.length - 1;
        while (start <= end) {
            int mid = (start + end) / 2;
            if (key == inputArr[mid])
           {
                return mid;
            }
            if (key < inputArr[mid])
           {
                end = mid - 1;
            }
            else
         {
                start = mid + 1;
            }
        }
        return -1;
    }
  public static void main(String[] args)
  {
        MyBinarySearch mbs = new MyBinarySearch();
        int[] arr = {2, 4, 6, 8, 10, 12, 14, 16};
        System.out.println("Key 14's position: "+mbs.binarySearch(arr, 14));
        int[] arr1 = {6,34,78,123,432,900};
        System.out.println("Key 432's position: "+mbs.binarySearch(arr1, 432));
    }
}

Implement Binary search in java using recursive algorithm

package com.java2novice.algos;
public class MyRecursiveBinarySearch
{
public static int recursiveBinarySearch(int[] sortedArray, int start, int end, int key)
{
           if (start < end)
            {
            int mid = start + (end - start) / 2; 
            if (key < sortedArray[mid])
           {
                return recursiveBinarySearch(sortedArray, start, mid, key);
                }
                else if (key > sortedArray[mid])
                {
                return recursiveBinarySearch(sortedArray, mid+1, end , key);
                }
                else
               {
                return mid;  
            }
        }
        return -(start + 1); 
    }
    public static void main(String[] args)
    {
        int[] arr1 = {2,45,234,567,876,900,976,999};
        int index = recursiveBinarySearch(arr1,0,arr1.length,45);
        System.out.println("Found 45 at "+index+" index");
        index = recursiveBinarySearch(arr1,0,arr1.length,999);
        System.out.println("Found 999 at "+index+" index");
        index = recursiveBinarySearch(arr1,0,arr1.length,876);
        System.out.println("Found 876 at "+index+" index");
    }
}

Implement bubble sort in java

package com.java2novice.algos;
public class MyBubbleSort
{
public static void bubble_srt(int array[])
{
        int n = array.length;
        int k;
        for (int m = n; m >= 0; m--)
        {
            for (int i = 0; i < n - 1; i++)
            {
                k = i + 1;
                if (array[i] > array[k])
               {
                    swapNumbers(i, k, array);
                }
            }
            printNumbers(array);
        }
    }
 private static void swapNumbers(int i, int j, int[] array)
{
       int temp;
        temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
  private static void printNumbers(int[] input)
  {
        for (int i = 0; i < input.length; i++)
       {
            System.out.print(input[i] + ", ");
        }
        System.out.println("\n");
    }
  public static void main(String[] args)
  {
        int[] input = { 4, 2, 9, 6, 23, 12, 34, 0, 1 };
        bubble_srt(input);
  
    }
}

No comments:

Post a Comment