-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergeSort.java
110 lines (86 loc) · 1.97 KB
/
mergeSort.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
class mergeSort{
void merge(int arr[], int low, int mid, int high){
//We have got an array; Now we need to obtain two subarrays- left and right
//in order to be able to iterate separately and store result in final sorted array
int n1 = mid - low + 1;
int n2 = high - (mid +1) +1;
int left[] = new int[n1];
int right[] = new int[n2];
//Copying elements into respective subarrays
for(int i=0;i<n1;i++)
{
left[i] = arr[low+i];
}
for(int j=0;j<n2;j++)
{
right[j] = arr[mid+1+j];
}
System.out.print("\nLeft: ");
printArray(left);
System.out.print("\tRight: ");
printArray(right);
//Merging the two subarrays
int i=0, j=0, k=low; //do not put k=0 or k=1!!!!!!
while(i<n1 && j<n2)
{
if(left[i]<=right[j])
{
arr[k] = left[i];
i++;
k++;
}
else
{
arr[k] = right[j];
j++;
k++;
}
}
/* Copy remaining elements of left[] if any */
while (i < n1) {
arr[k] = left[i];
i++;
k++;
}
/* Copy remaining elements of right[] if any */
while (j < n2) {
arr[k] = right[j];
j++;
k++;
}
}
void sort(int arr[], int low, int high){
if(low<high)
{
int mid = (low + high)/2;
//Dividing problem into subparts and calling sort on these sub parts
sort(arr,low,mid);
sort(arr,mid+1,high);
//Combining the solutions of subparts
merge(arr,low,mid,high);
System.out.print("\nMerge step: ");
printArray(arr);
}
}
static void printArray(int arr[])
{
int n=arr.length;
for(int i=0;i<n;i++)
{
System.out.print(arr[i] + " ");
}
//System.out.println();
}
public static void main(String args[]){
mergeSort obj = new mergeSort();
int arr[] = {5,7,2,3,9,8,1,4,0,6};
System.out.print("Unsorted array: ");
printArray(arr);
System.out.println();
int n = arr.length;
//low=0,high=n-1
obj.sort(arr,0,n-1);
System.out.print("\n\nSorted array: ");
printArray(arr);
}
}