Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>BinarySearch1</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
8 changes: 8 additions & 0 deletions src/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
*
*/
/**
*
*/
module BinarySearch1 {
}
40 changes: 40 additions & 0 deletions src/sree/BinarySearch1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package sree;

class BinarySearch1 {
public boolean searchMatrix(int[][] matrix, int target) {
int m= matrix.length;
int n= matrix[0].length;

if (target < matrix[0][0] || target > matrix[m - 1][n - 1]) return false;
int low=0;
int high= m-1;
while(low<high){
int mid= low + (high-low)/2;
if(matrix[mid][n-1]== target){
return true;
}
else if(matrix[mid][n-1]< target){
low=mid+1;
}
else{
high=mid;
}
}
int row=low;
low=0;
high=n-1;
while(low<=high){
int mid=low + (high-low)/2;
if(matrix[row][mid]== target){
return true;
}
else if(matrix[row][mid]< target){
low= mid+1;
}
else{
high= mid-1;
}
}
return false;
}
}
34 changes: 34 additions & 0 deletions src/sree/BinarySearch2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package sree;

class BinarySearch2 {
public int search(int[] nums, int target) {
int low=0;
int high= nums.length-1;
while(low<=high){
int mid= low + (high-low)/2;
if(nums[mid]== target){
return mid;
}
if(nums[low]<=nums[mid]){
if(nums[low]<=target && nums[mid]>target){
high= mid-1;
}
else{
low= mid+1;
}
}
else
{
if(nums[high]>=target && nums[mid]<target)
{
low= mid+1;
}
else
{
high=mid-1;
}
}
}
return -1;
}
}
27 changes: 27 additions & 0 deletions src/sree/BinarySearch3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package sree;

//Logic for searching in an infinitely sorted array has been using binary search.
//Here we increase the search space by twice. and then do binary search in the required window.
//O(log n)
class Solution {
public int search( int arr[], int target) {
int low = 0, high = 1;

while(arr[high] < target){
low = high ;
high = high * 2;
}

while(low <= high){
int mid = low + (high - low)/2;
if(arr[mid] == target) return mid;
if(arr[mid] > target){
high = mid - 1;
}else{
low = mid + 1;
}
}

return -1;
}
}