diff --git a/JAVA/BinarySearch.java b/JAVA/BinarySearch.java new file mode 100644 index 0000000..4216ade --- /dev/null +++ b/JAVA/BinarySearch.java @@ -0,0 +1,62 @@ +public class BinarySearch { +//Recursive binary search + public static int binarySearch(int[] arr, int target) { + return binarySearch(arr, target, 0, arr.length - 1); + } + + // Helper function for recursive binary search + private static int binarySearch(int[] arr, int target, int left, int right) { + if (left > right) { + return -1; // Target not found + } + + int mid = left + (right - left) / 2; + + if (arr[mid] == target) { + return mid; // Target found at index 'mid' + } else if (arr[mid] < target) { + return binarySearch(arr, target, mid + 1, right); // Search the right half + } else { + return binarySearch(arr, target, left, mid - 1); // Search the left half + } + } + + // Iterative binary search + public static int binarySearchIterative(int[] arr, int target) { + int left = 0; + int right = arr.length - 1; + + while (left <= right) { + int mid = left + (right - left) / 2; + + if (arr[mid] == target) { + return mid; // Target found at index 'mid' + } else if (arr[mid] < target) { + left = mid + 1; // Search the right half + } else { + right = mid - 1; // Search the left half + } + } + + return -1; // Target not found + } + + public static void main(String[] args) { + int[] sortedArray = {1, 3, 5, 7, 9, 11, 13, 15, 17}; + int target = 9; + + int result = binarySearch(sortedArray, target); + if (result != -1) { + System.out.println("Target " + target + " found at index " + result); + } else { + System.out.println("Target " + target + " not found in the array."); + } + + int resultIterative = binarySearchIterative(sortedArray, target); + if (resultIterative != -1) { + System.out.println("Iterative Binary Search: Target " + target + " found at index " + resultIterative); + } else { + System.out.println("Iterative Binary Search: Target " + target + " not found in the array."); + } + } +} diff --git a/JAVA/Bubblesort.java b/JAVA/Bubblesort.java new file mode 100644 index 0000000..f0ab4cd --- /dev/null +++ b/JAVA/Bubblesort.java @@ -0,0 +1,44 @@ +public class BubbleSort { + public static void bubbleSort(int[] arr) { + int n = arr.length; + boolean swapped; + + for (int i = 0; i < n - 1; i++) { + swapped = false; + + for (int j = 0; j < n - i - 1; j++) { + if (arr[j] > arr[j + 1]) { + // Swap arr[j] and arr[j + 1] + int temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + swapped = true; + } + } + + // If no two elements were swapped in the inner loop, the array is already sorted + if (!swapped) { + break; + } + } + } + + public static void main(String[] args) { + int[] arr = {64, 34, 25, 12, 22, 11, 90}; + + System.out.println("Original array:"); + printArray(arr); + + bubbleSort(arr); + + System.out.println("\nSorted array:"); + printArray(arr); + } + + public static void printArray(int[] arr) { + for (int i = 0; i < arr.length; i++) { + System.out.print(arr[i] + " "); + } + System.out.println(); + } +}