Skip to content

Commit cb26595

Browse files
Merging in Java
1 parent 4e1f7f9 commit cb26595

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed

sorting/Merging.java

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
import java.util.Scanner;
7+
8+
public class Merging
9+
{
10+
private Merging(){} //this class is not for instantiation
11+
12+
public static void merge(int[] a1, int[] a2, int[] temp, int n1, int n2)
13+
{
14+
int i=0,j=0,k=0;
15+
16+
while( (i<=n1-1) && (j<=n2-1) )
17+
{
18+
if(a1[i] < a2[j])
19+
temp[k++]=a1[i++];
20+
else
21+
temp[k++]=a2[j++];
22+
}
23+
24+
/*copy remaining elements of a1, array a2 finished */
25+
while(i<=n1-1)
26+
temp[k++]=a1[i++];
27+
28+
/*copy remaining elements of a2, array a1 finished */
29+
while(j<=n2-1)
30+
temp[k++]=a2[j++];
31+
}
32+
33+
public static void main(String[] args)
34+
{
35+
int i,n1,n2;
36+
int[] a1 = new int[20];
37+
int[] a2 = new int[20];
38+
int[] temp = new int[40];
39+
40+
Scanner scan = new Scanner(System.in);
41+
42+
System.out.print("Enter the number of elements in array a1 : ");
43+
n1 = scan.nextInt();
44+
System.out.println("Enter elements in sorted order : " );
45+
for(i=0; i<n1; i++)
46+
{
47+
System.out.print("Enter element " + (i+1) + " : ");
48+
a1[i] = scan.nextInt();
49+
}
50+
51+
System.out.print("Enter the number of elements in array a2 : ");
52+
n2 = scan.nextInt();
53+
System.out.println("Enter elements in sorted order : ");
54+
for(i=0; i<n2; i++)
55+
{
56+
System.out.print("Enter element " + (i+1) + " : ");
57+
a2[i] = scan.nextInt();
58+
}
59+
60+
merge(a1,a2,temp,n1,n2);
61+
62+
System.out.println("Merged array temp is : ");
63+
for(i=0; i<n1+n2; i++)
64+
System.out.print(temp[i] + " ");
65+
System.out.println();
66+
scan.close();
67+
}
68+
69+
}

sorting/Merging1.java

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
import java.util.Scanner;
7+
8+
public class Merging1
9+
{
10+
private Merging1(){} //this class is not for instantiation
11+
12+
/* a[low1]...a[up1] and a[low2]...a[up2] merged to temp[low1]...temp[up2] */
13+
public static void merge( int a[], int temp[], int low1, int up1, int low2, int up2 )
14+
{
15+
int i = low1;
16+
int j = low2;
17+
int k = low1;
18+
19+
while( (i<=up1) && (j<=up2) )
20+
{
21+
if(a[i]<=a[j])
22+
temp[k++]=a[i++];
23+
else
24+
temp[k++]=a[j++];
25+
}
26+
27+
while(i<=up1)
28+
temp[k++]=a[i++];
29+
30+
while(j<=up2)
31+
temp[k++]=a[j++];
32+
}
33+
34+
public static void main(String[] args)
35+
{
36+
int i;
37+
int[] a = {1,3,5,7, 2,4,6,9,11,14};
38+
int[] temp = new int[20];
39+
40+
Scanner scan = new Scanner(System.in);
41+
42+
merge(a,temp,0,3,4,9);
43+
44+
System.out.println("Array a is : ");
45+
for(i=0; i<=9; i++)
46+
System.out.print(a[i] + " ");
47+
System.out.println();
48+
49+
System.out.println("Merged array temp is : ");
50+
for(i=0; i<=9; i++)
51+
System.out.print(temp[i] + " ");
52+
System.out.println();
53+
scan.close();
54+
}
55+
}

0 commit comments

Comments
 (0)