8000 format MergeSort · fishercoder1534/RandomJava@69675da · GitHub
[go: up one dir, main page]

Skip to content

Commit 69675da

Browse files
format MergeSort
1 parent 12cd557 commit 69675da

File tree

1 file changed

+63
-63
lines changed

1 file changed

+63
-63
lines changed

src/main/java/sorting/MergeSort.java

Lines changed: 63 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,69 @@
11
package sorting;
22

33
public class MergeSort {
4-
public static void main(String...strings){
5-
int a[] = new int[] { 9, 3, 8, 6, 2, 1, 5, 4};
6-
int b[] = new int[a.length];
4+
public static void main(String... strings) {
5+
int a[] = new int[]{9, 3, 8, 6, 2, 1, 5, 4};
6+
int b[] = new int[a.length];
77

8-
System.out.println("List before sorting\n");
9-
for (int i = 0; i < a.length; i++) System.out.print(a[i] + " ");
10-
11-
MergeSort test = new MergeSort();
12-
test.sort(a, 0, a.length-1);
13-
14-
System.out.println("\nList after sorting\n");
15-
for (int i = 0; i < a.length; i++) System.out.print(a[i] + " ");
16-
}
17-
18-
public void sort(int[] arr, int l, int r){
19-
if(l < r){
20-
int m = (l+r)/2;
21-
sort(arr, l, m);
22-
sort(arr, m+1, r);
23-
merge(arr, l, m, r);
24-
}
25-
}
8+
System.out.println("List before sorting\n");
9+
for (int i = 0; i < a.length; i++) System.out.print(a[i] + " ");
2610

27-
private void merge(int[] arr, int l, int m, int r) {
28-
//find sizes of two subarrays that are to be merged
29-
int size1 = m-l+1;
30-
int size2 = r-m;
31-
32-
//copy the two subarrays into two temp arrays
33-
int[] tempL = new int[size1];
34-
int[] tempR = new int[size2];
35-
for(int i = 0; i < size1; i++){
36-
tempL[i] = arr[l+i];
37-
}
38-
for(int i = 0; i < size2; i++){
39-
tempR[i] = arr[m+i+1];
40-
}
41-
42-
//now we merge the two subarrays
43-
44-
//initial indices of the two subarrays
45-
int i = 0, j = 0;
46-
47-
//initial index of the merged subarray array
48-
int k = l;
49-
50-
while(i < size1 && j < size2){
51-
if(tempL[i] <= tempR[j]){
52-
arr[k] = tempL[i];
53-
i++;
54-
} else {
55-
arr[k] = tempR[j];
56-
j++;
57-
}
58-
k++;
59-
}
60-
61-
//copy remaining list into arr if any
62-
while(i < size1){
63-
arr[k++] = tempL[i++];
64-
}
65-
while(j < size2){
66-
arr[k++] = tempR[j++];
67-
}
68-
}
11+
MergeSort test = new MergeSort();
12+
test.sort(a, 0, a.length - 1);
13+
14+
System.out.println("\nList after sorting\n");
15+
for (int i = 0; i < a.length; i++) System.out.print(a[i] + " ");
16+
}
17+
18+
public void sort(int[] arr, int l, int r) {
19+
if (l < r) {
20+
int m = (l + r) / 2;
21+
sort(arr, l, m);
22+
sort(arr, m + 1, r);
23+
merge(arr, l, m, r);
24+
}
25+
}
26+
27+
private void merge(int[] arr, int l, int m, int r) {
28+
//find sizes of two subarrays that are to be merged
29+
int size1 = m - l + 1;
30+
int size2 = r - m;
31+
32+
//copy the two subarrays into two temp arrays
33+
int[] tempL = new int[size1];
34+
int[] tempR = new int[size2];
35+
for (int i = 0; i < size1; i++) {
36+
tempL[i] = arr[l + i];
37+
}
38+
for (int i = 0; i < size2; i++) {
39+
tempR[i] = arr[m + i + 1];
40+
}
41+
42+
//now we merge the two subarrays
43+
44+
//initial indices of the two subarrays
45+
int i = 0, j = 0;
46+
47+
//initial index of the merged subarray array
48+
int k = l;
49+
50+
while (i < size1 && j < size2) {
51+
if (tempL[i] <= tempR[j]) {
52+
arr[k] = tempL[i];
53+
i++;
54+
} else {
55+
arr[k] = tempR[j];
56+
j++;
57+
}
58+
k++;
59+
}
60+
61+
//copy remaining list into arr if any
62+
while (i < size1) {
63+
arr[k++] = tempL[i++];
64+
} 5582
65+
while (j < size2) {
66+
arr[k++] = tempR[j++];
67+
}
68+
}
6969
}

0 commit comments

Comments
 (0)
0