-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSortAlgorithm.java
More file actions
38 lines (35 loc) · 1.05 KB
/
MergeSortAlgorithm.java
File metadata and controls
38 lines (35 loc) · 1.05 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
package SortingGithub;
public class MergeSortAlgorithm {
public static void main(String[] args) {
int[] arr = {8,2,9,0,1,4,0,0,-2,-10,3};
mergeSort(arr,0,arr.length-1);
for(int i:arr) System.out.print(i+" ");
}
private static void mergeSort(int[] arr,int left, int right) {
if(left >= right) return;
int mid = (left+right)/2;
mergeSort(arr,left,mid);
mergeSort(arr,mid+1,right);
merge(arr,left,mid,right);
}
private static void merge(int[] arr,int left, int mid, int right) {
int[] leftArray = new int[mid-left+1];
int[] rightArray = new int[right-mid];
for(int i = 0; i < mid-left+1; i++) {
leftArray[i] = arr[left+i];
}
for(int i = 0; i < right-mid; i++) {
rightArray[i] = arr[mid+1+i];
}
int p = 0, q = 0;
int k = left; // very important
while (p < leftArray.length && q < rightArray.length) {
if (leftArray[p] < rightArray[q]) arr[k++] = leftArray[p++];
else arr[k++] = rightArray[q++];
}
while (p < leftArray.length)
arr[k++] = leftArray[p++];
while (q < rightArray.length)
arr[k++] = rightArray[q++];
}
}