File tree Expand file tree Collapse file tree
src/com/codefortomorrow/advanced/chapter13 Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .codefortomorrow .advanced .chapter13 .practice ;
2+
3+ public class RecurMergeSort {
4+ public static void recurMergeSort (Comparable [] array , int start , int end ) {
5+
6+ }
7+ }
Original file line number Diff line number Diff line change 1+ package com .codefortomorrow .advanced .chapter13 .solutions ;
2+
3+ public class RecurMergeSort {
4+ public static void recurMergeSort (Comparable [] array , int start , int end ) {
5+ if (start >= end ) {
6+ return ;
7+ }
8+ int middle = (start + end ) / 2 ;
9+ recurMergeSort (array , start , middle );
10+ recurMergeSort (array , middle + 1 , end );
11+ int i = start ;
12+ int j = middle + 1 ;
13+ Comparable [] sortedArray = new Comparable [end - start + 1 ];
14+ int k = 0 ;
15+ while (i <= middle && j <= end ) {
16+ if (array [i ].compareTo (array [j ]) > 0 ) {
17+ sortedArray [k ] = array [i ];
18+ i ++;
19+ } else {
20+ sortedArray [k ] = array [j ];
21+ j ++;
22+ }
23+ k ++;
24+ }
25+ while (i <= middle ) {
26+ sortedArray [k ] = array [i ];
27+ i ++;
28+ k ++;
29+ }
30+ while (j <= end ) {
31+ sortedArray [k ] = array [j ];
32+ j ++;
33+ k ++;
34+ }
35+ k = 0 ;
36+ for (int l = start ; l <= end ; l ++) {
37+ array [l ] = sortedArray [k ];
38+ k ++;
39+ }
40+ }
41+ }
You can’t perform that action at this time.
0 commit comments