From e6100919e6336650dfe49663ba5c3781e49b1f0d Mon Sep 17 00:00:00 2001 From: Diala Da Costa Date: Fri, 18 Dec 2015 16:41:44 -0800 Subject: [PATCH 1/2] Just testing --- Documentation/test.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 Documentation/test.txt diff --git a/Documentation/test.txt b/Documentation/test.txt new file mode 100644 index 000000000000..30d74d258442 --- /dev/null +++ b/Documentation/test.txt @@ -0,0 +1 @@ +test \ No newline at end of file From d079b2215d11be79e67579bed19dd105b3639516 Mon Sep 17 00:00:00 2001 From: Diala Da Costa Date: Wed, 2 Mar 2016 14:39:20 -0800 Subject: [PATCH 2/2] Refactory_Array_Sort --- src/System.Sorting/src/InsertionSort.cs | 74 ++++++ src/System.Sorting/src/IntroSort.cs | 20 ++ src/System.Sorting/src/Sort.cs | 223 ++++++++++++++++++ src/System.Sorting/src/System.Sorting.csproj | 3 + src/System.Sorting/tests/Program.cs | 17 ++ src/System.Sorting/tests/SortTests.cs | 140 +++++++++++ .../tests/System.Sorting.Tests.csproj | 6 +- 7 files changed, 481 insertions(+), 2 deletions(-) create mode 100644 src/System.Sorting/src/InsertionSort.cs create mode 100644 src/System.Sorting/src/IntroSort.cs create mode 100644 src/System.Sorting/src/Sort.cs create mode 100644 src/System.Sorting/tests/Program.cs create mode 100644 src/System.Sorting/tests/SortTests.cs diff --git a/src/System.Sorting/src/InsertionSort.cs b/src/System.Sorting/src/InsertionSort.cs new file mode 100644 index 000000000000..774d6df55f51 --- /dev/null +++ b/src/System.Sorting/src/InsertionSort.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.Contracts; + +namespace System.Sorting +{ + public class InsertionSorting : Sorting + { + public InsertionSorting() { } + internal override void SortInternal(Tkey[] array, int index, int length, IComparer comparer) + { + + //Cheking the input parameters conditions. + Contract.Requires(array != null); + Contract.Requires(index >= 0); + Contract.Requires(length >= 0); + Contract.Requires(length <= array.Length); + Contract.Requires(length + index <= array.Length); + + int i, j; + int high = index + length - 1; + Tkey t; + for (i = index; i < high; i++) + { + j = i; + t = array[i + 1]; + while (j >= index && comparer.Compare(t, array[j]) < 0) + { + array[j + 1] = array[j]; + j--; + } + array[j + 1] = t; + } + } + + + internal override void SortInternal(TKey[] keys, TValue[] items, int index, int length, IComparer comparer) + { + Contract.Requires(keys != null); + Contract.Requires(items != null); + Contract.Requires(comparer != null); + Contract.Requires(index >= 0); + Contract.Requires(length >= 0); + Contract.Requires(length <= keys.Length); + Contract.Requires(length + index <= keys.Length); + Contract.Requires(length + index <= items.Length); + + if (length < 2) + return; + + int i, j; + int high = length + index - 1; + TKey t; + TValue tValue; + + for (i = index; i < high; i++) + { + j = i; + t = keys[i + 1]; + tValue = (items != null) ? items[i + 1] : default(TValue); + while (j >= index && comparer.Compare(t, keys[j]) < 0) + { + keys[j + 1] = keys[j]; + if (items != null) + items[j + 1] = items[j]; + j--; + } + keys[j + 1] = t; + if (items != null) + items[j + 1] = tValue; + } + } + } +} \ No newline at end of file diff --git a/src/System.Sorting/src/IntroSort.cs b/src/System.Sorting/src/IntroSort.cs new file mode 100644 index 000000000000..ca8025de731d --- /dev/null +++ b/src/System.Sorting/src/IntroSort.cs @@ -0,0 +1,20 @@ +using System.Collections.Generic; + +namespace System.Sorting +{ + public class IntroSorting : Sorting + { + public IntroSorting() + { } + internal override void SortInternal(TKey[] array, int index, int length, IComparer comparer) + { + //It will call the current clr of Array.Sort Implementation; Just a wrap + Array.Sort(array, index, length, comparer); + } + internal override void SortInternal(TKey[] keys, TValue[] items, int index, int length, IComparer comparer) + { + //It will call the current clr of Array.Sort Implementation; Just a wrap + Array.Sort(keys, items, index, length, comparer); + } + } +} \ No newline at end of file diff --git a/src/System.Sorting/src/Sort.cs b/src/System.Sorting/src/Sort.cs new file mode 100644 index 000000000000..46f7b451a998 --- /dev/null +++ b/src/System.Sorting/src/Sort.cs @@ -0,0 +1,223 @@ +using System; +using System.Collections.Generic; +using System.Reflection; +using System.Diagnostics.Contracts; +using System.Text; + +namespace System.Sorting +{ + + /// + /// Base class that defines the signature methods for possible derived classes + /// which will be implementing diffrent types of Sorting Algorithms. + /// + public abstract class Sorting + { + + public Sorting () + { } + + #region Methods of array of keys only + + /// + /// Sorts the elements of an array. + /// + /// array to be sorted + public virtual void Sort(T[] array) //where T : IComparable + { + if (array == null) + throw new ArgumentNullException("array"); + Sort(array, array.GetLowerBound(0), array.Length, null); + } + + + /// + /// Sorts the elements in a section of an array. The sort compares the + /// elements to each other using the given IComparer interface. If + /// comparer is null, the elements are compared to each other using + /// the IComparable interface, which in that case must be implemented + /// by all elements in the given section of the array. + /// + /// + /// + public virtual void Sort (T[] array, IComparer comparer) //where T : IComparable + { + if (array == null) + throw new ArgumentNullException("array"); + Sort(array, array.GetLowerBound(0), array.Length, comparer); + } + + + /// + /// Sorts the elements in a section of an array. The sort compares the + /// elements to each other using the IComparable interface, which + /// must be implemented by all elements in the given section of the array. + /// + /// + /// + /// + public virtual void Sort(T[] array, int index, int length) //where T : IComparable + { + if (array == null) + throw new ArgumentNullException("array"); + this.Sort(array, index, length, null); + } + + + /// + /// Sorts the elements in a section of an array. The sort compares the + /// elements to each other using the given IComparer interface. If + /// comparer is null, the elements are compared to each other using + /// the IComparable interface, which in that case must be implemented + /// by all elements in the given section of the array. + /// + /// + /// + /// + /// + public void Sort (T[] array, int index, int length, IComparer comparer) //where T : IComparable + { + if (array == null) + throw new ArgumentNullException("array"); + if (index < 0 || length < 0) + //throw new ArgumentOutOfRangeException((length < 0 ? "length" : "index"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + throw new ArgumentOutOfRangeException((length < 0 ? "length" : "index"), "ArgumentOutOfRange_NeedNonNegNum"); + if (array.Length - index < length) + //throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); + throw new ArgumentException("Argument_InvalidOffLen"); + + if (length < 2) + return; + + //Delegating the implementation to the derived classes which will implement different types of sorting algorithms. + SortInternal(array, index, length, comparer); + + } + + + /// + /// Sorts the elements in a section of an array. The sort compares the + /// elements to each other using the given IComparer interface. If + /// comparer is null, the elements are compared to each other using + /// the IComparable interface, which in that case must be implemented + /// by all elements in the given section of the array. + /// + /// + /// + /// + /// + internal virtual void SortInternal(T[] array, int index, int length, IComparer comparer) //where T : IComparable + { + // Each type of derived sorting Class will implement a different type of sorting algorithm + } + + #endregion + + #region Methods of arrays of keys and values pair + + /// + /// Sorts the elements of two arrays based on the keys in the first array. + /// Elements in the keys array specify the sort keys for + /// corresponding elements in the items array. The sort compares the + /// keys to each other using the IComparable interface, which must be + /// implemented by all elements of the keys array. + /// + /// array to be sorted + public virtual void Sort(TKey[] keys, TValue[] items) //where TKey : IComparable + { + if (keys == null) + throw new ArgumentNullException("keys"); + Sort(keys, items, keys.GetLowerBound(0), keys.Length, null); + } + + /// + /// Sorts the elements of two arrays based on the keys in the first array. + /// Elements in the keys array specify the sort keys for + /// corresponding elements in the items array. The sort compares the + /// keys to each other using the given IComparer interface. If + /// comparer is null, the elements are compared to each other using + /// the IComparable interface, which in that case must be implemented + /// by all elements of the keys array + /// + /// + /// + /// + /// + /// + public virtual void Sort(TKey[] keys, TValue[] items, IComparer comparer) //where TKey : IComparable + { + if (keys == null) + throw new ArgumentNullException("keys"); + Sort(keys, keys.GetLowerBound(0), keys.Length, comparer); + } + + + /// + /// Sorts the elements in a section of two arrays based on the keys in the + /// first array. Elements in the keys array specify the sort keys for + /// corresponding elements in the items array. The sort compares the + /// keys to each other using the given IComparer interface. If + /// comparer is null, the elements are compared to each other using + /// the IComparable interface, which in that case must be implemented + /// by all elements of the given section of the keys array. + /// + /// + /// + /// + /// + /// + /// + public virtual void Sort(TKey[] keys, TValue[] items, int index, int length) //where TKey : IComparable + { + if (keys == null) + throw new ArgumentNullException("keys"); + Sort(keys, items, index, length, null); + } + + /// + /// Sorts the elements in a section of two arrays based on the keys in the + /// first array. Elements in the keys array specify the sort keys for + /// corresponding elements in the items array. The sort compares the + /// keys to each other using the given IComparer interface. If + /// comparer is null, the elements are compared to each other using + /// the IComparable interface, which in that case must be implemented + /// by all elements of the given section of the keys array. + /// + /// + /// + /// + /// + /// + public void Sort (TKey[] keys, TValue[] items, int index, int length, IComparer comparer) //where TKey : IComparable + { + if (keys == null) + throw new ArgumentNullException("keys"); + if (index < 0 || length < 0) + //throw new ArgumentOutOfRangeException((length < 0 ? "length" : "index"), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + throw new ArgumentOutOfRangeException((length < 0 ? "length" : "index"), "ArgumentOutOfRange_NeedNonNegNum"); + if (keys.Length - index < length || (items != null && index > items.Length - length)) + //throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); + throw new ArgumentException("Argument_InvalidOffLen"); + + if (length < 2) + return; + + if (items == null) + { + Sort(keys, index, length, comparer); + return; + } + + SortInternal(keys, items, index, length, comparer); + } + + internal virtual void SortInternal(T[] keys, TValue[] items, int index, int length, IComparer comparer) //where T : IComparable + { + // Each type of derived sorting Class will implement a different type of sorting algorithm + } + #endregion + + } + + +} diff --git a/src/System.Sorting/src/System.Sorting.csproj b/src/System.Sorting/src/System.Sorting.csproj index 2351742790c3..f432799b503f 100644 --- a/src/System.Sorting/src/System.Sorting.csproj +++ b/src/System.Sorting/src/System.Sorting.csproj @@ -16,6 +16,9 @@ + + + diff --git a/src/System.Sorting/tests/Program.cs b/src/System.Sorting/tests/Program.cs new file mode 100644 index 000000000000..861e9939dc5a --- /dev/null +++ b/src/System.Sorting/tests/Program.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace System.Sorting.Tests +{ + class Program + { + public static void Main (String[] args) + { + SortTests.TestSort(); + + } + } +} diff --git a/src/System.Sorting/tests/SortTests.cs b/src/System.Sorting/tests/SortTests.cs new file mode 100644 index 000000000000..0ee8d235da26 --- /dev/null +++ b/src/System.Sorting/tests/SortTests.cs @@ -0,0 +1,140 @@ +using System; +using System.Collections.Generic; +using System.Reflection; +using System.Linq; +using Xunit; +using System.Text; +using System.Threading.Tasks; + +namespace System.Sorting.Tests +{ + public class SortTests + { + [Fact] + public static void TestSort() + { + IComparer icomparer = new IntegerComparer(); + + TestSortHelper(new int[] { }, 0, 0, icomparer); + TestSortHelper(new int[] { 5 }, 0, 1, icomparer); + TestSortHelper(new int[] { 5, 2 }, 0, 2, icomparer); + + TestSortHelper(new int[] { 5, 2, 9, 8, 4, 3, 2, 4, 6 }, 0, 9, icomparer); + TestSortHelper(new int[] { 5, 2, 9, 8, 4, 3, 2, 4, 6 }, 3, 4, icomparer); + TestSortHelper(new int[] { 5, 2, 9, 8, 4, 3, 2, 4, 6 }, 3, 6, icomparer); + + IComparer scomparer = new StringComparer(); + TestSortHelper(new string[] { }, 0, 0, scomparer); + TestSortHelper(new string[] { "5" }, 0, 1, scomparer); + TestSortHelper(new string[] { "5", "2" }, 0, 2, scomparer); + + TestSortHelper(new string[] { "5", "2", null, "8", "4", "3", "2", "4", "6" }, 0, 9, scomparer); + TestSortHelper(new string[] { "5", "2", null, "8", "4", "3", "2", "4", "6" }, 3, 4, scomparer); + TestSortHelper(new string[] { "5", "2", null, "8", "4", "3", "2", "4", "6" }, 3, 6, scomparer); + } + + + private static void TestSortHelper(T[] array, int index, int length, IComparer comparer) + { + + + T[] control = SimpleSort(array, index, length, comparer); + + //TODO - Can use Reflection to get the derivedTypes of Sorting and invoking the methods. + //It's not so trivial because we are using Reflection with Generic . + //TypeInfo[] mySortTestAlgorithms = FindAllDerivedTypesOfSorting(); + + Sorting[] sortAlgos = new Sorting[] { new IntroSorting(), new InsertionSorting() }; + foreach(var algo in sortAlgos) + { + T[] spawn = (T[])(array.Clone()); + // get instance from algo + algo.Sort(spawn, index, length, comparer); + Assert.True(ArraysAreEqual((T[])spawn, control, comparer)); + } + + } + + private static T[] SimpleSort(T[] a, int index, int length, IComparer comparer) + { + T[] result = (T[])(a.Clone()); + if (length < 2) + return result; + + for (int i = index; i < index + length - 1; i++) + { + T tmp = result[i]; + for (int j = i + 1; j < index + length; j++) + { + if (comparer.Compare(tmp, result[j]) > 0) + { + result[i] = result[j]; + result[j] = tmp; + tmp = result[i]; + } + } + } + return result; + } + + private static bool ArraysAreEqual(T[] a, T[] b, IComparer comparer) + { + // If the same instances were passed, this is unlikely what the test intended. + Assert.False(Object.ReferenceEquals(a, b)); + + if (a.Length != b.Length) + return false; + for (int i = 0; i < a.Length; i++) + { + if (0 != comparer.Compare(a[i], b[i])) + return false; + } + return true; + } + + private class IntegerComparer : IComparer + { + public int Compare(object x, object y) + { + return Compare((int)x, (int)y); + } + + public int Compare(int x, int y) + { + return x - y; + } + } + + private class StringComparer : IComparer + { + public int Compare(object x, object y) + { + return Compare((string)x, (string)y); + } + + public int Compare(string x, string y) + { + if (x == y) + return 0; + if (x == null) + return -1; + if (y == null) + return 1; + return x.CompareTo(y); + } + } + + public static TypeInfo [] FindAllDerivedTypesOfSorting() + { + TypeInfo selectedType = typeof(Sorting).GetTypeInfo(); + + // Find all the derived classes of the selected type. + Assembly asm = selectedType.Assembly; + TypeInfo[] subTypes = (asm.DefinedTypes + .Where(t => t.IsClass && t.IsSubclassOf(selectedType.AsType()))).ToArray(); + + return subTypes; + } + + } +} diff --git a/src/System.Sorting/tests/System.Sorting.Tests.csproj b/src/System.Sorting/tests/System.Sorting.Tests.csproj index 73bf176c8fb8..2b375c9965cc 100644 --- a/src/System.Sorting/tests/System.Sorting.Tests.csproj +++ b/src/System.Sorting/tests/System.Sorting.Tests.csproj @@ -1,4 +1,4 @@ - + @@ -19,6 +19,8 @@ Common\System\PerfUtils.cs + + @@ -30,4 +32,4 @@ - + \ No newline at end of file