-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.java
More file actions
80 lines (73 loc) · 1.51 KB
/
MergeSort.java
File metadata and controls
80 lines (73 loc) · 1.51 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
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
package poorvatutorial;
public class MergeSort {
public static void MergeSortAlgo(int[] arr, int l, int h)
{
if(l<h)
{
int mid = (l+h+1)/2;
System.out.println("The mid is : "+mid);
int[] temp1 = new int[mid-l];
int[] temp2 = new int[h-mid+1];
for( int i = l;i <= mid-1; i++)
{
temp1[i-l] = arr[i];
System.out.println("The value in array1 is: "+temp1[i-l]);
}
for( int i = mid;i <= h ; i++)
{
temp2[i-mid] = arr[i];
System.out.println("The value in array 2 is :" +temp2[i-mid]);
}
MergeSortAlgo(temp1, l , mid-1);
MergeSortAlgo(temp2, mid, h);
Merge(temp1, temp2, arr, l, h, mid);
}
}
public static void Merge(int[] temp1, int[] temp2, int[] arr, int l, int h, int mid)
{
int i = 0;
int j = 0;
int k=l;
while(i <= temp1.length-1 && j <= temp2.length-1 )
{
if(temp1[i] < temp2[j])
{
arr[k] = temp1[i];
i++;
k++;
System.out.println("The value in array is: " +arr[k]);
}
else
{
arr[k] = temp2[j];
j++;
k++;
System.out.println("The value in array is: " +arr[k]);
}
}
while(i <= temp1.length-1)
{
arr[k] = temp1[i];
k++;
i++;
}
while(j <= temp2.length-1)
{
arr[k] = temp2[j];
k++;
j++;
}
PrintArray(arr);
}
public static void PrintArray(int[] arr)
{
for( int i = 0; i < arr.length; i ++)
{
System.out.print(arr[i]);
}
}
public static void main(String[] args) {
int[] myarray= {8,4,9,2};
MergeSortAlgo(myarray,0,myarray.length-1);
}
}