Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

day 29 #210

Merged
merged 2 commits into from
Feb 8, 2019
Merged
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
45 changes: 45 additions & 0 deletions day29/Java/binarySearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication3;

/**
* @date 29/01/19
* @author SPREEHA DUTTA
*/
import java.util.*;
public class binarySearch {
public static int search(int []arr,int p,int l,int r)
{
int mid=0;
if(r>=l)
{
mid=(l+r)/2;
if(p==arr[mid])
return mid;
else if(p>arr[mid])
return search(arr,p,mid+1,r);
else
return search(arr,p,l,mid-1);
}
return -1;
}
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
int n,i;int p,f;
n=sc.nextInt();
int arr[]=new int[n];
for(i=0;i<n;i++)
arr[i]=sc.nextInt();
System.out.println("Enter element to be searched");
p=sc.nextInt();
f=search(arr,p,0,arr.length-1);
if(f==-1)
System.out.println("undefined");
else
System.out.println(f);
}
}
47 changes: 46 additions & 1 deletion day29/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,51 @@ console.log (binarySearch ([1, 2, 3, 4, 5, 8, 9], 8));
console.log (binarySearch ([1, 2, 3, 4, 5, 8, 9], 7));
```

## Java Implementation

### [Solution](./Java/binarySearch.java)

```java
/**
* @date 29/01/19
* @author SPREEHA DUTTA
*/
import java.util.*;
public class binarySearch {
public static int search(int []arr,int p,int l,int r)
{
int mid=0;
if(r>=l)
{
mid=(l+r)/2;
if(p==arr[mid])
return mid;
else if(p>arr[mid])
return search(arr,p,mid+1,r);
else
return search(arr,p,l,mid-1);
}
return -1;
}
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
int n,i;int p,f;
n=sc.nextInt();
int arr[]=new int[n];
for(i=0;i<n;i++)
arr[i]=sc.nextInt();
System.out.println("Enter element to be searched");
p=sc.nextInt();
f=search(arr,p,0,arr.length-1);
if(f==-1)
System.out.println("undefined");
else
System.out.println(f);
}
}
```

## C++ Implementation

### [Solution by @imkaka](./C++/binary_search.cpp)
Expand Down Expand Up @@ -106,4 +151,4 @@ int main(){
cout << "Recursive: " << binary_search_rec(arr, 0, size-1, val) << endl;
return 0;
}
```
```