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

Design Patterns using Java

According to Gang of Four(GOF) any software is classified in to one of the three categories. I read so many books about design patterns which provide a lot of information about Design Patterns in a language neutral way or related to a particular programming language. I am trying to complement the great books by providing the precise and concise information that is required in the day to day programming of a Java Developer. Any software can be classified into one of the three categories -Framework, Toolkit, Application. Framework - Framework defines a set of steps to create an application. Framework provides design reuse. Toolkit - Toolkit provides some utility functions to an existing application. Toolkit provides code reuse. Application - Application is some thing that is specific to the project, and is not useful outside the context of the current application. Gang of Four divided the Design Patterns in to 3 types based on their usage. There are 3 types of Gang of Fo