Skip to main content

Searching Algorithms

1) Binary Search Iterative Approach


/**

 *
 */
package com.test.search;

/**
 * @author Kiran
 *
 */
public class BinarySearchIterative {



/**
* @param args
*/
public static void main(String[] args) {
BinarySearchIterative binarySearch = new BinarySearchIterative();
int[] elements = new int[]{3,4,5,6,7,8,9,10,11,12,13,14,15,16};
int searchElement = 11  ;
int start = 0;
int end = 0;

if(elements!=null) {
end =  (elements.length);
}
int elementIndex =
binarySearch.doBinarySearchIterative(elements, searchElement, start, end);
if(elementIndex!=-1) {
System.out.println(" Found the searching element"
+ " at the Index of "
+ elementIndex);
} else {
System.out.println(" The element is Not"
+ "  found in the Array ");
}
}


/**
*
* @param elements
* @param searchElement
* @param startIndex
* @param endIndex
* @return
*/
private int doBinarySearchIterative(int[] elements,int searchElement,
int startIndex,int endIndex) {
int elementIndex = -1;
while(startIndex<=endIndex) {
int middleIndex = (startIndex+endIndex)/2;
if(elements[middleIndex]==searchElement) {
elementIndex = middleIndex;
return elementIndex;
} else if(searchElement<elements[middleIndex]) {
endIndex = middleIndex-1;
} else if(searchElement>elements[middleIndex]) {
startIndex = middleIndex+1;
} else {
System.out.println(" The element is"
+ " Not found in the Array ");
elementIndex = -1;
}
}
return elementIndex;
}
}


2) Binary Search Recursive Approach


/**

 *
 */
package com.test.search;

/**
 * @author Kiran
 *
 */
public class BinarySearchRecursive {

/**
* @param args
*/
public static void main(String[] args) {
BinarySearchRecursive binarySearch = new BinarySearchRecursive();
int[] elements = new int[]{3,4,5,6,7,8,9,10,11,12,13,14,15,16};
int searchElement = 1  ;
int start = 0;
int end = 0;

if(elements!=null) {
end =  (elements.length);
}
int elementIndex =
binarySearch.doBinarySearch(elements, searchElement, start, end);
if(elementIndex!=-1) {
System.out.println(" Found the searching element"
+ " at the Index of "
+ elementIndex);
} else {
System.out.println(" The element is Not"
+ "  found in the Array ");
}
}


/**
*
* @return
*/
private int doBinarySearch(int elements[],int searchElement,int start,int end)  {
int elementIndex = 0;
int middle = (start+end)/2;
System.out.println(" Middle started at  "+ middle+" start "+start+" end "+end);
if(searchElement==elements[middle]) {
elementIndex = middle;
return elementIndex;
} else {
if(searchElement<middle) {
elementIndex = doBinarySearch(elements, searchElement, start, middle-1);
} else if(searchElement>middle) {
elementIndex = doBinarySearch(elements, searchElement, middle+1, end);
} else {
System.out.println(" The element is "
+ " Not found in the Array ");
elementIndex = -1;
}
}
return elementIndex;
}

}

Comments

Popular posts from this blog

HashMap in Java

1) Implement HashMap in Java, with the put and get operations   HashMap can be implemented in Java Using Arrays. Use the same logic that the Out of the Box   HashMap follows, for resizing, and load factor, when ever the HashMap reaches the size of the   resize with the load factor a new Array is created, and the previous array contents are copied over   to the new Array.  HashMap is Not Synchronized by default. We can synchronize the whole map by using Synchronization, or by using collection.synchronizedmap(map), which synchronizes all the operations on the map. Alternatively We can use the CocurrentHashMap which does not lock the read operations, rather locks the segments that are being written. 2) HashMap vs LinkedHashMap vs IdentityHashMap 3) HashMap vs ConcurrentHashMap 4) Implement a Cache using LinkedHashMap

Data Structures - Strings

1) Find if a given First String is a SubSequence of a Second String 2) Find Max Occurring Frequency Character in  a String 3) Check if Sequence of Characters of Two Strings can become same by removal of a Character 4) Remove all Duplicate Words from a String 5) Find for a Given Two Strings, if one is the rotation of the Other. 6) Find if two Strings are K-Anagrams 7) Longest Common Subsequence where character occurs at least k times. 8) Find if the two given Strings are anagrams of Each Other. 9) Anagram SubString Search