-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortMerge.java
More file actions
97 lines (85 loc) · 2.32 KB
/
SortMerge.java
File metadata and controls
97 lines (85 loc) · 2.32 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package ds;
import java.util.Scanner;
public class SortMerge {
/*
* i have no idea, just define i point to the s, j point to the m + 1, then
* proceed to compare the array[i] and array[j], tmp store the smaller.
* MergSort like two link in DataStructure written by yanweimin.
*/
static void Merge(int[] array, int[] tmp, int s, int m, int e) {
// when start merger, we use the tmp array store the sorted element
/*
* System.out.println(s + " " + m + " " + e); show(array);
*/
// point to the start and end of the array
// version-1
/*
* int j,k,i; for(j = m + 1,i = s, k = s; i <= m && j <= e; k++){ if
* (array[i] < array[j]) tmp[k] = array[i++]; else tmp[k] = array[j++];
* }
*/
// version_2 ;k pos store the current small element
int i = s, j = m + 1, k = s;
while (i <= m && j <= e) {
if (array[i] < array[j]) {
tmp[k++] = array[i++];
} else {
tmp[k++] = array[j++];
}
}
// if right part haved add to the tmp, so left part copy to the tmp
// directly
while (i <= m) {
tmp[k++] = array[i++];
}
// as the above
while (j <= e) {
tmp[k++] = array[j++];
}
// copy the sorted tmp to the original array; my idea by myself
for (int r = s; r <= e; r++) {
array[r] = tmp[r];
// System.out.print(array[r] + " ");
}
}
/*
* s start of the array e the end of the array
*/
static void MSort(int[] array, int s, int e) {
int[] tmp = new int[array.length];
/*
* when s < e , we can start merge if s== e ,we need not do anything
*/
// if (s == e) return ;
if (s < e) {
int m = (s + e) / 2;
MSort(array, s, m);
MSort(array, m + 1, e);
Merge(array, tmp, s, m, e);
}
// here alse can return array , just modify the return type to int array
// just like static int [] array
}
public static void MergSort(int[] array) {
show(array);
MSort(array, 0, array.length - 1);
show(array);
}
public static void show(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
int[] array = { 49, 38, 65, 97, 76, 13, 27 };
/* Scanner s = new Scanner(System.in);
int n = s.nextInt();
int [] arr = new int[n];
for(int i =0 ; i < arr.length; i++){
arr[i] = s.nextInt();
}
MergSort(arr);*/
MergSort(array);
}
}