Skip to content

Commit

Permalink
Merge pull request #887 from dotnet/UseArraySort
Browse files Browse the repository at this point in the history
Use Array.Sort instead of QuickSort.
  • Loading branch information
Oren Novotny authored Apr 22, 2019
2 parents 7c2d6a0 + f311d17 commit 5906af9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ public void OrderBy_Null()
Assert.Throws<ArgumentNullException>(() => AsyncEnumerable.ThenByDescending(xs, default, Comparer<int>.Default));
}

[Fact]
public async Task OrderBy_Empty()
{
var ys = AsyncEnumerable.Empty<int>().OrderBy(x => x);

var e = ys.GetAsyncEnumerator();
await NoNextAsync(e);
}

[Fact]
public async Task OrderBy1()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,66 +625,7 @@ internal sealed override int CompareAnyKeys(int index1, int index2)

private int CompareKeys(int index1, int index2) => index1 == index2 ? 0 : CompareAnyKeys(index1, index2);

protected override void QuickSort(int[] map, int left, int right)
{
// REVIEW: Consider using Array.Sort, see https://github.com/dotnet/corefx/commit/a6aff797a33e606a60ec0c9ca034a161c609620f#diff-d90239bd8284188e2bd210790483f5ca.

do
{
var i = left;
var j = right;
var x = map[i + ((j - i) >> 1)];

do
{
while (i < map.Length && CompareKeys(x, map[i]) > 0)
{
i++;
}

while (j >= 0 && CompareKeys(x, map[j]) < 0)
{
j--;
}

if (i > j)
{
break;
}

if (i < j)
{
var temp = map[i];
map[i] = map[j];
map[j] = temp;
}

i++;
j--;
}
while (i <= j);

if (j - left <= right - i)
{
if (left < j)
{
QuickSort(map, left, j);
}

left = i;
}
else
{
if (i < right)
{
QuickSort(map, i, right);
}

right = j;
}
}
while (left < right);
}
protected override void QuickSort(int[] keys, int lo, int hi) => Array.Sort(keys, lo, hi - lo + 1, Comparer<int>.Create(CompareAnyKeys));

protected override void PartialQuickSort(int[] map, int left, int right, int minIndexInclusive, int maxIndexInclusive)
{
Expand Down

0 comments on commit 5906af9

Please sign in to comment.