Sunday, 17 September 2017

Counting Sort using Java...

Here is the code .....


       
public class CountingSort {

 public static void main(String[] args) {

  // Array of items on which search will
  // be conducted.
  int arr[] = new int[] { 10, 12, 13, 16, 18, 19, 20, 21, 22, 23, 24, 33, 35, 42, 47 };
  int maxElemnt = findMaxElement(arr);
  arr = countSort(arr, maxElemnt);

  System.out.println("Below is the sorted array:");
  for (int s : arr)
   System.out.print(s + " ");
 }

 // Finding Max Element
 public static int findMaxElement(int[] arr) {

  int i = 0;
  int temp;
  while (i < arr.length - 1) {
   if (arr[i] > arr[i + 1]) {
    temp = arr[i + 1];
    arr[i + 1] = arr[i];
    arr[i] = temp;
   }
   i++;
  }

  return arr[arr.length - 1];
 }

 public static int[] countSort(int[] arr, int range) {

  int[] countArray = new int[range + 1];
  int[] outputArray = new int[arr.length];

  int i = 0;
  while (i < arr.length) {
   countArray[arr[i]] = ++countArray[arr[i]];
   i++;
  }
  i = 1;
  while (i < countArray.length) {
   countArray[i] = countArray[i] + countArray[i - 1];
   i++;
  }

  i = 0;
  while (i < outputArray.length) {
   outputArray[countArray[arr[i]] - 1] = arr[i];
   countArray[arr[i]] = --countArray[arr[i]];
   i++;
  }

  return outputArray;
 }
}

Sunday, 10 September 2017

Exponential Search using Java....

Here is the code ....

public class ExponentialSearch {

 public static void main(String[] args) {

  // Array of items on which search will
  // be conducted.
  int arr[] = new int[] { 10, 12, 13, 16, 18, 19, 20, 21, 22, 23, 24, 33, 35, 42, 47 };
  int valueToSearch = 24;
  int i = 1;

  if (arr[0] == valueToSearch) {
   System.out.println("ELement found at location");
   return;
  }

  while (i < arr.length && arr[i] <= valueToSearch) {
   i = i * 2;

   int loc = binarySearrch(arr, valueToSearch, i / 2, min(i, arr.length - 1));
   if (loc != -1) {
    System.out.println("Element found at location : " + loc);
    break;
   }
  }
 }

 public static int binarySearrch(int[] arr, int valueToSearch, int startIndex, int lastIndex) {

  if (lastIndex == startIndex) {
   if (arr[lastIndex] == valueToSearch)
    return lastIndex;
   else
    return -1;
  } else {

   int mid = (startIndex + lastIndex) / 2;
   if (arr[mid] == valueToSearch)
    return mid;
   else if (arr[mid] > valueToSearch)
    return binarySearrch(arr, valueToSearch, startIndex, mid - 1);
   else
    return binarySearrch(arr, valueToSearch, mid + 1, lastIndex);
  }
 }

 public static int min(int i, int j) {
  if (i < j)
   return i;
  else
   return j;
 }
}


       
 

Interpolation Search using Java...

Here is the code ....

public class InterpolationSearch {

 public static void main(String[] args) {

  // Array of items on which search will
  // be conducted.
  int arr[] = new int[] { 10, 12, 13, 16, 18, 19, 20, 21, 22, 23, 24, 33, 35, 42, 47 };
  int valueToSearch = 33;
  int loc = searchElement(arr, valueToSearch, 0, arr.length - 1);

  if (loc != -1)
   System.out.print("Element appears at location : " + loc);
  else
   System.out.print("ELement not found");
 }

 // Recursive search : Interpolation search
 public static int searchElement(int[] arr, int valueTosearch, int low, int high) {

  if (high == low) {
   if (arr[high] == valueTosearch)
    return high;
   else
    return -1;
  } else {

   int pos = low + ((valueTosearch - arr[low]) * (high - low)) / (arr[high] - arr[low]);
   if (arr[pos] == valueTosearch)
    return pos;
   else if (arr[pos] > valueTosearch)
    return searchElement(arr, valueTosearch, low, pos - 1);
   else
    return searchElement(arr, valueTosearch, pos + 1, high);
  }
 }
}


       
 

Jump Search using Java....

Jump Search - Jump Search is a searching algorithm for sorted arrays. The basic idea is to check fewer elements (than linear search) by jumping ahead by fixed steps or skipping some elements in place of searching all elements.

Input - A sorted array.

Here is the code .....
       
public class JumpSearch {

 public static void main(String[] args) {

  int[] arr = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610 };
  int lastJumpedIndex = 0;

  // Evaluate optimal block size
  int t = (int) Math.floor(Math.sqrt(arr.length));
  int valueToSearch = 21;

  while (t < arr.length) {

   if (arr[t] > valueToSearch) {

    if (valueToSearch >= arr[lastJumpedIndex]) {

     // Doing a linear search for x in block
     int i = lastJumpedIndex;
     while (i < t) {
      if (arr[i] == valueToSearch) {
       System.out.print("Element found at index : " + i);
       return;
      }
      i++;
     }
    }
   }
   lastJumpedIndex = t;
   t = 2 * t;
  }

  // In case few of the elements in the last iteration of above loop have
  // left if array is not of an even length
  if (t > arr.length - 1) {
   int k = lastJumpedIndex;
   while (k <= arr.length - 1) {
    if (arr[k] == valueToSearch) {
     System.out.print("Element found at index : " + k);
     return;
    }
    k++;
   }
  }
  System.out.print("Element was not found");
 }

 public static int min(int i, int j) {
  if (i < j)
   return i;
  else
   return j;
 }
}