Skip to content

Commit aca84b2

Browse files
committed
Sort descending option
1 parent 88c2b07 commit aca84b2

38 files changed

+487
-246
lines changed

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,37 @@ namespace Advanced.Algorithms.DataStructures
1010
/// </summary>
1111
public class BHeap<T> : IEnumerable<T> where T : IComparable
1212
{
13-
private readonly bool isMax;
13+
private readonly bool isMaxHeap;
1414

1515
private T[] heapArray;
1616
private readonly IComparer<T> comparer;
1717

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

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

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

26-
public BHeap(bool isMax, IComparer<T> comparer)
27-
: this(isMax, null, comparer) { }
26+
public BHeap(Order order, IComparer<T> comparer)
27+
: this(order, 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(bool isMax, IEnumerable<T> initial, IComparer<T> comparer)
33+
public BHeap(Order order, IEnumerable<T> initial, IComparer<T> comparer)
3434
{
35-
this.isMax = isMax;
35+
this.isMaxHeap = order == Order.Descending;
3636

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

4646
if (initial != null)

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Advanced.Algorithms.DataStructures
1010
/// </summary>
1111
public class BinomialHeap<T> : IEnumerable<T> where T : IComparable
1212
{
13-
private readonly bool isMax;
13+
private readonly bool isMaxHeap;
1414
private readonly IComparer<T> comparer;
1515

1616
private DoublyLinkedList<BinomialHeapNode<T>> heapForest
@@ -21,10 +21,10 @@ private Dictionary<T, List<BinomialHeapNode<T>>> heapMapping
2121

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

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

3030
/// <summary>
@@ -109,7 +109,7 @@ public void UpdateKey(T currentValue, T newValue)
109109

110110
if (comparer.Compare(newValue, node.Value) > 0)
111111
{
112-
throw new Exception($"New value is not {(!isMax ? "less" : "greater")} than old value.");
112+
throw new Exception($"New value is not {(!isMaxHeap ? "less" : "greater")} than old value.");
113113
}
114114

115115
updateNodeValue(currentValue, newValue, node);

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Advanced.Algorithms.DataStructures
1010
/// </summary>
1111
public class FibornacciHeap<T> : IEnumerable<T> where T : IComparable
1212
{
13-
private readonly bool isMax;
13+
private readonly bool isMaxHeap;
1414
private readonly IComparer<T> comparer;
1515

1616
//holds the min/max node at any given time
@@ -23,10 +23,10 @@ private Dictionary<T, List<FibornacciHeapNode<T>>> heapMapping
2323

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

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

3232
/// <summary>
@@ -97,7 +97,7 @@ public void UpdateKey(T currentValue, T newValue)
9797

9898
if (comparer.Compare(newValue, node.Value) > 0)
9999
{
100-
throw new Exception($"New value is not {(!isMax ? "less" : "greater")} than old value.");
100+
throw new Exception($"New value is not {(!isMaxHeap ? "less" : "greater")} than old value.");
101101
}
102102

103103
updateNodeValue(currentValue, newValue, node);

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Advanced.Algorithms.DataStructures
1010
/// </summary>
1111
public class PairingHeap<T> : IEnumerable<T> where T : IComparable
1212
{
13-
private readonly bool isMax;
13+
private readonly bool isMaxHeap;
1414
private readonly IComparer<T> comparer;
1515

1616
private PairingHeapNode<T> Root;
@@ -19,10 +19,10 @@ private Dictionary<T, List<PairingHeapNode<T>>> heapMapping
1919

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

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

2828
/// <summary>
@@ -63,7 +63,7 @@ public void UpdateKey(T currentValue, T newValue)
6363

6464
if (comparer.Compare(newValue, node.Value) > 0)
6565
{
66-
throw new Exception($"New value is not {(!isMax ? "less" : "greater")} than old value.");
66+
throw new Exception($"New value is not {(!isMaxHeap ? "less" : "greater")} than old value.");
6767
}
6868

6969
updateNodeValue(currentValue, newValue, node);

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Advanced.Algorithms.DataStructures
1010
/// </summary>
1111
public class DaryHeap<T> : IEnumerable<T> where T : IComparable
1212
{
13-
private readonly bool isMax;
13+
private readonly bool isMaxHeap;
1414
private readonly IComparer<T> comparer;
1515

1616
private T[] heapArray;
@@ -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, bool isMax = false, IEnumerable<T> initial = null)
25+
public DaryHeap(int k, Order order = Order.Ascending, IEnumerable<T> initial = null)
2626
{
27-
this.isMax = isMax;
28-
comparer = new HeapComparer<T>(isMax, Comparer<T>.Default);
27+
this.isMaxHeap = order == Order.Descending;
28+
comparer = new CustomComparer<T>(order, 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(bool isMax = false)
13+
public PriorityQueue(Order order = Order.Ascending)
1414
{
15-
heap = new BHeap<T>(isMax);
15+
heap = new BHeap<T>(order);
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>(false, eventQueueLookUp, new EventQueueComparer());
69+
eventQueue = new BHeap<Event>(Order.Ascending, eventQueueLookUp, new EventQueueComparer());
7070
}
7171

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

src/Advanced.Algorithms/Search/QuickSelect.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ 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);
64+
6365
var size = 5;
6466
var currentLeft = left;
6567

@@ -71,12 +73,12 @@ private static T medianOfMedian(T[] input, int left, int right)
7173

7274
if (currentRight <= right)
7375
{
74-
sort(input, currentLeft, currentRight);
76+
sort(input, currentLeft, currentRight, comparer);
7577
medians[++medianIndex] = median(input, currentLeft, currentRight);
7678
}
7779
else
7880
{
79-
sort(input, currentLeft, right);
81+
sort(input, currentLeft, right, comparer);
8082
medians[++medianIndex] = median(input, currentLeft, right);
8183
}
8284

@@ -116,9 +118,9 @@ private static int partition(T[] input, int left, int right, int pivot)
116118
return newPivot;
117119
}
118120

119-
private static void sort(T[] input, int left, int right)
121+
private static void sort(T[] input, int left, int right, CustomComparer<T> comparer)
120122
{
121-
MergeSort<T>.PartitionMerge(input, left, right);
123+
MergeSort<T>.PartitionMerge(input, left, right, comparer);
122124
}
123125

124126
private static T median(T[] input, int left, int right)
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
using System.Text;
55
using System.Threading.Tasks;
66

7-
namespace Advanced.Algorithms.DataStructures
7+
namespace Advanced.Algorithms
88
{
9-
internal class HeapComparer<T> : IComparer<T> where T : IComparable
9+
internal class CustomComparer<T> : IComparer<T> where T : IComparable
1010
{
1111
private readonly bool isMax;
1212
private readonly IComparer<T> comparer;
1313

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Advanced.Algorithms
8+
{
9+
public enum Order
10+
{
11+
Ascending = 0,
12+
Descending = 1
13+
}
14+
}

0 commit comments

Comments
 (0)