-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.java
More file actions
80 lines (70 loc) · 2.29 KB
/
MergeSort.java
File metadata and controls
80 lines (70 loc) · 2.29 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 com.yijie.sort;
import java.util.Arrays;
public class MergeSort {
public static void main(String[] args) {
int arr[] = {8, 4, 5, 7, 1, 3, 6, 2};
int[] temp = new int[arr.length];
mergeSort(arr, 0, arr.length - 1, temp);
System.out.println("after merge sort: " + Arrays.toString(arr));
}
//Method: Merge + Sort
public static void mergeSort(int[] arr, int left, int right, int[] temp) {
if (left < right) {
int mid = (left + right) / 2;
//Recursion to the left
mergeSort(arr, left, mid, temp);
//Recursion to the right
mergeSort(arr, mid + 1, right, temp);
//merge
merge(arr, left, mid, right, temp);
}
}
//Method Merge
/***
*
* @param arr the original array to be sorted
* @param left initialized index of the left array
* @param mid middle index
* @param right initialized index of the right array
* @param temp auxiliary array
*/
public static void merge(int[] arr, int left, int mid, int right, int[] temp) {
int i = left;//initialize i as original index of left array
int j = mid + 1;//initialize i as original index of right array
int t = 0;
//1. put the left and right sorted lists to the temp array, until either array is empty
while (i <= mid && j <= right) {
//if the element in the left array is smaller than the element in the right array,
//copy the element to the temp array
//
if (arr[i] <= arr[j]) {
temp[t] = arr[i];
t++;
i++;
} else {
temp[t] = arr[j];
t++;
j++;
}
}
//2. transfer the remaining items in the not empty array to the end of the temp array
while (i <= mid) {
temp[t] = arr[i];
t++;
i++;
}
while (j <= right) {
temp[t] = arr[j];
t++;
j++;
}
//3. Copy the elements in the temp to the arr
t = 0;
int tempLeft = left;
while (tempLeft <= right) {
arr[tempLeft] = temp[t];
t++;
tempLeft++;
}
}
}