|
1 | 1 | package sorting;
|
2 | 2 |
|
3 | 3 | 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]; |
7 | 7 |
|
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] + " "); |
26 | 10 |
|
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 | + } |
69 | 69 | }
|
0 commit comments