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

Java Productivity tools

Here are the list of Java Productivity tools that help in simplifying the daily life of a developer. Eclipse as an IDE simplifies the development life cycle which has a lots of plugins to support different programming languages and frameworks. Some of useful Eclipse plugins are - Sonarlint that helps to do static code analysis and give suggestions as we go along the development life cycle. JDGUI - Is the Eclipse Plugin that supports the decompilation of the Java application within eclipse, this is very handy when debugging and need to look at the out of the box code from a framework/library. JUnit is the unit testing framework that supports Unit Testing of Java Applications. Mockito is the framework that supports the Mock Unit Testing of the Java Application.

Core Java Interview Questions

1) Is Java Compiled or Interpreted languages? Java is both, first it is compiled by Java Compiler into ByteCode(which is a form of machine language just for the JVM). Then JVM interprets this byte code and converts it into machine understandable language. 2) How JVM Memory is organized? Which are stored in which part of JVM Memory? Classes and Methods are stored in Perm Gen space. String internalized are also stored in Perm Gen space. This space is very limited compared to the HeapSize. 3) Difference between PrintWriter and Buffered Writer PrintWriter is used for small and dirty applications where we can display/write the messages on to the console. More over PrintWriter swallows the exceptions, later can read the exceptions by calling checkError() method. 4) Difference between BufferedWriter vs FileWriter BufferedWriter stores the data in the buffer and won't make those many calls to the operating system, it also lets us to specify the size of the buffer, if the buffer size is s...