From f40566906deef95b21af813f9b4eb35cf73d5ae8 Mon Sep 17 00:00:00 2001 From: Farheen Shabbir Shaikh Date: Sun, 8 Jun 2025 17:59:27 -0700 Subject: [PATCH 1/3] Add Bubble Sort algorithm with comments --- Sorting/BubbleSort.java | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Sorting/BubbleSort.java 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 From c0601d005ae5d7e09ec03bf86b4ea34bf9a65903 Mon Sep 17 00:00:00 2001 From: Farheen Shabbir Shaikh Date: Sun, 8 Jun 2025 18:26:12 -0700 Subject: [PATCH 2/3] Add Insertion Sort algorithm in Java with comments --- Sorting/InsertionSort.java | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Sorting/InsertionSort.java 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 From 9d5490900f3e89856c0d2d0edfe4712f7c1b5d7f Mon Sep 17 00:00:00 2001 From: Farheen Shabbir Shaikh Date: Sun, 8 Jun 2025 18:36:20 -0700 Subject: [PATCH 3/3] Add Linear Search algorithm in Java with comments --- Search/LinearSearch.java | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Search/LinearSearch.java 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