Merge sort is a divide-and-conquer method that sorts an array by splitting it into half recursively until the subarrays are of size 1 or 0, then merging them back together in sorted order. The main notion underlying this technique is that merging two sorted arrays is substantially easier than sorting an unsorted array.
The merge sort algorithm can be implemented as follows:
public static void mergeSort(int[] arr) {
if (arr == null || arr.length <= 1) {
return;
}
int[] temp = new int[arr.length];
mergeSortHelper(arr, temp, 0, arr.length - 1);
}
private static void mergeSortHelper(int[] arr, int[] temp, int left, int right) {
if (left < right) {
int mid = (left + right) / 2;
mergeSortHelper(arr, temp, left, mid);
mergeSortHelper(arr, temp, mid + 1, right);
merge(arr, temp, left, mid, right);
}
}
private static void merge(int[] arr, int[] temp, int left, int mid, int right) {
for (int i = left; i <= right; i++) {
temp[i] = arr[i];
}
int leftPointer = left;
int rightPointer = mid + 1;
int current = left;
while (leftPointer <= mid && rightPointer <= right) {
if (temp[leftPointer] <= temp[rightPointer]) {
arr[current] = temp[leftPointer];
leftPointer++;
} else {
arr[current] = temp[rightPointer];
rightPointer++;
}
current++;
}
while (leftPointer <= mid) {
arr[current] = temp[leftPointer];
leftPointer++;
current++;
}
}
Example:
Assumptions N = 8 A = [1, 9, 14, 2, 17, 14, 5, 18] Approach Subsequence: [9,14,17,14,18] is good which is the maximum length subsequence. Hence the output is 5.
Solution:
import java.util.Arrays;
public class GoodSubsequence {
public static void main(String[] args) {
int[] A = {1, 9, 14, 2, 17, 14, 5, 18};
int n = A.length;
// sort the array using merge sort
mergeSort(A, 0, n-1);
// find the length of the longest good subsequence
int maxLen = longestGoodSubsequence(A, n);
System.out.println("Maximum length of good subsequence: " + maxLen);
}
// implementation of merge sort
private static void mergeSort(int[] arr, int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid+1, right);
merge(arr, left, mid, right);
}
}
// implementation of merge operation in merge sort
private static void merge(int[] arr, int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
int[] L = new int[n1];
int[] R = new int[n2];
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];
}
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
// implementation of finding the length of longest good subsequence
private static int longestGoodSubsequence(int[] arr, int n) {
int[] dp = new int[n];
Arrays.fill(dp, 1);
int maxLen = 1;
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (Math.abs(arr[i]-arr[j]) <= 10) {
dp[i] = Math.max(dp[i], dp[j]+1);
}
}
maxLen = Math.max(maxLen, dp[i]);
}
return maxLen;
}
}
Note: also read about Quick Sort
Please follow me to read my latest post on programming and technology if you like my post.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…