-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathTest.java
More file actions
37 lines (32 loc) · 1.22 KB
/
Copy pathTest.java
File metadata and controls
37 lines (32 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package sdeProblems;
import java.util.Arrays;
public class Test
{
static int arr1[] = new int[]{1, 5, 9, 10, 15, 20};
static int arr2[] = new int[]{2, 3, 8, 13};
static void merge(int m, int n)
{
// Iterate through all elements of ar2[] starting from to the last element
for (int i=n-1; i>=0; i--)
{
/* Find the smallest element greater than ar2[i]. Move all elements one position ahead till the smallest greater
element is not found */
int j, last = arr1[m-1];
for (j=m-2; j >= 0 && arr1[j] > arr2[i]; j--)
arr1[j+1] = arr1[j];
// If there was a greater element
if (j != m-2 || last > arr2[i]) {
arr1[j+1] = arr2[i];
arr2[i] = last; }
}
}
// Driver method to test the above function
public static void main1(String[] args)
{
merge(arr1.length,arr2.length);
System.out.print("After Merging nFirst Array: ");
System.out.println(Arrays.toString(arr1));
System.out.print("Second Array: ");
System.out.println(Arrays.toString(arr2));
}
}