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

Data Structures using Java

package com.test.count; /**   *     * @author Kiran   *   */ public class LinkedList { Node headNode ; /** *   This method appends the Node at the end of the List *   * @param data */ public void appendNode( int data ) { Node currentNode = headNode ; if ( currentNode == null ) { Node newNode = new Node( data ); headNode = newNode ; return ; } while ( currentNode .getNextNode()!= null ) { currentNode = currentNode .getNextNode(); } currentNode .setNextNode( new Node( data )); } /** * This method prints all the Nodes/Values of the   * LinkedList *   */ public void printNodes() { Node currentNode = headNode ; if ( currentNode == null ) { System. out .println( " The Node is Null" ); return ; } else { System. out .print( "   " + currentNode .getData()); while ( currentNo...

Gang of Four Design Patterns

Gang of Four(GOF) Design Patterns are divided in to 3 categories. Creational Design Patterns ( 5 Patterns ) AbstractFactory Builder Method Factory Method Prototype Singleton Singleton pattern is used when there is only one instance of the object need to be created per JVM instance. This pattern is used internally by the Connection Pooling to create the objects only on demand. Structural Design Patterns ( 7 Patterns ) Adapter Bridge Pattern Composite Pattern Decorator Pattern Facade FlyWeight FlyWeight Pattern is used when there are a large number of objects to be created, and they need to be created efficiently. This pattern is used by many frame works to create the objects using configuration. Proxy Behavioral Design Patterns ( 11 Patterns ) Chain Of Responsibility Pattern Command Interpreter Iterator Mediator Memento Memento is used internally by Text Editor Applications, where ctr+Z or cmd+Z operations are used to remember the snap shot of the data. State State Design Pattern is use...