-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathMergeSort.java
40 lines (34 loc) · 1.08 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
public class MergeSort {
public static void main(String[] args) {
System.out.println("hey hey hey!");
int arr [] = {-1, 6, 7, 4, -3, 43};
mergeSort(arr, 0, arr.length);
for(int i = 0; i<arr.length; i++){
System.out.println(arr[i]);
}
}
public static void mergeSort(int arrIn[], int start, int end){
if(end - start < 2){
return;
}
int mid = (start + end)/2;
mergeSort(arrIn, start, mid);
mergeSort(arrIn, mid, end);
merge(arrIn, start, mid, end);
}
public static void merge(int arrIn[], int start, int mid, int end){
//Change the condition here and on line 33 for change in ascending or descending order
if(arrIn[mid-1]>arrIn[mid]){
return;
}
int i = start;
int j = mid;
int temp[] = new int[end - start];
int tempIndex = 0;
while(i<mid && j<end){
temp[tempIndex++] = arrIn[i] >= arrIn[j] ? arrIn[i++] : arrIn[j++];
}
System.arraycopy(arrIn, i,arrIn, start+tempIndex, mid-i);
System.arraycopy(temp, 0, arrIn, start, tempIndex);
}
}