Skip to content

dd Merge Sort Algorithm in Java #6280

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

Closed
Closed
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
38 changes: 38 additions & 0 deletions Search/BinarySearch.java
Original file line number Diff line number Diff line change
@@ -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.");
}
}
}
30 changes: 30 additions & 0 deletions Search/LinearSearch.java
Original file line number Diff line number Diff line change
@@ -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.");
}
}
}
40 changes: 40 additions & 0 deletions Sorting/BubbleSort.java
Original file line number Diff line number Diff line change
@@ -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 + " ");
}
}
}
33 changes: 33 additions & 0 deletions Sorting/InsertionSort.java
Original file line number Diff line number Diff line change
@@ -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 + " ");
}
}
}
69 changes: 69 additions & 0 deletions Sorting/MergeSort.java
Original file line number Diff line number Diff line change
@@ -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 + " ");
}
}
}