Binary Search


package com.multithreding.producerconsumer;
public class BinarySearch {
public static void main(String[] args) {
int[] array = {10,39,40,123};
int k = 39;
System.out.println(findElement(array, k));
}

public static int findElement(int[] array, int k){
int length = array.length;

int startIndex = 0;
int endIndex = length-1;
while (startIndex <= endIndex) {
int mid = (startIndex + endIndex)/2;
if(array[mid] == k){
return mid;
}

if(k < array[mid]){
endIndex = mid - 1;
}else{
startIndex = mid + 1;
}
}
return -1;
}
}

This entry was posted in JAVA, Logical Problems and tagged on by .
Rahul Thakur

About Rahul Thakur

Hi, this is Rahul working as Software Engineer at Orange Business Services. Responsible for Requirement analysis, design ,development, deployment, and testing. Working on Agile methodology and Scrum Experience on UML, OOAD, OOPS, Design patterns, Java / J2ee technologies, ORM, performance and load tests, code review and lot of SDLC activities.