From a43099d1642b3de0e0783a9c0788cef069e1adde Mon Sep 17 00:00:00 2001 From: Yash Handa Date: Fri, 15 Oct 2021 01:03:34 +0530 Subject: [PATCH 1/2] binary search code --- binarysearch.java | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 binarysearch.java diff --git a/binarysearch.java b/binarysearch.java new file mode 100644 index 0000000..3784d02 --- /dev/null +++ b/binarysearch.java @@ -0,0 +1,30 @@ +class Binarysearch { + int binarySearch(int arr[], int l, int r, int x) + { + if (r >= l) { + int mid = l + (r - l) / 2; + + if (arr[mid] == x) + return mid; + + if (arr[mid] > x) + return binarySearch(arr, l, mid - 1, x); + + return binarySearch(arr, mid + 1, r, x); + } + + return -1; + } + public static void main(String args[]) + { + BinarySearch ob = new BinarySearch(); + int arr[] = { 2, 3, 4, 10, 40 }; + int n = arr.length; + int x = 10; + int result = ob.binarySearch(arr, 0, n - 1, x); + if (result == -1) + System.out.println("Element not present"); + else + System.out.println("Element found at index " + result); + } +} \ No newline at end of file From 3d121680901412a7d3950e71c4ceb04065d4ef2d Mon Sep 17 00:00:00 2001 From: Yash Handa Date: Fri, 15 Oct 2021 01:08:14 +0530 Subject: [PATCH 2/2] bubble sort --- bubblesort.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 bubblesort.java diff --git a/bubblesort.java b/bubblesort.java new file mode 100644 index 0000000..7a5e381 --- /dev/null +++ b/bubblesort.java @@ -0,0 +1,14 @@ + static void bubbleSort(int[] arr) { + int n = arr.length; + int temp = 0; + for(int i=0; i < n; i++){ + for(int j=1; j < (n-i); j++){ + if(arr[j-1] > arr[j]){ + //swap elements + temp = arr[j-1]; + arr[j-1] = arr[j]; + arr[j] = temp; + } + } + } + } \ No newline at end of file