Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9deb794

Browse files
committed
Fix Fibonacci calculation input and add binary search implementation with sorting
1 parent 93dba2f commit 9deb794

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

‎Recursion/BS.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class BS {
2+
public static void main(String[] args) {
3+
int arr[] = {1,2,4,5,59,80,100};
4+
int target = 80;
5+
6+
System.out.println(binary_search(arr,0,arr.length-1,target));
7+
}
8+
9+
static int binary_search(int arr[], int left,int right,int target){
10+
if(left > right){
11+
return -1;
12+
}
13+
14+
int mid = (left+right)/2;
15+
16+
if(arr[mid] == target ) return mid;
17+
18+
if(arr[mid] > target){
19+
return binary_search(arr,left,mid-1,target);
20+
}
21+
22+
return binary_search(arr, mid+1 , right, target);
23+
24+
}
25+
}

‎Recursion/fibonacci.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
public class fibonacci {
33
public static void main(String[] args) {
4-
int result = fib(6);
4+
int result = fib(5);
55
System.out.println("Fibonacci of is: " + result);
66
// System.out.println(fib(5));
77
}

‎Searching/BS.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
import java.util.Arrays;
3+
4+
public class BS {
5+
public static void main(String[] args) {
6+
int arr[] = {4,26,1,3,87,20 };
7+
Arrays.sort(arr);
8+
System.out.println("Sorted Array: " + Arrays.toString(arr));
9+
int target = 26;
10+
int result = binary_search(arr, target);
11+
12+
if (result != -1) {
13+
System.out.println("Find the Element! , at index of : " + result);
14+
} else {
15+
System.out.println("Not found the Element ");
16+
}
17+
}
18+
19+
static int binary_search(int arr[], int target) {
20+
21+
int leftIndex = 0;
22+
int rightIndex = arr.length - 1;
23+
24+
while (leftIndex <= rightIndex) {
25+
int mid = (leftIndex + rightIndex) / 2;
26+
27+
if (arr[mid] == target) {
28+
return mid;
29+
} else if (arr[mid] > target) {
30+
rightIndex = mid - 1;
31+
32+
} else {
33+
leftIndex = mid + 1;
34+
35+
}
36+
}
37+
return -1;
38+
39+
}
40+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /