-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathALT_MergeSort.java
More file actions
62 lines (51 loc) · 1.46 KB
/
Copy pathALT_MergeSort.java
File metadata and controls
62 lines (51 loc) · 1.46 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
package Sorting;
import java.util.Arrays;
public class ALT_MergeSort {
public static void main(String[] args) {
int[] arr = {5,4,3,2,1};
System.out.println(Arrays.toString(sort(arr)));
}
static int[] sort(int[] arr){
// base case = when array is null or have just a single ele
if(arr == null || arr.length < 2)
return arr;
// now we do partition
int mid = arr.length/2;
int[] left = sort(Arrays.copyOfRange(arr , 0 , mid));
int[] right = sort(Arrays.copyOfRange(arr , mid , arr.length));
return merge(left,right);
}
static int[] merge(int[] first , int[] last){
// create the ans array
int[] ans = new int[first.length + last.length];
// to access the index of ans array
int k = 0;
// to access the index of the divided arrays
int i = 0;
int j = 0;
// checking the eles:
while(i < first.length && j < last.length){
if (first[i] < last[j]){
ans[k] = first[i];
i++;
}
else{
ans[k] = last[j];
j++;
}
k++;
}
// for the left eles:
while(i != first.length){
ans[k] = first[i];
i++;
k++;
}
while(j != last.length){
ans[k] = last[j];
j++;
k++;
}
return ans;
}
}