-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.java
More file actions
57 lines (43 loc) · 870 Bytes
/
Copy pathMergeSort.java
File metadata and controls
57 lines (43 loc) · 870 Bytes
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
void MergeSort (int[] array)
{
int[] helper = new int[array.length];
mergesort(array, helper, 0 , array.length);
}
void mergesort(int array[], int helper[], int low, int high)
{
if (low < high)
{
middle = (low + high) / 2
mergesort(array, helper, low, middle);
mergesort(array, helper, middle + 1, high);
merge(array, helper, low, middle, high);
}
}
void merge(int[] array, int[] helper, int low, int middle, int high)
{
for(i=0;i<array.length;i++)
{
helper[i] = array[i];
}
int left = low;
int current = low;
int right = middle + 1;
while (left <= middle && right <= high)
{
if (helper[left] < helper[right])
{
array[current] = helper[left]
left ++;
}
else
{
array[current] = helper[right]
right ++;
}
current ++;
}
for(int i = 0; i < middle - left; i ++)
{
array[current + i] = helper[left + i];
}
}