Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create BinarySearch.java #96

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions JAVA/BinarySearch.java
Original file line number Diff line number Diff line change
@@ -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.");
}
}
}
44 changes: 44 additions & 0 deletions JAVA/Bubblesort.java
Original file line number Diff line number Diff line change
@@ -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();
}
}