Skip to content

Commit 4e1f7f9

Browse files
Merge Sort in Java
1 parent 916662c commit 4e1f7f9

File tree

2 files changed

+188
-0
lines changed

2 files changed

+188
-0
lines changed

sorting/MergeSortIterative.java

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
9+
public class MergeSortIterative
10+
{
11+
public static void sort(int a[], int n)
12+
{
13+
int[] temp = new int[n];
14+
int size=1;
15+
while(size<=n-1)
16+
{
17+
sortPass(a,temp,size,n);
18+
size=size*2;
19+
}
20+
}
21+
22+
private static void sortPass(int a[], int temp[], int size, int n)
23+
{
24+
int i,low1,up1,low2,up2;
25+
26+
low1=0;
27+
28+
while( low1+size <= n-1 )
29+
{
30+
up1=low1+size-1;
31+
low2=low1+size;
32+
up2=low2+size-1;
33+
34+
if(up2>=n)/*if length of last sublist is less than size*/
35+
up2=n-1;
36+
37+
merge(a,temp,low1,up1,low2,up2);
38+
39+
low1=up2+1; /*Take next two sublists for merging*/
40+
}
41+
42+
for(i=low1; i<=n-1; i++)
43+
temp[i]=a[i]; /*If any sublist is left alone*/
44+
45+
copy(a, temp, n);
46+
}
47+
48+
49+
/* a[low1]...a[up1] and a[low2]...a[up2] merged to temp[low1]...temp[up2] */
50+
private static void merge( int a[], int temp[], int low1, int up1, int low2, int up2 )
51+
{
52+
int i = low1;
53+
int j = low2;
54+
int k = low1;
55+
56+
while( (i<=up1) && (j<=up2) )
57+
{
58+
if(a[i]<=a[j])
59+
temp[k++]=a[i++];
60+
else
61+
temp[k++]=a[j++];
62+
}
63+
64+
while(i<=up1)
65+
temp[k++]=a[i++];
66+
67+
while(j<=up2)
68+
temp[k++]=a[j++];
69+
}
70+
71+
/*copies temp[low]....temp[up] to a[low]...a[up]*/
72+
private static void copy(int a[], int temp[], int n)
73+
{
74+
for(int i=0; i<n; i++)
75+
a[i]=temp[i];
76+
}
77+
78+
public static void main(String[] args)
79+
{
80+
int i,n;
81+
int[] a = new int[20];
82+
Scanner scan = new Scanner(System.in);
83+
84+
System.out.print("Enter the number of elements : ");
85+
n = scan.nextInt();
86+
87+
for(i=0; i<n; i++)
88+
{
89+
System.out.print("Enter element " + (i+1) + " : ");
90+
a[i] = scan.nextInt();
91+
}
92+
93+
sort(a,n);
94+
95+
System.out.println("Sorted array is : ");
96+
for(i=0; i<n; i++)
97+
System.out.print(a[i] + " ");
98+
System.out.println();
99+
scan.close();
100+
}
101+
}

sorting/MergeSortRecursive.java

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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 MergeSortRecursive
9+
{
10+
private MergeSortRecursive(){} //this class is not for instantiation
11+
12+
public static void sort(int a[], int n)
13+
{
14+
int[] temp = new int[n];
15+
sort(a,temp,0,n-1);
16+
}
17+
18+
private static void sort(int[] a, int[] temp, int low, int up)
19+
{
20+
if(low==up) /*only one element*/
21+
return;
22+
23+
int mid = (low+up)/2;
24+
25+
sort(a, temp, low, mid); /* Sort a[low]....a[mid] */
26+
sort(a, temp, mid+1, up); /* Sort a[mid+1]....a[up] */
27+
28+
/* Merge a[low]...a[mid] and a[mid+1]....a[up] to temp[low]...temp[up] */
29+
merge(a, temp, low, mid, mid+1, up);
30+
31+
/* Copy temp[low]...temp[up] to a[low]...a[up] */
32+
copy(a,temp,low,up);
33+
}
34+
35+
/* a[low1]...a[up1] and a[low2]...a[up2] merged to temp[low1]...temp[up2] */
36+
private static void merge( int a[], int temp[], int low1, int up1, int low2, int up2 )
37+
{
38+
int i = low1;
39+
int j = low2;
40+
int k = low1;
41+
42+
while( (i<=up1) && (j<=up2) )
43+
{
44+
if(a[i]<=a[j])
45+
temp[k++]=a[i++];
46+
else
47+
temp[k++]=a[j++];
48+
}
49+
50+
while(i<=up1)
51+
temp[k++]=a[i++];
52+
53+
while(j<=up2)
54+
temp[k++]=a[j++];
55+
}
56+
57+
/*copies temp[low]....temp[up] to a[low]...a[up]*/
58+
private static void copy(int[] a, int[] temp, int low, int up)
59+
{
60+
for(int i=low; i<=up; i++)
61+
a[i]=temp[i];
62+
}
63+
64+
public static void main(String[] args)
65+
{
66+
int i,n;
67+
int[] a = new int[20];
68+
Scanner scan = new Scanner(System.in);
69+
70+
System.out.print("Enter the number of elements : ");
71+
n = scan.nextInt();
72+
73+
for(i=0; i<n; i++)
74+
{
75+
System.out.print("Enter element " + (i+1) + " : ");
76+
a[i] = scan.nextInt();
77+
}
78+
79+
sort(a,n);
80+
81+
System.out.println("Sorted array is : ");
82+
for(i=0; i<n; i++)
83+
System.out.print(a[i] + " ");
84+
System.out.println();
85+
scan.close();
86+
}
87+
}

0 commit comments

Comments
 (0)