@@ -11,37 +11,27 @@ public static List<T> MergeSort<T>(this List<T> collection, Comparer<T> comparer
1111 {
1212 comparer = comparer ?? Comparer < T > . Default ;
1313
14- return InternalMergeSort ( collection , 0 , collection . Count - 1 , comparer ) ;
14+ return InternalMergeSort ( collection , comparer ) ;
1515 }
1616
1717
1818 //
1919 // Private static method
2020 // Implements the recursive merge-sort algorithm
21- private static List < T > InternalMergeSort < T > ( List < T > collection , int startIndex , int endIndex , Comparer < T > comparer )
21+ private static List < T > InternalMergeSort < T > ( List < T > collection , Comparer < T > comparer )
2222 {
2323 if ( collection . Count < 2 )
2424 {
2525 return collection ;
2626 }
2727
28- if ( collection . Count == 2 )
29- {
30- if ( comparer . Compare ( collection [ endIndex ] , collection [ startIndex ] ) < 0 )
31- {
32- collection . Swap ( endIndex , startIndex ) ;
33- }
34-
35- return collection ;
36- }
37-
3828 int midIndex = collection . Count / 2 ;
3929
40- var leftCollection = collection . GetRange ( startIndex , midIndex ) ;
41- var rightCollection = collection . GetRange ( midIndex , ( endIndex - midIndex ) + 1 ) ;
30+ var leftCollection = collection . GetRange ( 0 , midIndex ) ;
31+ var rightCollection = collection . GetRange ( midIndex , collection . Count - midIndex ) ;
4232
43- leftCollection = InternalMergeSort < T > ( leftCollection , 0 , leftCollection . Count - 1 , comparer ) ;
44- rightCollection = InternalMergeSort < T > ( rightCollection , 0 , rightCollection . Count - 1 , comparer ) ;
33+ leftCollection = InternalMergeSort < T > ( leftCollection , comparer ) ;
34+ rightCollection = InternalMergeSort < T > ( rightCollection , comparer ) ;
4535
4636 return InternalMerge < T > ( leftCollection , rightCollection , comparer ) ;
4737 }
@@ -59,42 +49,31 @@ private static List<T> InternalMerge<T>(List<T> leftCollection, List<T> rightCol
5949
6050 List < T > result = new List < T > ( length ) ;
6151
62- for ( index = 0 ; index < length ; ++ index )
52+ for ( index = 0 ; right < rightCollection . Count && left < leftCollection . Count ; ++ index )
6353 {
64- if ( right < rightCollection . Count && comparer . Compare ( rightCollection [ right ] , leftCollection [ left ] ) <= 0 ) // rightElement <= leftElement
54+ if ( comparer . Compare ( rightCollection [ right ] , leftCollection [ left ] ) <= 0 ) // rightElement <= leftElement
6555 {
6656 //resultArray.Add(rightCollection[right]);
67- result . Insert ( index , rightCollection [ right ] ) ;
68- right ++ ;
57+ result . Insert ( index , rightCollection [ right ++ ] ) ;
6958 }
7059 else
7160 {
7261 //result.Add(leftCollection[left]);
73- result . Insert ( index , leftCollection [ left ] ) ;
74- left ++ ;
75-
76- if ( left == leftCollection . Count )
77- break ;
62+ result . Insert ( index , leftCollection [ left ++ ] ) ;
7863 }
7964 }
8065
8166 //
82- // Either one might have elements left
83- int rIndex = index + 1 ;
84- int lIndex = index + 1 ;
85-
67+ // At most one of left and right might still have elements left
68+
8669 while ( right < rightCollection . Count )
8770 {
88- result . Insert ( rIndex , rightCollection [ right ] ) ;
89- rIndex ++ ;
90- right ++ ;
71+ result . Insert ( index ++ , rightCollection [ right ++ ] ) ;
9172 }
9273
9374 while ( left < leftCollection . Count )
9475 {
95- result . Insert ( lIndex , leftCollection [ left ] ) ;
96- lIndex ++ ;
97- left ++ ;
76+ result . Insert ( index ++ , leftCollection [ left ++ ] ) ;
9877 }
9978
10079 return result ;
0 commit comments