Skip to content

Commit

Permalink
rename Order to SortDirection
Browse files Browse the repository at this point in the history
  • Loading branch information
justcoding121 committed Nov 9, 2018
1 parent d9bb857 commit d8a82b9
Show file tree
Hide file tree
Showing 38 changed files with 101 additions and 111 deletions.
20 changes: 10 additions & 10 deletions src/Advanced.Algorithms/DataStructures/Heap/BHeap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,30 @@ public class BHeap<T> : IEnumerable<T> where T : IComparable

public int Count { get; private set; }

public BHeap(Order order = Order.Ascending)
: this(order, null, null) { }
public BHeap(SortDirection sortDirection = SortDirection.Ascending)
: this(sortDirection, null, null) { }

public BHeap(Order order, IEnumerable<T> initial)
: this(order, initial, null) { }
public BHeap(SortDirection sortDirection, IEnumerable<T> initial)
: this(sortDirection, initial, null) { }

public BHeap(Order order, IComparer<T> comparer)
: this(order, null, comparer) { }
public BHeap(SortDirection sortDirection, IComparer<T> comparer)
: this(sortDirection, null, comparer) { }

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

if (comparer != null)
{
this.comparer = new CustomComparer<T>(order, comparer);
this.comparer = new CustomComparer<T>(sortDirection, comparer);
}
else
{
this.comparer = new CustomComparer<T>(order, Comparer<T>.Default);
this.comparer = new CustomComparer<T>(sortDirection, Comparer<T>.Default);
}

if (initial != null)
Expand Down
6 changes: 3 additions & 3 deletions src/Advanced.Algorithms/DataStructures/Heap/BinomialHeap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ private Dictionary<T, List<BinomialHeapNode<T>>> heapMapping

public int Count { get; private set; }

public BinomialHeap(Order order = Order.Ascending)
public BinomialHeap(SortDirection sortDirection = SortDirection.Ascending)
{
this.isMaxHeap = order == Order.Descending;
comparer = new CustomComparer<T>(order, Comparer<T>.Default);
this.isMaxHeap = sortDirection == SortDirection.Descending;
comparer = new CustomComparer<T>(sortDirection, Comparer<T>.Default);
}

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions src/Advanced.Algorithms/DataStructures/Heap/FibornacciHeap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ private Dictionary<T, List<FibornacciHeapNode<T>>> heapMapping

public int Count { get; private set; }

public FibornacciHeap(Order order = Order.Ascending)
public FibornacciHeap(SortDirection sortDirection = SortDirection.Ascending)
{
this.isMaxHeap = order == Order.Descending;
comparer = new CustomComparer<T>(order, Comparer<T>.Default);
this.isMaxHeap = sortDirection == SortDirection.Descending;
comparer = new CustomComparer<T>(sortDirection, Comparer<T>.Default);
}

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions src/Advanced.Algorithms/DataStructures/Heap/PairingHeap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ private Dictionary<T, List<PairingHeapNode<T>>> heapMapping

public int Count { get; private set; }

public PairingHeap(Order order = Order.Ascending)
public PairingHeap(SortDirection sortDirection = SortDirection.Ascending)
{
this.isMaxHeap = order == Order.Descending;
comparer = new CustomComparer<T>(order, Comparer<T>.Default);
this.isMaxHeap = sortDirection == SortDirection.Descending;
comparer = new CustomComparer<T>(sortDirection, Comparer<T>.Default);
}

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions src/Advanced.Algorithms/DataStructures/Heap/d-aryHeap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ public class DaryHeap<T> : IEnumerable<T> where T : IComparable
/// </summary>
/// <param name="k">The number of children per heap node.</param>
/// <param name="initial">The initial items if any.</param>
public DaryHeap(int k, Order order = Order.Ascending, IEnumerable<T> initial = null)
public DaryHeap(int k, SortDirection sortDirection = SortDirection.Ascending, IEnumerable<T> initial = null)
{
this.isMaxHeap = order == Order.Descending;
comparer = new CustomComparer<T>(order, Comparer<T>.Default);
this.isMaxHeap = sortDirection == SortDirection.Descending;
comparer = new CustomComparer<T>(sortDirection, Comparer<T>.Default);

if (k <= 2)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ namespace Advanced.Algorithms.DataStructures
public class PriorityQueue<T> : IEnumerable<T> where T : IComparable
{
private readonly BHeap<T> heap;
public PriorityQueue(Order order = Order.Ascending)
public PriorityQueue(SortDirection sortDirection = SortDirection.Ascending)
{
heap = new BHeap<T>(order);
heap = new BHeap<T>(sortDirection);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Advanced.Algorithms/Geometry/BentleyOttmann.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private void initialize(IEnumerable<Line> lineSegments)
x.Value
}), new PointComparer());

eventQueue = new BHeap<Event>(Order.Ascending, eventQueueLookUp, new EventQueueComparer());
eventQueue = new BHeap<Event>(SortDirection.Ascending, eventQueueLookUp, new EventQueueComparer());
}

public Dictionary<Point, List<Line>> FindIntersections(IEnumerable<Line> lineSegments)
Expand Down
2 changes: 1 addition & 1 deletion src/Advanced.Algorithms/Search/QuickSelect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private static T medianOfMedian(T[] input, int left, int right)
return input[left];
}

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

var size = 5;
var currentLeft = left;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ internal class CustomComparer<T> : IComparer<T> where T : IComparable
private readonly bool isMax;
private readonly IComparer<T> comparer;

internal CustomComparer(Order order, IComparer<T> comparer)
internal CustomComparer(SortDirection sortDirection, IComparer<T> comparer)
{
this.isMax = order == Order.Descending;
this.isMax = sortDirection == SortDirection.Descending;
this.comparer = comparer;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Advanced.Algorithms
{
public enum Order
public enum SortDirection
{
Ascending = 0,
Descending = 1
Expand Down
4 changes: 2 additions & 2 deletions src/Advanced.Algorithms/Sorting/BubbleSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public class BubbleSort<T> where T : IComparable
/// <summary>
/// Time complexity: O(n^2).
/// </summary>
public static T[] Sort(T[] array, Order order = Order.Ascending)
public static T[] Sort(T[] array, SortDirection sortDirection = SortDirection.Ascending)
{
var comparer = new CustomComparer<T>(order, Comparer<T>.Default);
var comparer = new CustomComparer<T>(sortDirection, Comparer<T>.Default);
var swapped = true;

while (swapped)
Expand Down
6 changes: 3 additions & 3 deletions src/Advanced.Algorithms/Sorting/BucketSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class BucketSort
/// <summary>
/// Sort given integers using bucket sort with merge sort as sub sort.
/// </summary>
public static int[] Sort(int[] array, int bucketSize, Order order = Order.Ascending)
public static int[] Sort(int[] array, int bucketSize, SortDirection sortDirection = SortDirection.Ascending)
{
if (bucketSize < 0 || bucketSize > array.Length)
{
Expand Down Expand Up @@ -45,13 +45,13 @@ public static int[] Sort(int[] array, int bucketSize, Order order = Order.Ascend
foreach (var bucket in buckets.ToList())
{
buckets[bucket.Key] = new List<int>(MergeSort<int>
.Sort(bucket.Value.ToArray(), order));
.Sort(bucket.Value.ToArray(), sortDirection));

bucketKeys[i] = bucket.Key;
i++;
}

bucketKeys = MergeSort<int>.Sort(bucketKeys, order);
bucketKeys = MergeSort<int>.Sort(bucketKeys, sortDirection);

var result = new int[array.Length];

Expand Down
21 changes: 13 additions & 8 deletions src/Advanced.Algorithms/Sorting/CountingSort.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

namespace Advanced.Algorithms.Sorting
{
Expand All @@ -10,9 +11,12 @@ public class CountingSort
/// <summary>
/// Sort given integers.
/// </summary>
public static int[] Sort(int[] array, Order order = Order.Ascending)
public static int[] Sort(IEnumerable<int> array, SortDirection sortDirection = SortDirection.Ascending)
{
var max = getMax(array);
var lengthAndMax = getLengthAndMax(array);

var length = lengthAndMax.Item1;
var max = lengthAndMax.Item2;

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

var result = new int[array.Length];
var result = new int[length];

//now assign result
foreach (var item in array)
{
var index = countArray[item];
result[order == Order.Ascending ? index-1 : result.Length - index] = item;
result[sortDirection == SortDirection.Ascending ? index-1 : result.Length - index] = item;
countArray[item]--;
}

Expand All @@ -52,19 +56,20 @@ public static int[] Sort(int[] array, Order order = Order.Ascending)
/// <summary>
/// Get Max of given array.
/// </summary>
private static int getMax(int[] array)
private static Tuple<int, int> getLengthAndMax(IEnumerable<int> array)
{
var length = 0;
var max = int.MinValue;

foreach (var item in array)
{
length++;
if (item.CompareTo(max) > 0)
{
max = item;
}
}

return max;
return new Tuple<int, int>(length, max);
}
}
}
8 changes: 5 additions & 3 deletions src/Advanced.Algorithms/Sorting/HeapSort.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Advanced.Algorithms.DataStructures;

namespace Advanced.Algorithms.Sorting
Expand All @@ -11,13 +13,13 @@ public class HeapSort<T> where T : IComparable
/// <summary>
/// Time complexity: O(nlog(n)).
/// </summary>
public static T[] Sort(T[] array, Order order = Order.Ascending)
public static T[] Sort(ICollection<T> collection, SortDirection sortDirection = SortDirection.Ascending)
{
//heapify
var heap = new BHeap<T>(order, array);
var heap = new BHeap<T>(sortDirection, collection);

//now extract min until empty and return them as sorted array
var sortedArray = new T[array.Length];
var sortedArray = new T[collection.Count];
var j = 0;
while (heap.Count > 0)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Advanced.Algorithms/Sorting/InsertionSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public class InsertionSort<T> where T : IComparable
/// <summary>
/// Time complexity: O(n^2).
/// </summary>
public static T[] Sort(T[] array, Order order = Order.Ascending)
public static T[] Sort(T[] array, SortDirection sortDirection = SortDirection.Ascending)
{
var comparer = new CustomComparer<T>(order, Comparer<T>.Default);
var comparer = new CustomComparer<T>(sortDirection, Comparer<T>.Default);

for (int i = 0; i < array.Length - 1; i++)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Advanced.Algorithms/Sorting/MergeSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public class MergeSort<T> where T : IComparable
/// <summary>
/// Time complexity: O(nlog(n)).
/// </summary>
public static T[] Sort(T[] array, Order order = Order.Ascending)
public static T[] Sort(T[] array, SortDirection sortDirection = SortDirection.Ascending)
{
PartitionMerge(array, 0, array.Length - 1, new CustomComparer<T>(order, Comparer<T>.Default));
PartitionMerge(array, 0, array.Length - 1, new CustomComparer<T>(sortDirection, Comparer<T>.Default));
return array;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Advanced.Algorithms/Sorting/QuickSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ public class QuickSort<T> where T : IComparable
/// <summary>
/// Time complexity: O(n^2)
/// </summary>
public static T[] Sort(T[] array, Order order = Order.Ascending)
public static T[] Sort(T[] array, SortDirection sortDirection = SortDirection.Ascending)
{
if (array.Length <= 1)
{
return array;
}

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

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

Expand Down
23 changes: 3 additions & 20 deletions src/Advanced.Algorithms/Sorting/RadixSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Advanced.Algorithms.Sorting
/// </summary>
public class RadixSort
{
public static int[] Sort(int[] array, Order order = Order.Ascending)
public static int[] Sort(int[] array, SortDirection sortDirection = SortDirection.Ascending)
{
int i;
for (i = 0; i < array.Length; i++)
Expand All @@ -20,7 +20,7 @@ public static int[] Sort(int[] array, Order order = Order.Ascending)
}

var @base = 1;
var max = getMax(array);
var max = array.Max();


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

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

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

/// <summary>
/// Get Max of given array.
/// </summary>
private static int getMax(int[] array)
{
var max = int.MinValue;

foreach (var item in array)
{
if (item.CompareTo(max) > 0)
{
max = item;
}
}

return max;
}
}
}
4 changes: 2 additions & 2 deletions src/Advanced.Algorithms/Sorting/SelectionSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public class SelectionSort<T> where T : IComparable
/// <summary>
/// Time complexity: O(n^2).
/// </summary>
public static T[] Sort(T[] array, Order order = Order.Ascending)
public static T[] Sort(T[] array, SortDirection sortDirection = SortDirection.Ascending)
{
var comparer = new CustomComparer<T>(order, Comparer<T>.Default);
var comparer = new CustomComparer<T>(sortDirection, Comparer<T>.Default);

for (int i = 0; i < array.Length; i++)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Advanced.Algorithms/Sorting/ShellSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ namespace Advanced.Algorithms.Sorting
/// </summary>
public class ShellSort<T> where T : IComparable
{
public static T[] Sort(T[] array, Order order = Order.Ascending)
public static T[] Sort(T[] array, SortDirection sortDirection = SortDirection.Ascending)
{
var comparer = new CustomComparer<T>(order, Comparer<T>.Default);
var comparer = new CustomComparer<T>(sortDirection, Comparer<T>.Default);

var k = array.Length / 2;
var j = 0;
Expand Down
Loading

0 comments on commit d8a82b9

Please sign in to comment.