diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..15cec4b --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..8872ad7 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..797acea --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/BinarySearch.java b/BinarySearch.java new file mode 100644 index 0000000..d414a1c --- /dev/null +++ b/BinarySearch.java @@ -0,0 +1,23 @@ + +// This is the code for binary search + +public class BinarySearch { + static int binarySearch(int[] arr, int target){ // requires an array and a target + + int start = 0; + int end = arr.length-1; + + while (start <= end){ + int mid = start + (end - start) / 2; + + // if the middle element of the array is the target element it gets returned + if (arr[mid] == target) return mid; + + // if the middle element is not the target element then the following code is executed + else if(arr[mid] > target) end = mid - 1; + else start = mid + 1; + } + // if the target element is not found it the array it returns 0 + return 0; + } +} diff --git a/Java-Codes.iml b/Java-Codes.iml new file mode 100644 index 0000000..deb07dc --- /dev/null +++ b/Java-Codes.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file