-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergeSort.java
125 lines (105 loc) · 3.82 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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package mergesort;
/**
*
* @author admin
*/
public class MergeSort {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
final int LENGTH = 45;
int A[] = new int[LENGTH];
for (int i = 0; i < LENGTH; i++)
A[i] = (int) (1 + Math.random() * LENGTH); // range [1..LENGTH]
System.out.print("original array A: ");
print(A);
mergeSort(A);
System.out.print("sorted array B: ");
print(A);
}//End of main method
static void mergeSort(int A[]) {
if (A.length <= 1){
return;
}
int size1; // size of first & second temp arrays
int size3; // size of third temp array
if (A.length == 2) {
size1 = 1;
size3 = 0;
}
else {
size1 = A.length / 3; // size of first & second temp arrays
size3 = A.length - 2* size1; // size of third temp array
} // end if
int temp1[] = new int[size1];
int temp2[] = new int[size1];
int temp3[] = new int[size3];
for (int i = 0; i < size1; i++)
temp1[i] = A[i];
for (int i = 0; i < size1; i++)
temp2[i] = A[size1+i];
for (int i = 0; i < size3; i++)
temp3[i] = A[2*size1 + i];
// sort each of the temporary arrays (recursive step)
mergeSort(temp1);
mergeSort(temp2);
mergeSort(temp3);
// merge the temp arrays back into the original array
merge(temp1, temp2, temp3, A);
} // end mergeSort method
private static void merge(int[] source1, int[] source2, int[] source3, int[] dest) {
int srcElt1 = getElement(source1, 0);
int srcElt2 = getElement(source2, 0);
int srcElt3 = getElement(source3, 0);
int srcIndex1 = 0;
int srcIndex2 = 0;
int srcIndex3 = 0;
int destIndex = 0;
while (destIndex < dest.length) {
if (srcElt1 <= srcElt2) {
if (srcElt3 <= srcElt1) {
dest[destIndex] = srcElt3;
srcIndex3++;
srcElt3 = getElement(source3, srcIndex3);
}
else {
dest[destIndex] = srcElt1;
srcIndex1++;
srcElt1 = getElement(source1, srcIndex1);
}
}
else {
if (srcElt3 <= srcElt2) {
dest[destIndex] = srcElt3;
srcIndex3++;
srcElt3 = getElement(source3, srcIndex3);
}
else {
dest[destIndex] = srcElt2;
srcIndex2++;
srcElt2 = getElement(source2, srcIndex2);
}
}
destIndex++;
}
} // end merge
public static int getElement(int array[], int index) {
if (index < array.length)
return array[index];
else
return Integer.MAX_VALUE;
} // end getElement
static void print(int arr[]) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
} // end print
}//End of class