@@ -11,37 +11,27 @@ public static List<T> MergeSort<T>(this List<T> collection, Comparer<T> comparer
11
11
{
12
12
comparer = comparer ?? Comparer < T > . Default ;
13
13
14
- return InternalMergeSort ( collection , 0 , collection . Count - 1 , comparer ) ;
14
+ return InternalMergeSort ( collection , comparer ) ;
15
15
}
16
16
17
17
18
18
//
19
19
// Private static method
20
20
// 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 )
22
22
{
23
23
if ( collection . Count < 2 )
24
24
{
25
25
return collection ;
26
26
}
27
27
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
-
38
28
int midIndex = collection . Count / 2 ;
39
29
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 ) ;
42
32
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 ) ;
45
35
46
36
return InternalMerge < T > ( leftCollection , rightCollection , comparer ) ;
47
37
}
@@ -59,42 +49,31 @@ private static List<T> InternalMerge<T>(List<T> leftCollection, List<T> rightCol
59
49
60
50
List < T > result = new List < T > ( length ) ;
61
51
62
- for ( index = 0 ; index < length ; ++ index )
52
+ for ( index = 0 ; right < rightCollection . Count && left < leftCollection . Count ; ++ index )
63
53
{
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
65
55
{
66
56
//resultArray.Add(rightCollection[right]);
67
- result . Insert ( index , rightCollection [ right ] ) ;
68
- right ++ ;
57
+ result . Insert ( index , rightCollection [ right ++ ] ) ;
69
58
}
70
59
else
71
60
{
72
61
//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 ++ ] ) ;
78
63
}
79
64
}
80
65
81
66
//
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
+
86
69
while ( right < rightCollection . Count )
87
70
{
88
- result . Insert ( rIndex , rightCollection [ right ] ) ;
89
- rIndex ++ ;
90
- right ++ ;
71
+ result . Insert ( index ++ , rightCollection [ right ++ ] ) ;
91
72
}
92
73
93
74
while ( left < leftCollection . Count )
94
75
{
95
- result . Insert ( lIndex , leftCollection [ left ] ) ;
96
- lIndex ++ ;
97
- left ++ ;
76
+ result . Insert ( index ++ , leftCollection [ left ++ ] ) ;
98
77
}
99
78
100
79
return result ;
0 commit comments