aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorToby Vincent <tobyv13@gmail.com>2021-10-07 23:48:12 -0500
committerToby Vincent <tobyv13@gmail.com>2021-10-07 23:48:12 -0500
commitb579b1d3aa619ecb81d9761b739643673fea09d0 (patch)
tree2047a51f579c62fdc6ed176d83c8ac732089f7e1
parent1c687d6d3ed79afcba25a5c3dc3054bc7bcd99c4 (diff)
wrote extension methods for shallow copying and splitting arrays
-rw-r--r--src/ClosestPair/ClosestPair.cs11
-rw-r--r--src/ClosestPair/Extensions.cs26
-rw-r--r--src/ClosestPair/PointArray.cs30
-rw-r--r--src/MergeSort/Extensions.cs26
-rw-r--r--src/MergeSort/MergeSort.cs20
5 files changed, 67 insertions, 46 deletions
diff --git a/src/ClosestPair/ClosestPair.cs b/src/ClosestPair/ClosestPair.cs
index aa51106..441c40c 100644
--- a/src/ClosestPair/ClosestPair.cs
+++ b/src/ClosestPair/ClosestPair.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using Extensions;
namespace ClosestPair
{
@@ -29,19 +30,13 @@ namespace ClosestPair
return BruteForce(Px);
int middle = Px.Length / 2;
- Point[] Qx, Qy, Rx, Ry;
-
- Qx = Qy = new Point[middle];
- Rx = Ry = new Point[Px.Length - middle];
// Creates four lists, needs to be O(n log n)
// lists: Qx, Qy (first half of Px and Py)
// Rx, Ry (last half of Px and Py)
- Array.Copy(Px, Qx, Qx.Length);
- Array.Copy(Px, Qy, Qy.Length);
- Array.Copy(Px, middle, Rx, 0, Rx.Length);
- Array.Copy(Px, middle, Ry, 0, Ry.Length);
+ Px.Split(out Point[] Qx, out Point[] Rx);
+ Py.Split(out Point[] Qy, out Point[] Ry);
// calls ClosestPairRecursive on (Qx, Qy), (Rx, Ry) returns closest points in each set
PointPair qPair = ClosestPairRecursive(Qx, Qy);
diff --git a/src/ClosestPair/Extensions.cs b/src/ClosestPair/Extensions.cs
new file mode 100644
index 0000000..24bb43c
--- /dev/null
+++ b/src/ClosestPair/Extensions.cs
@@ -0,0 +1,26 @@
+using System;
+
+namespace Extensions
+{
+ static class ArrayExtensions
+ {
+ public static void Split<T>(this T[] array, int index, out T[] first, out T[] second)
+ {
+ first = new T[index];
+ second = new T[array.Length - index];
+
+ Array.Copy(array, first, first.Length);
+ Array.Copy(array, index, second, 0, second.Length);
+ }
+
+ public static void Split<T>(this T[] array, out T[] first, out T[] second)
+ => Split(array, array.Length / 2, out first, out second);
+
+ public static T[] Copy<T>(this T[] array)
+ {
+ T[] outArray = new T[array.Length];
+ Array.Copy(array, outArray, outArray.Length);
+ return outArray;
+ }
+ }
+} \ No newline at end of file
diff --git a/src/ClosestPair/PointArray.cs b/src/ClosestPair/PointArray.cs
index 698ed39..5c223dd 100644
--- a/src/ClosestPair/PointArray.cs
+++ b/src/ClosestPair/PointArray.cs
@@ -1,4 +1,4 @@
-using System;
+using Extensions;
using MergeSort;
namespace ClosestPair
@@ -15,33 +15,19 @@ namespace ClosestPair
SortedByX = new Point[array.Length];
SortedByY = new Point[array.Length];
- // SortedByX = isXSorted ? _points : SortByX();
- // SortedByY = isYSorted ? _points : SortByY();
- Array.Copy(array, _points, _points.Length);
- if (isXSorted)
- Array.Copy(array, SortedByX, SortedByX.Length);
- else
- {
- Point[] sorted = SortByX();
- Array.Copy(sorted, SortedByX, SortedByX.Length);
- }
-
- if (isYSorted)
- Array.Copy(array, SortedByY, SortedByY.Length);
- else{
- Point[] sorted = SortByY();
- Array.Copy(sorted, SortedByY, SortedByY.Length);
- }
+ _points = array.Copy();
+ SortedByX = (isXSorted ? array.Copy() : SortByX(array));
+ SortedByY = (isYSorted ? array.Copy() : SortByY(array));
}
public PointArray(string file) : this(PointFile.ReadFile(file), false, false) { }
public Point this[int i] => _points[i];
- Point[] SortByX()
- => MergeSort<Point>.Sort(_points, Point.CompareX());
+ public static Point[] SortByX(Point[] points)
+ => MergeSort<Point>.Sort(points.Copy(), Point.CompareX());
- Point[] SortByY()
- => MergeSort<Point>.Sort(_points, Point.CompareY());
+ public static Point[] SortByY(Point[] points)
+ => MergeSort<Point>.Sort(points.Copy(), Point.CompareY());
}
} \ No newline at end of file
diff --git a/src/MergeSort/Extensions.cs b/src/MergeSort/Extensions.cs
new file mode 100644
index 0000000..24bb43c
--- /dev/null
+++ b/src/MergeSort/Extensions.cs
@@ -0,0 +1,26 @@
+using System;
+
+namespace Extensions
+{
+ static class ArrayExtensions
+ {
+ public static void Split<T>(this T[] array, int index, out T[] first, out T[] second)
+ {
+ first = new T[index];
+ second = new T[array.Length - index];
+
+ Array.Copy(array, first, first.Length);
+ Array.Copy(array, index, second, 0, second.Length);
+ }
+
+ public static void Split<T>(this T[] array, out T[] first, out T[] second)
+ => Split(array, array.Length / 2, out first, out second);
+
+ public static T[] Copy<T>(this T[] array)
+ {
+ T[] outArray = new T[array.Length];
+ Array.Copy(array, outArray, outArray.Length);
+ return outArray;
+ }
+ }
+} \ No newline at end of file
diff --git a/src/MergeSort/MergeSort.cs b/src/MergeSort/MergeSort.cs
index 5c57bd4..106e554 100644
--- a/src/MergeSort/MergeSort.cs
+++ b/src/MergeSort/MergeSort.cs
@@ -1,32 +1,20 @@
-using System;
-using System.Collections.Generic;
+using System.Collections.Generic;
+using Extensions;
namespace MergeSort
{
public class MergeSort<TElement>
{
- // public static IEnumerable<TComparable> Sort(IEnumerable<TComparable> elements)
- // => Sort(elements, Comparer<TComparable>.Default);
public static TElement[] Sort(TElement[] arr, IComparer<TElement>? comparer = null)
- {
- if (comparer == null)
- comparer = Comparer<TElement>.Default;
+ => SortRecursive(arr, comparer ?? Comparer<TElement>.Default);
- return SortRecursive(arr, comparer);
- }
private static TElement[] SortRecursive(TElement[] arr, IComparer<TElement> comparer)
{
if (arr.Length <= 1)
return arr;
- int middle = arr.Length / 2;
-
- TElement[] leftArr = new TElement[middle];
- TElement[] rightArr = new TElement[arr.Length - middle];
-
- Array.Copy(arr, leftArr, leftArr.Length);
- Array.Copy(arr, middle, rightArr, 0, rightArr.Length);
+ arr.Split(out TElement[] leftArr, out TElement[] rightArr);
SortRecursive(leftArr, comparer);
SortRecursive(rightArr, comparer);