Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfa314c2

Browse files
committed
merge Sort
1 parentbb3eb2b commitfa314c2

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

‎sort/MergeSort.java

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
packageAlgorithms.sort;
2+
3+
/********************************************************
4+
*
5+
* 08-722 Data Structures for Application Programmers
6+
* Lecture 14 Advanced Sorting
7+
*
8+
* Naive version of Merge Sort
9+
*
10+
*********************************************************/
11+
importjava.util.Arrays;
12+
13+
publicclassMergeSort {
14+
15+
privatestaticfinalintSIZE =10000;
16+
17+
publicstaticint[]mergeSort(int[]data) {
18+
// parameter valid judge.
19+
if (data ==null) {
20+
returnnull;
21+
}
22+
23+
// the base case.
24+
intlen =data.length;
25+
if (len <=1) {
26+
returndata;
27+
}
28+
29+
// divide into two arrays.
30+
// create left half and right half.
31+
intleft[] =newint[len/2];
32+
intright[] =newint[len -len/2];
33+
34+
System.arraycopy(data,0,left,0,len/2);
35+
System.arraycopy(data,len/2,right,0,len -len/2);
36+
37+
// call itself to sort left half
38+
left =mergeSort(left);
39+
right =mergeSort(right);
40+
41+
returnmerge(left,right);
42+
}
43+
44+
// Precondition: two input arrays are sorted and they are not null.
45+
privatestaticint[]merge(int[]left,int[]right) {
46+
intlen =left.length +right.length;
47+
48+
int[]ret =newint[len];
49+
50+
intleftPoint =0;
51+
intrightPoint =0;
52+
53+
intcur =0;
54+
while (leftPoint <left.length &&rightPoint <right.length) {
55+
if (left[leftPoint] <right[rightPoint]) {
56+
ret[cur++] =left[leftPoint++];
57+
}else {
58+
ret[cur++] =right[rightPoint++];
59+
}
60+
}
61+
62+
if (leftPoint >=left.length) {
63+
while (rightPoint <right.length) {
64+
ret[cur++] =right[rightPoint++];
65+
}
66+
}else {
67+
while (leftPoint <left.length) {
68+
ret[cur++] =left[leftPoint++];
69+
}
70+
}
71+
72+
returnret;
73+
}
74+
75+
publicstaticvoidmergeSort2(int[]data) {
76+
// parameter valid judge.
77+
if (data ==null) {
78+
returnnull;
79+
}
80+
81+
// the base case.
82+
intlen =data.length;
83+
if (len <=1) {
84+
returndata;
85+
}
86+
87+
// divide into two arrays.
88+
// create left half and right half.
89+
intleft[] =newint[len/2];
90+
intright[] =newint[len -len/2];
91+
92+
System.arraycopy(data,0,left,0,len/2);
93+
System.arraycopy(data,len/2,right,0,len -len/2);
94+
95+
// call itself to sort left half
96+
left =mergeSort(left);
97+
right =mergeSort(right);
98+
99+
returnmerge(left,right);
100+
}
101+
102+
publicstaticvoidmain(String[]args) {
103+
int[]a =newint[SIZE];
104+
for (inti =0;i <SIZE;i++)
105+
a[i] = (int) (Math.random() *SIZE);
106+
107+
//mergeSort(a);
108+
109+
int[]test = {42,12,89,27,94,63,3,78 };
110+
System.out.println(Arrays.toString(mergeSort(test)));
111+
112+
// test merge method
113+
int[]left = {12,42,63,89 };
114+
int[]right = {3,27,78,94 };
115+
System.out.println(Arrays.toString(merge(left,right)));
116+
117+
// test merge method
118+
int[]left2 = {};
119+
int[]right2 = {3,27,78,94 };
120+
System.out.println(Arrays.toString(merge(left2,right2)));
121+
}
122+
123+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp