diff --git a/Functional/LambdaExpressionUtils.java b/Functional/LambdaExpressionUtils.java new file mode 100644 index 000000000000..5a28f7300c32 --- /dev/null +++ b/Functional/LambdaExpressionUtils.java @@ -0,0 +1,37 @@ +/** + * LambdaExpressionUtils.java + * Unique Java utility functions using Lambda expressions. + * Demonstrates Function, Predicate, Consumer, and Supplier. + */ + +import java.util.Random; +import java.util.function.*; + +public class LambdaExpressionUtils { + + public static void main(String[] args) { + // Reverse a string + Function reverse = str -> new StringBuilder(str).reverse().toString(); + System.out.println("Reversed: " + reverse.apply("lambda")); + + // Check palindrome + Predicate isPalindrome = str -> str.equalsIgnoreCase(new StringBuilder(str).reverse().toString()); + System.out.println("Is Palindrome: " + isPalindrome.test("madam")); + + // Print message in all caps with exclamation + Consumer shout = s -> System.out.println(s.toUpperCase() + "!"); + shout.accept("functional interface"); + + // Check if number is even + Function isEven = n -> n % 2 == 0; + System.out.println("Is 10 even? " + isEven.apply(10)); + + // Get random greeting + Supplier randomGreeting = () -> { + String[] greetings = {"Hello", "Hi", "Hey", "Hola"}; + return greetings[new Random().nextInt(greetings.length)]; + }; + System.out.println("Random Greeting: " + randomGreeting.get()); + } + +} \ No newline at end of file diff --git a/Search/BinarySearch.java b/Search/BinarySearch.java new file mode 100644 index 000000000000..07c0696f1493 --- /dev/null +++ b/Search/BinarySearch.java @@ -0,0 +1,38 @@ +/** + * BinarySearch.java + * This program implements recursive Binary Search on a sorted array. + * Time Complexity: O(log n) + */ + +public class BinarySearch { + + // Recursive method to perform binary search + public static int binarySearch(int[] arr, int left, int right, int target) { + if (left > right) { + return -1; // Base case: element not found + } + + int mid = left + (right - left) / 2; + + if (arr[mid] == target) { + return mid; // Found the target + } else if (target < arr[mid]) { + return binarySearch(arr, left, mid - 1, target); // Search in the left half + } else { + return binarySearch(arr, mid + 1, right, target); // Search in the right half + } + } + + public static void main(String[] args) { + int[] arr = {3, 8, 15, 23, 42, 56}; + int target = 23; + + int result = binarySearch(arr, 0, arr.length - 1, target); + + if (result != -1) { + System.out.println("Target " + target + " found at index: " + result); + } else { + System.out.println("Target " + target + " not found in the array."); + } + } +} \ No newline at end of file diff --git a/Search/LinearSearch.java b/Search/LinearSearch.java new file mode 100644 index 000000000000..372aaaef0cc1 --- /dev/null +++ b/Search/LinearSearch.java @@ -0,0 +1,30 @@ +/** + * LinearSearch.java + * This program implements the Linear Search algorithm. + * Time Complexity: O(n) + */ + +public class LinearSearch { + + public static int linearSearch(int[] arr, int target) { + // Traverse each element in the array + for (int i = 0; i < arr.length; i++) { + if (arr[i] == target) { + return i; // Found the target at index i + } + } + return -1; // Target not found + } + + public static void main(String[] args) { + int[] numbers = {5, 8, 12, 20, 35}; + int target = 20; + + int index = linearSearch(numbers, target); + if (index != -1) { + System.out.println("Target " + target + " found at index: " + index); + } else { + System.out.println("Target " + target + " not found in the array."); + } + } +} \ No newline at end of file diff --git a/Sorting/BubbleSort.java b/Sorting/BubbleSort.java new file mode 100644 index 000000000000..511c8dc6d653 --- /dev/null +++ b/Sorting/BubbleSort.java @@ -0,0 +1,40 @@ +/** + * BubbleSort.java + * This program implements the Bubble Sort algorithm. + * Time Complexity: O(n^2) + */ + +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, array is sorted + if (!swapped) + break; + } + } + + public static void main(String[] args) { + int[] arr = {64, 25, 12, 22, 11}; + bubbleSort(arr); + System.out.println("Sorted array:"); + for (int num : arr) { + System.out.print(num + " "); + } + } +} \ No newline at end of file diff --git a/Sorting/InsertionSort.java b/Sorting/InsertionSort.java new file mode 100644 index 000000000000..8da2ba4c5331 --- /dev/null +++ b/Sorting/InsertionSort.java @@ -0,0 +1,33 @@ +/** + * InsertionSort.java + * This program implements the Insertion Sort algorithm. + * Time Complexity: O(n^2) in worst case, O(n) in best case (already sorted). + */ + +public class InsertionSort { + + public static void insertionSort(int[] arr) { + // Loop from the second element to the end + for (int i = 1; i < arr.length; i++) { + int key = arr[i]; // Store the current element to be inserted + int j = i - 1; + + // Move elements of arr[0..i-1], that are greater than key, + // to one position ahead of their current position + while (j >= 0 && arr[j] > key) { + arr[j + 1] = arr[j]; // Shift element to the right + j = j - 1; + } + arr[j + 1] = key; // Insert the key into its correct position + } + } + + public static void main(String[] args) { + int[] arr = {29, 10, 14, 37, 13}; + insertionSort(arr); + System.out.println("Sorted array:"); + for (int num : arr) { + System.out.print(num + " "); + } + } +} \ No newline at end of file diff --git a/Sorting/MergeSort.java b/Sorting/MergeSort.java new file mode 100644 index 000000000000..fa7a80214a9e --- /dev/null +++ b/Sorting/MergeSort.java @@ -0,0 +1,69 @@ +/** + * MergeSort.java + * This program implements the Merge Sort algorithm using recursion. + * Time Complexity: O(n log n) + */ + +public class MergeSort { + + // Method to recursively divide and sort the array + public static void mergeSort(int[] arr, int left, int right) { + if (left < right) { + int mid = (left + right) / 2; + + // Recursively sort the left half + mergeSort(arr, left, mid); + + // Recursively sort the right half + mergeSort(arr, mid + 1, right); + + // Merge the two sorted halves + merge(arr, left, mid, right); + } + } + + // Method to merge two sorted halves + public static void merge(int[] arr, int left, int mid, int right) { + // Sizes of subarrays + int n1 = mid - left + 1; + int n2 = right - mid; + + // Temp arrays + int[] L = new int[n1]; + int[] R = new int[n2]; + + // Copy data to temp arrays + for (int i = 0; i < n1; ++i) + L[i] = arr[left + i]; + for (int j = 0; j < n2; ++j) + R[j] = arr[mid + 1 + j]; + + // Merge temp arrays back into arr + int i = 0, j = 0, k = left; + while (i < n1 && j < n2) { + if (L[i] <= R[j]) { + arr[k++] = L[i++]; + } else { + arr[k++] = R[j++]; + } + } + + // Copy remaining elements of L[] + while (i < n1) + arr[k++] = L[i++]; + + // Copy remaining elements of R[] + while (j < n2) + arr[k++] = R[j++]; + } + + public static void main(String[] args) { + int[] arr = {38, 27, 43, 3, 9, 82, 10}; + mergeSort(arr, 0, arr.length - 1); + + System.out.println("Sorted array:"); + for (int value : arr) { + System.out.print(value + " "); + } + } +} \ No newline at end of file diff --git a/Strings/AnagramChecker.java b/Strings/AnagramChecker.java new file mode 100644 index 000000000000..29225c30aadd --- /dev/null +++ b/Strings/AnagramChecker.java @@ -0,0 +1,51 @@ +/** + * AnagramChecker.java + * Checks whether two input strings are anagrams using a HashMap. + * Time Complexity: O(n), Space Complexity: O(n) + */ + +import java.util.HashMap; + +public class AnagramChecker { + + public static boolean areAnagrams(String str1, String str2) { + // Remove spaces and convert to lowercase + str1 = str1.replaceAll("\\s", "").toLowerCase(); + str2 = str2.replaceAll("\\s", "").toLowerCase(); + + // Quick length check + if (str1.length() != str2.length()) { + return false; + } + + // Count characters in first string + HashMap map = new HashMap<>(); + for (char c : str1.toCharArray()) { + map.put(c, map.getOrDefault(c, 0) + 1); + } + + // Subtract character counts using second string + for (char c : str2.toCharArray()) { + if (!map.containsKey(c)) { + return false; + } + map.put(c, map.get(c) - 1); + if (map.get(c) == 0) { + map.remove(c); + } + } + + return map.isEmpty(); + } + + public static void main(String[] args) { + String s1 = "Listen"; + String s2 = "Silent"; + + if (areAnagrams(s1, s2)) { + System.out.println("\"" + s1 + "\" and \"" + s2 + "\" are anagrams."); + } else { + System.out.println("\"" + s1 + "\" and \"" + s2 + "\" are NOT anagrams."); + } + } +} \ No newline at end of file