-
Notifications
You must be signed in to change notification settings - Fork 1k
/
MergeSortedArrays.java
80 lines (74 loc) · 2.02 KB
/
MergeSortedArrays.java
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.util.*;
class MergeSortedArrays
{
private static void merge(int arr1[], int arr2[])
{
if (arr1 == null || arr2 == null) {
return;
}
// Iterate through all elements of arr2[] in reverse
for (int i = (arr2.length - 1); i >= 0; i--)
{
// In here we compare the different terms of
// both arrays and place them in respective positions
int j, last = arr1[arr1.length - 1];
for (j = arr1.length - 2; j >= 0 && arr1[j] > arr2[i]; j--) {
arr1[j+1] = arr1[j];
}
// If there was a greater element
if (j != arr1.length - 2 || last > arr2[i])
{
arr1[j+1] = arr2[i];
arr2[i] = last;
}
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
// taking input array 1
System.out.println("Enter size of first array:");
int size1 = sc.nextInt();
int arr1[] = new int[size1];
System.out.println("Enter array elements:");
for (int i=0; i<size1; i++) {
arr1[i] = sc.nextInt();
}
// taking input array 2
System.out.println("Enter size of second array:");
int size2 = sc.nextInt();
int arr2[] = new int[size2];
System.out.println("Enter array elements:");
for (int i=0; i<size2; i++) {
arr2[i] = sc.nextInt();
}
sc.close();
merge(arr1, arr2);
System.out.println("Sorted arrays are:");
for (int i = 0; i < size1; i++) {
System.out.print(arr1[i] + " ");
}
System.out.println();
for (int i = 0; i < size2; i++) {
System.out.print(arr2[i] + " ");
}
}
}
/**
* Sample input/output
* Enter size of first array:
* 6
* Enter array elements:
* 1 8 9 15 16 18
* Enter size of second array:
* 4
* Enter array elements:
* 2 4 7 10
* Sorted arrays are:
* 1 2 4 7 8 9
* 10 15 16 18
*
* Time complexity: O(m*n)
* where m and n are sizes of the arrays
* Space complexity: O(1)
*/