Skip to content

Commit 44c07c6

Browse files
committed
Added recursive code for Binary search and Fibo
1 parent d032ab9 commit 44c07c6

File tree

5 files changed

+38
-1
lines changed

5 files changed

+38
-1
lines changed

15 - Recursion/BS.class

716 Bytes
Binary file not shown.

15 - Recursion/BS.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java.util.Arrays;
2+
import java.util.Scanner;
3+
import java.util.ArrayList;
4+
public class BS
5+
{
6+
public static void main(String[] args){
7+
Scanner in = new Scanner(System.in);
8+
int[] arr = {1, 2, 34, 38, 65, 133, 155, 175};
9+
System.out.print(bs(arr,0,arr.length-1, 155));
10+
}
11+
12+
static int bs(int[] arr, int s, int e, int n){
13+
if(s > e) return -1;
14+
int mid = s + (e-s)/2;
15+
if(n == arr[mid]) return mid;
16+
else if(n > arr[mid]) return bs(arr, mid+1, e, n);
17+
else return bs(arr, s, mid-1, n);
18+
19+
}
20+
}

15 - Recursion/Fibo.class

610 Bytes
Binary file not shown.

15 - Recursion/Fibo.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import java.util.Arrays;
2+
import java.util.Scanner;
3+
import java.util.ArrayList;
4+
public class Fibo
5+
{
6+
public static void main(String[] args){
7+
Scanner in = new Scanner(System.in);
8+
System.out.print(fibo(6));
9+
}
10+
11+
static int fibo(int n){
12+
if(n < 2){
13+
return n;
14+
}
15+
return fibo(n-1) + fibo(n-2);
16+
}
17+
}

15 - Recursion/NumberExampleRecursion.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ static void print(int n){
1313
System.out.println(n);
1414
if(n == 5) {
1515
return;
16-
}
16+
}
1717
print(n+1);
1818
}
1919
}

0 commit comments

Comments
 (0)