Skip to content

Commit d8a82b9

Browse files
committed
rename Order to SortDirection
1 parent d9bb857 commit d8a82b9

38 files changed

+101
-111
lines changed

src/Advanced.Algorithms/DataStructures/Heap/BHeap.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,30 @@ public class BHeap<T> : IEnumerable<T> where T : IComparable
1717

1818
public int Count { get; private set; }
1919

20-
public BHeap(Order order = Order.Ascending)
21-
: this(order, null, null) { }
20+
public BHeap(SortDirection sortDirection = SortDirection.Ascending)
21+
: this(sortDirection, null, null) { }
2222

23-
public BHeap(Order order, IEnumerable<T> initial)
24-
: this(order, initial, null) { }
23+
public BHeap(SortDirection sortDirection, IEnumerable<T> initial)
24+
: this(sortDirection, initial, null) { }
2525

26-
public BHeap(Order order, IComparer<T> comparer)
27-
: this(order, null, comparer) { }
26+
public BHeap(SortDirection sortDirection, IComparer<T> comparer)
27+
: this(sortDirection, null, comparer) { }
2828

2929
/// <summary>
3030
/// Time complexity: O(n) if initial is provided. Otherwise O(1).
3131
/// </summary>
3232
/// <param name="initial">The initial items in the heap.</param>
33-
public BHeap(Order order, IEnumerable<T> initial, IComparer<T> comparer)
33+
public BHeap(SortDirection sortDirection, IEnumerable<T> initial, IComparer<T> comparer)
3434
{
35-
this.isMaxHeap = order == Order.Descending;
35+
this.isMaxHeap = sortDirection == SortDirection.Descending;
3636

3737
if (comparer != null)
3838
{
39-
this.comparer = new CustomComparer<T>(order, comparer);
39+
this.comparer = new CustomComparer<T>(sortDirection, comparer);
4040
}
4141
else
4242
{
43-
this.comparer = new CustomComparer<T>(order, Comparer<T>.Default);
43+
this.comparer = new CustomComparer<T>(sortDirection, Comparer<T>.Default);
4444
}
4545

4646
if (initial != null)

src/Advanced.Algorithms/DataStructures/Heap/BinomialHeap.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ private Dictionary<T, List<BinomialHeapNode<T>>> heapMapping
2121

2222
public int Count { get; private set; }
2323

24-
public BinomialHeap(Order order = Order.Ascending)
24+
public BinomialHeap(SortDirection sortDirection = SortDirection.Ascending)
2525
{
26-
this.isMaxHeap = order == Order.Descending;
27-
comparer = new CustomComparer<T>(order, Comparer<T>.Default);
26+
this.isMaxHeap = sortDirection == SortDirection.Descending;
27+
comparer = new CustomComparer<T>(sortDirection, Comparer<T>.Default);
2828
}
2929

3030
/// <summary>

src/Advanced.Algorithms/DataStructures/Heap/FibornacciHeap.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ private Dictionary<T, List<FibornacciHeapNode<T>>> heapMapping
2323

2424
public int Count { get; private set; }
2525

26-
public FibornacciHeap(Order order = Order.Ascending)
26+
public FibornacciHeap(SortDirection sortDirection = SortDirection.Ascending)
2727
{
28-
this.isMaxHeap = order == Order.Descending;
29-
comparer = new CustomComparer<T>(order, Comparer<T>.Default);
28+
this.isMaxHeap = sortDirection == SortDirection.Descending;
29+
comparer = new CustomComparer<T>(sortDirection, Comparer<T>.Default);
3030
}
3131

3232
/// <summary>

src/Advanced.Algorithms/DataStructures/Heap/PairingHeap.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ private Dictionary<T, List<PairingHeapNode<T>>> heapMapping
1919

2020
public int Count { get; private set; }
2121

22-
public PairingHeap(Order order = Order.Ascending)
22+
public PairingHeap(SortDirection sortDirection = SortDirection.Ascending)
2323
{
24-
this.isMaxHeap = order == Order.Descending;
25-
comparer = new CustomComparer<T>(order, Comparer<T>.Default);
24+
this.isMaxHeap = sortDirection == SortDirection.Descending;
25+
comparer = new CustomComparer<T>(sortDirection, Comparer<T>.Default);
2626
}
2727

2828
/// <summary>

src/Advanced.Algorithms/DataStructures/Heap/d-aryHeap.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ public class DaryHeap<T> : IEnumerable<T> where T : IComparable
2222
/// </summary>
2323
/// <param name="k">The number of children per heap node.</param>
2424
/// <param name="initial">The initial items if any.</param>
25-
public DaryHeap(int k, Order order = Order.Ascending, IEnumerable<T> initial = null)
25+
public DaryHeap(int k, SortDirection sortDirection = SortDirection.Ascending, IEnumerable<T> initial = null)
2626
{
27-
this.isMaxHeap = order == Order.Descending;
28-
comparer = new CustomComparer<T>(order, Comparer<T>.Default);
27+
this.isMaxHeap = sortDirection == SortDirection.Descending;
28+
comparer = new CustomComparer<T>(sortDirection, Comparer<T>.Default);
2929

3030
if (k <= 2)
3131
{

src/Advanced.Algorithms/DataStructures/Queues/PriorityQueue.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ namespace Advanced.Algorithms.DataStructures
1010
public class PriorityQueue<T> : IEnumerable<T> where T : IComparable
1111
{
1212
private readonly BHeap<T> heap;
13-
public PriorityQueue(Order order = Order.Ascending)
13+
public PriorityQueue(SortDirection sortDirection = SortDirection.Ascending)
1414
{
15-
heap = new BHeap<T>(order);
15+
heap = new BHeap<T>(sortDirection);
1616
}
1717

1818
/// <summary>

src/Advanced.Algorithms/Geometry/BentleyOttmann.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private void initialize(IEnumerable<Line> lineSegments)
6666
x.Value
6767
}), new PointComparer());
6868

69-
eventQueue = new BHeap<Event>(Order.Ascending, eventQueueLookUp, new EventQueueComparer());
69+
eventQueue = new BHeap<Event>(SortDirection.Ascending, eventQueueLookUp, new EventQueueComparer());
7070
}
7171

7272
public Dictionary<Point, List<Line>> FindIntersections(IEnumerable<Line> lineSegments)

src/Advanced.Algorithms/Search/QuickSelect.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private static T medianOfMedian(T[] input, int left, int right)
6060
return input[left];
6161
}
6262

63-
var comparer = new CustomComparer<T>(Order.Ascending, Comparer<T>.Default);
63+
var comparer = new CustomComparer<T>(SortDirection.Ascending, Comparer<T>.Default);
6464

6565
var size = 5;
6666
var currentLeft = left;

src/Advanced.Algorithms/Shared/Comparer.cs renamed to src/Advanced.Algorithms/Shared/CustomComparer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ internal class CustomComparer<T> : IComparer<T> where T : IComparable
1111
private readonly bool isMax;
1212
private readonly IComparer<T> comparer;
1313

14-
internal CustomComparer(Order order, IComparer<T> comparer)
14+
internal CustomComparer(SortDirection sortDirection, IComparer<T> comparer)
1515
{
16-
this.isMax = order == Order.Descending;
16+
this.isMax = sortDirection == SortDirection.Descending;
1717
this.comparer = comparer;
1818
}
1919

src/Advanced.Algorithms/Shared/Order.cs renamed to src/Advanced.Algorithms/Shared/SortDirection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace Advanced.Algorithms
88
{
9-
public enum Order
9+
public enum SortDirection
1010
{
1111
Ascending = 0,
1212
Descending = 1

src/Advanced.Algorithms/Sorting/BubbleSort.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ public class BubbleSort<T> where T : IComparable
1111
/// <summary>
1212
/// Time complexity: O(n^2).
1313
/// </summary>
14-
public static T[] Sort(T[] array, Order order = Order.Ascending)
14+
public static T[] Sort(T[] array, SortDirection sortDirection = SortDirection.Ascending)
1515
{
16-
var comparer = new CustomComparer<T>(order, Comparer<T>.Default);
16+
var comparer = new CustomComparer<T>(sortDirection, Comparer<T>.Default);
1717
var swapped = true;
1818

1919
while (swapped)

src/Advanced.Algorithms/Sorting/BucketSort.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class BucketSort
1313
/// <summary>
1414
/// Sort given integers using bucket sort with merge sort as sub sort.
1515
/// </summary>
16-
public static int[] Sort(int[] array, int bucketSize, Order order = Order.Ascending)
16+
public static int[] Sort(int[] array, int bucketSize, SortDirection sortDirection = SortDirection.Ascending)
1717
{
1818
if (bucketSize < 0 || bucketSize > array.Length)
1919
{
@@ -45,13 +45,13 @@ public static int[] Sort(int[] array, int bucketSize, Order order = Order.Ascend
4545
foreach (var bucket in buckets.ToList())
4646
{
4747
buckets[bucket.Key] = new List<int>(MergeSort<int>
48-
.Sort(bucket.Value.ToArray(), order));
48+
.Sort(bucket.Value.ToArray(), sortDirection));
4949

5050
bucketKeys[i] = bucket.Key;
5151
i++;
5252
}
5353

54-
bucketKeys = MergeSort<int>.Sort(bucketKeys, order);
54+
bucketKeys = MergeSort<int>.Sort(bucketKeys, sortDirection);
5555

5656
var result = new int[array.Length];
5757

src/Advanced.Algorithms/Sorting/CountingSort.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23

34
namespace Advanced.Algorithms.Sorting
45
{
@@ -10,9 +11,12 @@ public class CountingSort
1011
/// <summary>
1112
/// Sort given integers.
1213
/// </summary>
13-
public static int[] Sort(int[] array, Order order = Order.Ascending)
14+
public static int[] Sort(IEnumerable<int> array, SortDirection sortDirection = SortDirection.Ascending)
1415
{
15-
var max = getMax(array);
16+
var lengthAndMax = getLengthAndMax(array);
17+
18+
var length = lengthAndMax.Item1;
19+
var max = lengthAndMax.Item2;
1620

1721
//add one more space for zero
1822
var countArray = new int[max + 1];
@@ -36,13 +40,13 @@ public static int[] Sort(int[] array, Order order = Order.Ascending)
3640
countArray[i] = sum;
3741
}
3842

39-
var result = new int[array.Length];
43+
var result = new int[length];
4044

4145
//now assign result
4246
foreach (var item in array)
4347
{
4448
var index = countArray[item];
45-
result[order == Order.Ascending ? index-1 : result.Length - index] = item;
49+
result[sortDirection == SortDirection.Ascending ? index-1 : result.Length - index] = item;
4650
countArray[item]--;
4751
}
4852

@@ -52,19 +56,20 @@ public static int[] Sort(int[] array, Order order = Order.Ascending)
5256
/// <summary>
5357
/// Get Max of given array.
5458
/// </summary>
55-
private static int getMax(int[] array)
59+
private static Tuple<int, int> getLengthAndMax(IEnumerable<int> array)
5660
{
61+
var length = 0;
5762
var max = int.MinValue;
58-
5963
foreach (var item in array)
6064
{
65+
length++;
6166
if (item.CompareTo(max) > 0)
6267
{
6368
max = item;
6469
}
6570
}
6671

67-
return max;
72+
return new Tuple<int, int>(length, max);
6873
}
6974
}
7075
}

src/Advanced.Algorithms/Sorting/HeapSort.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
24
using Advanced.Algorithms.DataStructures;
35

46
namespace Advanced.Algorithms.Sorting
@@ -11,13 +13,13 @@ public class HeapSort<T> where T : IComparable
1113
/// <summary>
1214
/// Time complexity: O(nlog(n)).
1315
/// </summary>
14-
public static T[] Sort(T[] array, Order order = Order.Ascending)
16+
public static T[] Sort(ICollection<T> collection, SortDirection sortDirection = SortDirection.Ascending)
1517
{
1618
//heapify
17-
var heap = new BHeap<T>(order, array);
19+
var heap = new BHeap<T>(sortDirection, collection);
1820

1921
//now extract min until empty and return them as sorted array
20-
var sortedArray = new T[array.Length];
22+
var sortedArray = new T[collection.Count];
2123
var j = 0;
2224
while (heap.Count > 0)
2325
{

src/Advanced.Algorithms/Sorting/InsertionSort.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ public class InsertionSort<T> where T : IComparable
1111
/// <summary>
1212
/// Time complexity: O(n^2).
1313
/// </summary>
14-
public static T[] Sort(T[] array, Order order = Order.Ascending)
14+
public static T[] Sort(T[] array, SortDirection sortDirection = SortDirection.Ascending)
1515
{
16-
var comparer = new CustomComparer<T>(order, Comparer<T>.Default);
16+
var comparer = new CustomComparer<T>(sortDirection, Comparer<T>.Default);
1717

1818
for (int i = 0; i < array.Length - 1; i++)
1919
{

src/Advanced.Algorithms/Sorting/MergeSort.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ public class MergeSort<T> where T : IComparable
1111
/// <summary>
1212
/// Time complexity: O(nlog(n)).
1313
/// </summary>
14-
public static T[] Sort(T[] array, Order order = Order.Ascending)
14+
public static T[] Sort(T[] array, SortDirection sortDirection = SortDirection.Ascending)
1515
{
16-
PartitionMerge(array, 0, array.Length - 1, new CustomComparer<T>(order, Comparer<T>.Default));
16+
PartitionMerge(array, 0, array.Length - 1, new CustomComparer<T>(sortDirection, Comparer<T>.Default));
1717
return array;
1818
}
1919

src/Advanced.Algorithms/Sorting/QuickSort.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ public class QuickSort<T> where T : IComparable
1111
/// <summary>
1212
/// Time complexity: O(n^2)
1313
/// </summary>
14-
public static T[] Sort(T[] array, Order order = Order.Ascending)
14+
public static T[] Sort(T[] array, SortDirection sortDirection = SortDirection.Ascending)
1515
{
1616
if (array.Length <= 1)
1717
{
1818
return array;
1919
}
2020

21-
var comparer = new CustomComparer<T>(order, Comparer<T>.Default);
21+
var comparer = new CustomComparer<T>(sortDirection, Comparer<T>.Default);
2222

2323
sort(array, 0, array.Length - 1, comparer);
2424

src/Advanced.Algorithms/Sorting/RadixSort.cs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Advanced.Algorithms.Sorting
88
/// </summary>
99
public class RadixSort
1010
{
11-
public static int[] Sort(int[] array, Order order = Order.Ascending)
11+
public static int[] Sort(int[] array, SortDirection sortDirection = SortDirection.Ascending)
1212
{
1313
int i;
1414
for (i = 0; i < array.Length; i++)
@@ -20,7 +20,7 @@ public static int[] Sort(int[] array, Order order = Order.Ascending)
2020
}
2121

2222
var @base = 1;
23-
var max = getMax(array);
23+
var max = array.Max();
2424

2525

2626
while (max / @base > 0)
@@ -41,7 +41,7 @@ public static int[] Sort(int[] array, Order order = Order.Ascending)
4141
}
4242

4343
//now update array with what is in buckets
44-
var orderedBuckets = order == Order.Ascending ?
44+
var orderedBuckets = sortDirection == SortDirection.Ascending ?
4545
buckets : buckets.Reverse();
4646

4747
i = 0;
@@ -60,22 +60,5 @@ public static int[] Sort(int[] array, Order order = Order.Ascending)
6060
return array;
6161
}
6262

63-
/// <summary>
64-
/// Get Max of given array.
65-
/// </summary>
66-
private static int getMax(int[] array)
67-
{
68-
var max = int.MinValue;
69-
70-
foreach (var item in array)
71-
{
72-
if (item.CompareTo(max) > 0)
73-
{
74-
max = item;
75-
}
76-
}
77-
78-
return max;
79-
}
8063
}
8164
}

src/Advanced.Algorithms/Sorting/SelectionSort.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ public class SelectionSort<T> where T : IComparable
1111
/// <summary>
1212
/// Time complexity: O(n^2).
1313
/// </summary>
14-
public static T[] Sort(T[] array, Order order = Order.Ascending)
14+
public static T[] Sort(T[] array, SortDirection sortDirection = SortDirection.Ascending)
1515
{
16-
var comparer = new CustomComparer<T>(order, Comparer<T>.Default);
16+
var comparer = new CustomComparer<T>(sortDirection, Comparer<T>.Default);
1717

1818
for (int i = 0; i < array.Length; i++)
1919
{

src/Advanced.Algorithms/Sorting/ShellSort.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ namespace Advanced.Algorithms.Sorting
88
/// </summary>
99
public class ShellSort<T> where T : IComparable
1010
{
11-
public static T[] Sort(T[] array, Order order = Order.Ascending)
11+
public static T[] Sort(T[] array, SortDirection sortDirection = SortDirection.Ascending)
1212
{
13-
var comparer = new CustomComparer<T>(order, Comparer<T>.Default);
13+
var comparer = new CustomComparer<T>(sortDirection, Comparer<T>.Default);
1414

1515
var k = array.Length / 2;
1616
var j = 0;

0 commit comments

Comments
 (0)