Advertisement
amitsen01ei

MergeSortedArray.java

Sep 10th, 2023
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.48 KB | Source Code | 0 0
  1. public class MergeTwoSortedArrays {
  2.  
  3.     /**
  4.      * Merges two sorted integer arrays into one sorted integer array.
  5.      *
  6.      * <p>This method merges two sorted arrays, nums1 and nums2, and stores the result
  7.      * in nums1. The first m elements of nums1 and the first n elements of nums2 are considered
  8.      * for merging. After the operation, nums1 will contain the merged sorted array.</p>
  9.      *
  10.      * <p>Time Complexity: O(M + N), where M is the size of nums1 and N is the size of nums2.</p>
  11.      * <p>Space Complexity: O(M + N), due to the use of an auxiliary array.</p>
  12.      *
  13.      * @param nums1 The first sorted array. Should have enough space to hold the merged result.
  14.      * @param m     The number of meaningful elements in nums1.
  15.      * @param nums2 The second sorted array.
  16.      * @param n     The number of meaningful elements in nums2.
  17.      */
  18.     private static void merge(int[] nums1, int m, int[] nums2, int n) {
  19.  
  20.         int[] a = new int[m + n];
  21.  
  22.         int i = 0, j = 0, k = 0;
  23.  
  24.         while (i < m && j < n) {
  25.             if (nums1[i] < nums2[j]) {
  26.                 a[k++] = nums1[i++];
  27.             } else if (nums2[j] < nums1[i]) {
  28.                 a[k++] = nums2[j++];
  29.             } else {
  30.                 a[k++] = nums1[i++];
  31.                 a[k++] = nums2[j++];
  32.             }
  33.         }
  34.  
  35.         if (i < m) {
  36.             while (i < m) {
  37.                 a[k++] = nums1[i++];
  38.             }
  39.         }
  40.  
  41.         if (j < n) {
  42.             while (j < n) {
  43.                 a[k++] = nums2[j++];
  44.             }
  45.         }
  46.  
  47.         for (i = 0; i < m + n; i++) {
  48.             nums1[i] = a[i];
  49.         }
  50.     }
  51.  
  52.     public static void main(String[] args) {
  53.  
  54.         int[] nums1 = {1, 2, 3, 0, 0, 0};
  55.         int[] nums2 = {2, 5, 6};
  56.         int m = 3, n = 3;
  57.  
  58.         merge(nums1, m, nums2, n);
  59.  
  60.         for (int num : nums1) {
  61.             System.out.print(num + " ");
  62.         }
  63.  
  64.         System.out.println();
  65.  
  66.         int[] nums12 = {1};
  67.         int m12 = 1;
  68.         int[] nums22 = {};
  69.         int n22 = 0;
  70.  
  71.         merge(nums12, m12, nums22, n22);
  72.  
  73.         for (int num : nums12) {
  74.             System.out.print(num + " ");
  75.         }
  76.  
  77.         System.out.println();
  78.  
  79.         int[] nums13 = {0};
  80.         int m13 = 0;
  81.         int[] nums23 = {1};
  82.         int n23 = 1;
  83.  
  84.         merge(nums13, m13, nums23, n23);
  85.  
  86.         for (int num : nums13) {
  87.             System.out.print(num + " ");
  88.         }
  89.  
  90.         System.out.println();
  91.     }
  92. }
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement