static void Main() { //лист пользовательских типов IEnumerable<Person> personList = new List<Person> { new Person { BirthYear = 1948, Name = "Cat Stevens" }, new Person { BirthYear = 1955, Name = "Kevin Costner" }, new Person { BirthYear = 1952, Name = "Vladimir Putin" }, new Person { BirthYear = 1955, Name = "Bill Gates" }, new Person { BirthYear = 1948, Name = "Kathy Bates" }, new Person { BirthYear = 1956, Name = "David Copperfield" }, new Person { BirthYear = 1948, Name = "Jean Reno" } }; personList = new MergeSort().Sort(personList, (x, y) => x.BirthYear.CompareTo(y.BirthYear)); //массив чисел IEnumerable<int> intArray = new[] {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; intArray = new ShellSort().Sort(intArray, Comparer<int>.Default); IEnumerable<KeyValuePair<string, int>> personDictionary = new Dictionary<string, int> { {"Cat Stevens", 1948}, {"Kevin Costner", 1955}, {"Vladimir Putin", 1952}, {"Bill Gates", 1955}, {"Kathy Bates", 1948}, {"David Copperfield", 1954}, {"Jean Reno", 1948} }; personDictionary = new InsertSort().Sort(personDictionary, (x, y) => x.Value.CompareTo(y.Value)); }
// Main driver function static void Main(string[] args) { Console.Write("Please enter the length of the array: "); int length = int.Parse(Console.ReadLine()); int[] arr = new int[length]; Console.WriteLine("Please enter the elements of the array: "); for (int i = 0; i < length; i++) { Console.Write("arr[{0}] = ", i); arr[i] = int.Parse(Console.ReadLine()); } MergeSort merge = new MergeSort(); // Calling Merge Procedure merge.mergesort(arr, 0, arr.Length-1); // Printing Sorted array. after merge sort Console.Write("The sorted array is: "); foreach (int a in arr) { Console.Write(a + " "); } Console.WriteLine(); }
public static uint CountInversions_MergeSort(int[] arrayWithInversions) { MergeSort mSort = new MergeSort(); mSort.Sort(arrayWithInversions); return mSort.InversionCount; }
public void MergeSortTest() { // Random elements var sorter = new MergeSort<int>(); shuffledList = new List<int>(shuffledList); sorter.Sort(shuffledList); var temp = shuffledList.ToArray(); Array.Sort(temp); CollectionAssert.AreEqual(temp, shuffledList); //one element var collection = new[] { 0 }; sorter.Sort(collection); temp = collection.ToArray(); Array.Sort(temp); CollectionAssert.AreEqual(temp, collection); //zero elements collection = new int[0]; sorter.Sort(collection); temp = collection.ToArray(); Array.Sort(temp); CollectionAssert.AreEqual(temp, collection); //null elements collection = null; sorter.Sort(collection); CollectionAssert.AreEqual(null, collection); }
private void btn_MergeSort_Click(object sender, EventArgs e) { MergeSort sortAlg = new MergeSort(10); txt_beforeSort.Text = ArrayUtil.toString(sortAlg.array); sortAlg.Sort(); txt_afterSort.Text = ArrayUtil.toString(sortAlg.array); }
public static void Main(string[] args) { Console.WriteLine("Loading array from file..."); var array = LoadArrayFromFile("largeArray.txt"); Console.WriteLine("Array loaded"); var stopwatch = Stopwatch.StartNew(); Console.WriteLine("Started sorting the array"); var processorsCount = Environment.ProcessorCount; var elementsToSlice = array.Length / processorsCount; var elementsLeft = (array.Length % processorsCount) - 1; var arraySlices = new List<int[]>(processorsCount); var threads = new List<Thread>(processorsCount); for (int i = 0; i < processorsCount; i++) { // Cache the value of 'i' and use it // in the newly created thread var index = i; var thread = new Thread(() => { int[] arraySlice; if (index == processorsCount - 1) { arraySlice = array.SubArray(index * elementsToSlice, elementsToSlice + elementsLeft); } else { arraySlice = array.SubArray(index * elementsToSlice, elementsToSlice); } arraySlices.Add(arraySlice); var mergeSort = new MergeSort(); mergeSort.Sort(arraySlice); }); thread.Start(); // Doesn't execute immediately threads.Add(thread); } // Wait for all threads to finish their work for (int i = 0; i < threads.Count; i++) { threads[i].Join(); } // Merge results var result = RecursiveMerge(arraySlices); stopwatch.Stop(); Console.WriteLine("Array sorted"); // Elapsed time: 700-800 ms Console.WriteLine($"Elapsed time: {stopwatch.Elapsed.TotalMilliseconds} ms"); }
private static Tuple<PlaneCoordinate, PlaneCoordinate> DivideConquerClosestPair(PlaneCoordinate[] coordinates) { // sort by x and y Sorter<double> mergeSort = new MergeSort<double>(); double[] px = mergeSort.Sort(coordinates.Select(p => p.X).ToArray()); double[] py = mergeSort.Sort(coordinates.Select(p => p.Y).ToArray()); return FindClosestPair(px, py); }
private static void RunMergeSort() { var scheduler = new Scheduler(); var mergeSort = new MergeSort(new[] { 3, 4, 3, 2, 7, 5, 9, 0 }); scheduler.Spawn(mergeSort); scheduler.Schedule(); Console.WriteLine(String.Join(", ", mergeSort.Results.Select(i => i.ToString()).ToArray())); }
static void Main(string[] args) { var array = new int[] { 4, 45, 3, 2, 1, 5, 34 }; MergeSort.SortMerge(array); foreach (int t in array) { Console.Write(t + " "); } Console.ReadLine(); }
public void SortsInAscendingOrderTest() { int[] array = new int[] { 5, 4, 3, 2, 1, 7 }; var sortedArray = new MergeSort().Sort(array); Assert.AreEqual(sortedArray.Length, array.Length); for (int i = 0; i < array.Length - 1; i++) { Assert.IsTrue(sortedArray[i].CompareTo(sortedArray[i + 1]) <= 0); } }
public void FirstTry_Sort_With_A_List_Totally_Unsorted() { int[] listOfNumbers = new int[] { 5, 4, 6, 8, 1, 7, 3, 2 }; var sortedArray = new MergeSort().FirstTry(listOfNumbers); for (int i = 1; i <= sortedArray.Length; i++) { Assert.Equal(i, sortedArray[i - 1]); } }
public void SecondTry_Sort_With_A_List_In_Desc_Orders() { int[] listOfNumbers = new int[] { 8, 7, 6, 5, 4, 3, 2, 1 }; var sortedArray = new MergeSort().SecondTry(listOfNumbers); for (int i = 1; i <= sortedArray.Length; i++) { Assert.Equal(i, sortedArray[i - 1]); } }
public void SecondTry_Sort_With_A_List_Almost_Sorted() { int[] listOfNumbers = new int[] { 1, 2, 4, 3, 5, 7, 6, 8 }; var sortedArray = new MergeSort().SecondTry(listOfNumbers); for (int i = 1; i <= sortedArray.Length; i++) { Assert.Equal(i, sortedArray[i - 1]); } }
static void Main(string[] args) { int[] array = GetArray(); MergeSort.Sort(array); Console.Write("Sorted array: "); for (int i = 0; i < array.Length; i++) { Console.Write($"{array[i]} "); } Console.ReadKey(); }
public void SortArray() { var sut = new MergeSort <double>(); double[] x = GenerateRandomArray(7); var result = sut.Sort(x); Assert.That(result, Has.Exactly(x.Length).Items); Assert.That(result, Is.SubsetOf(x)); Assert.That(result, Is.Ordered.Ascending); }
static void Main(string[] args) { int[] input = Console.ReadLine() .Split() .Select(int.Parse) .ToArray(); MergeSort <int> .Sort(input); Console.WriteLine(string.Join(" ", input)); }
public void Mergesort_IntegerTest() { //arrange MergeSort <int> mergesort = new MergeSort <int>(); //act mergesort.Sort(intInput, 0, intInput.Length - 1); int[] result = intInput; //assert Assert.Equal(expectedInt, result); }
public void Test_Merge_Sort_Array_Not_Null() { int[] arr = new int[10] { 1, 5, 4, 11, 20, 8, 2, 98, 90, 16 }; MergeSort mergeSort = new MergeSort(); int[] actual = mergeSort.Sort(arr); int[] expected = new int[] { 1, 2, 4, 5, 8, 11, 16, 20, 90, 98 }; Assert.AreEqual(expected, actual); }
public void MergeSortTest_ArrIsSorted() { ISortAble mergeSort = new MergeSort(); int[] sorted = mergeSort.Sort(arr); for (int i = 0; i < sorted.Length - 1; i++) { Assert.IsTrue(sorted[i] <= sorted[i + 1]); } }
// [Repeat(25)] public void TestMergeSortAlgorithm() { int[] expected = SortingTests.GenerateArrayWithRandomIntegers(); var actual = (int[])expected.Clone(); // Perform sorting algorithms Array.Sort(expected); // Native MergeSort.Sort(actual); // Custom Assert.AreEqual(expected, actual); }
public static void Main(string[] args) { int[] array = new int[] { 3, 4, 2, 1, 6, 5, 7, 8, 1, 1 }; MergeSort m = new MergeSort(array); m.sort(); for (int i = 0; i < array.Length; i++) { Console.Write(array[i] + " "); } }
public void Mergesort_StringTest() { //arrange MergeSort <string> mergesort = new MergeSort <string>(); //act mergesort.Sort(strInput, 0, strInput.Length - 1); string[] result = strInput; //assert Assert.Equal(expectedStr, result); }
public void MergeArray() { var sut = new MergeSort <double>(); int size = _Random.Next(100); var x = GenerateRandomArray(size).OrderBy(t => t).ToArray(); var y = GenerateRandomArray(size).OrderBy(t => t).ToArray(); var result = sut.Merge(x, y); Assert.That(result, Has.Exactly(size * 2).Items); Assert.That(result, Is.Ordered.Ascending); }
static void Main(string[] args) { var merge = new MergeSort(); var data = SortCompare.GetRandomArrayInt(200); merge.Sort(data); for (var i = 0; i < data.Length; i++) { Console.Write(data[i] + " "); } }
FindMinimumSpanningTree(WeightedGraph <T, TW> graph) { var edges = new List <MSTEdge <T, TW> >(); //gather all unique edges dfs(graph.ReferenceVertex, new HashSet <T>(), new Dictionary <T, HashSet <T> >(), edges); //quick sort preparation var sortArray = new MSTEdge <T, TW> [edges.Count]; for (int i = 0; i < edges.Count; i++) { sortArray[i] = edges[i]; } //quick sort edges var sortedEdges = MergeSort <MSTEdge <T, TW> > .Sort(sortArray); var result = new List <MSTEdge <T, TW> >(); var disJointSet = new DisJointSet <T>(); //create set foreach (var vertex in graph.Vertices) { disJointSet.MakeSet(vertex.Key); } //pick each edge one by one //if both source and target belongs to same set //then don't add the edge to result //otherwise add it to result and union sets for (int i = 0; i < edges.Count; i++) { var currentEdge = sortedEdges[i]; var setA = disJointSet.FindSet(currentEdge.Source); var setB = disJointSet.FindSet(currentEdge.Destination); //can't pick edge with both ends already in MST if (setA.Equals(setB)) { continue; } result.Add(currentEdge); //union picked edge vertice sets disJointSet.Union(setA, setB); } return(result); }
public void MergeSortTest() { for (int i = 0; i < 100; i++) { int[] originData = Util.GenRandomIntArr(); int[] sortedData = (int[])originData.Clone(); Array.Sort(sortedData); MergeSort.Sort(originData, 0, originData.Length - 1); Assert.IsTrue(Util.CompareList(originData, sortedData)); } }
public void BasicSort() { var gen = new Random(44); var iSort = new MergeSort<MockComparable>(); foreach (var N in new[] { 1, 3, 5, 10 , 100000 }) { var mocks = Enumerable.Range(0, N).Select(i => new MockComparable(gen.Next(0, 10))).ToArray(); iSort.Sort(mocks); Assert.IsTrue(mocks.IsSorted(), "#D" + N); } }
public void Test2() { MergeSort <long> c = new MergeSort <long>(3, a); c.Start(); Assert.IsTrue(a[0] == f1[0]); Assert.IsTrue(a[1] == f1[1]); Assert.IsTrue(a[2] == f1[2]); Assert.IsTrue(a[3] == f1[3]); }
public void MergeSortTest() { var list = new List <int>() { 89, 45, 68, 90, 29, 34, 17 }; var sortList = MergeSort.SortTopDown(list); int[] sortedList = { 17, 29, 34, 45, 68, 89, 90 }; Assert.True(sortList.SequenceEqual(sortedList)); }
public void MergeSortText() { var merge = new MergeSort <int>(); merge.Items.AddRange(items); merge.Sort(); for (int i = 0; i < items.Count; i++) { Assert.AreEqual(sorted[i], merge.Items[i]); } }
private static void TestMergeSort(int[] myArray) { Console.WriteLine("\n====== STABILITY! Divide to conquer! ======" + "\nTime complexity Ω(nLog n) and O(nLog n) - Space complexity O(n)" + "\n\n======Steps:\n"); MergeSort <int> mergesort = new MergeSort <int>(); mergesort.SortTime(myArray, 0, myArray.Length - 1); Console.WriteLine("Execution time: " + mergesort.MSlog); }
private static void Sort <T>(T[] items, string expectedResult, Comparison <T> comparison = null) { // Arrange var sut = new MergeSort <T>(comparison); // Act var result = sut.Sort(items); // Assert Assert.Equal(expectedResult, string.Join(" ", result)); }
public void ThrowNullException() { // Arrange int[] collection = null; // Act Action action = () => MergeSort.Sort(collection); // Assert Assert.Throws <ArgumentNullException>(action); }
public void StringTopdownTest() { char[] array = { 'm', 'n', 'a', 'k', 'c', 'd', 'g', 'j', 'b', 'l', 'f', 'i', 'e', 'h' }; MergeSort <char> .TopDownSort(array); for (int i = 0; i < array.Length; i++) { Assert.Equal(array[i], expectChar[i]); } }
public void IntTopdownTest() { int[] array = { 10, 0, 3, 11, 8, 9, 1, 12, 5, 2, 4, 6, 7 }; MergeSort <int> .TopDownSort(array); for (int i = 0; i < array.Length; i++) { Assert.Equal(array[i], expectInt[i]); } }
public void SortTest4() { var input = new int[] { 1, 2, 3, 4, 5 }; MergeSort sort = new MergeSort(); var result = sort.Sort(input); int[] expectResult = new int[] { 1, 2, 3, 4, 5 }; CollectionAssert.AreEqual(result, expectResult); }
public void Given2Points_HasCorrectResult() { var array = new[] { 5, 2 }; var algorithm = new MergeSort(array); algorithm.Sort(); array.ShouldBe(new[] { 2, 5 }); //algorithm.RecursiveDivides[0].left.ShouldBe(new[] {5, int.MaxValue}); }
public void GivenManyPoints_HasCorrectResult() { var array = new[] { 5, 0, 1, 67, 4, 2, 52, 9, 3, 12 }; var algorithm = new MergeSort(array); algorithm.Sort(); array.ShouldBe(new[] { 0, 1, 2, 3, 4, 5, 9, 12, 52, 67 }); //algorithm.RecursiveDivides[0].left.ShouldBe(new[] {5, int.MaxValue}); }
// Driver code public static void Run() { int[] arr = { 12, 11, 13, 5, 6, 7 }; Console.WriteLine("Given Array"); printArray(arr); MergeSort ob = new MergeSort(); ob.sort(arr, 0, arr.Length - 1); Console.WriteLine("\nSorted array"); printArray(arr); }
public void ParallelSort() { var gen = new Random(44); const int cpu = 4; var mergeSort = new MergeSort<MockComparable>(); foreach (var N in new[] { 1, 3, 5, 10, 100000 }) { var mocks = Enumerable.Range(0, N).Select(i => new MockComparable(gen.Next(0, 10))).ToArray(); mergeSort.ParallelSort(mocks, cpu); Assert.IsTrue(mocks.IsSorted(), "#E" + N); } }
static void Main() { int[] arrayOfNumbers = { 1, 5, 9, 4, 8, 7, 2, 3, 6 }; MergeSort merge = new MergeSort(); merge.mergesort(arrayOfNumbers, 0, arrayOfNumbers.Length - 1); foreach (int a in arrayOfNumbers) { Console.Write(a + " "); } Console.WriteLine(); }
public void ViewSpeedUp() { var gen = new Random(15); int N = 4000000; var array1 = Enumerable.Range(0, N).Select(i => new MockComparable(gen.Next(int.MaxValue))).ToArray(); var array2 = (MockComparable[]) array1.Clone(); var watch = Stopwatch.StartNew(); var mSort = new MergeSort<MockComparable>(); mSort.ParallelSort(array1, 4); Console.WriteLine(@"total time elapsed in parallel merge sort " + watch.Elapsed.TotalSeconds.ToString()); watch.Reset(); watch.Start(); mSort.Sort(array2); Console.WriteLine(@"total time elapsed in regular merge sort " + watch.Elapsed.TotalSeconds.ToString()); }
static void Main() { Console.WriteLine("Enter integer numbers, separated by a comma:"); string arrStr = Console.ReadLine(); int[] arr = Array.ConvertAll(arrStr.Split(','), int.Parse); MergeSort merge = new MergeSort(); merge.mergesort(arr, 0, arr.Length - 1); foreach (int a in arr) { Console.Write(a + " "); } Console.WriteLine(); }
public static void Main(string[] args) { if (args.Length == 2) { MergeSort<int> ms = new MergeSort<int> (); int length = Convert.ToInt32 (args [1]); int[] unsorted = new int[length]; TextReader file = new StreamReader (args [0]); for (int i = 0; i < length; i++) unsorted [i] = Convert.ToInt32 (file.ReadLine ()); Console.WriteLine (string.Join (",", ms.mergesort (unsorted))); } else { Console.WriteLine ("Usage: ./program [list file] [list length]"); } }
public static void Start() { bool first = true; while ( first || Console.ReadLine() != "q") { first = false; var generator = new Random(1998); const int N = 50000000; var items = Enumerable.Range(0, N).Select(i => generator.Next(0, int.MaxValue - 1)).ToArray(); var pMergeSort = new MergeSort<int>(); Console.WriteLine(@"Change processor affinity in task manager and set the max number of thread..."); var str = Console.ReadLine(); int maxThread = int.Parse(str); var watch = Stopwatch.StartNew(); pMergeSort.ParallelSort(items, maxThread); Console.WriteLine(@"time elapsed in s.{0}", watch.Elapsed.TotalSeconds); Console.WriteLine(@"Press 'q' to exit..."); } }
public static void MergeSort(int[] a, SortDirection direction) { MergeSort<int> ms = new MergeSort<int>(); ms.Sort(a, direction); }
public void Initialize() { sorter = new MergeSort<int>(); }
public void MergeSortTest() { //Arrange MergeSort<int> mergeSort = new MergeSort<int>(); var arrayToSort = new int[_arrayToSort.Length]; _arrayToSort.CopyTo(arrayToSort, 0); //Act arrayToSort.Sort(mergeSort); //Assert Assert.IsTrue(arrayToSort.IsSorted()); }
public void Sort_EmptyCollection_ShouldReturnEmptyCollection() { var elements = new List<int>(); var sorted = new MergeSort<int>().Sort(elements); CollectionAssert.AreEqual(new List<int>(), sorted); }
public void Sort_FewElements_ShouldSortElements() { var elements = new List<int> {5, 6, 7, 435, 2, 23, 456, 63, 89, 2}; var sorted = new MergeSort<int>().Sort(elements); CollectionAssert.AreEqual(new List<int>{2,2,5,6,7,23,63, 89,435,456}, sorted); }
public void Sort_OneElement_ShouldSortElements() { var elements = new List<int> { 5,}; var sorted = new MergeSort<int>().Sort(elements); CollectionAssert.AreEqual(new List<int> {5}, sorted); }
public void Sort_TwoElements_ShouldSortElements() { var elements = new List<int> { 89, 2 }; var sorted = new MergeSort<int>().Sort(elements); CollectionAssert.AreEqual(new List<int> { 2, 89}, sorted); }
public void SetUp() { ms = new MergeSort<int> (); }
static void Main(string[] args) { int[] arr = new int[10000]; Random rnd = new Random(); for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(10000); } ISort<int> s = new BubbleSort<int>(); DateTime dt = DateTime.Now; arr = s.Sorting(arr); Console.WriteLine("Time for BubbleSort is {0}.", DateTime.Now - dt); for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(10000); } dt = DateTime.Now; s = new CocktailSort<int>(); arr = s.Sorting(arr); Console.WriteLine("Time for CocktailSort is {0}.", DateTime.Now - dt); for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(10000); } dt = DateTime.Now; s = new EvenOddSort<int>(); arr = s.Sorting(arr); Console.WriteLine("Time for EvenOddSort is {0}.", DateTime.Now - dt); for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(10000); } dt = DateTime.Now; s = new CombSort<int>(); arr = s.Sorting(arr); Console.WriteLine("Time for CombSort is {0}.", DateTime.Now - dt); for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(10000); } dt = DateTime.Now; s = new GnomeSort<int>(); arr = s.Sorting(arr); Console.WriteLine("Time for GnomeSort is {0}.", DateTime.Now - dt); arr = new int[10000]; for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(10000); } dt = DateTime.Now; s = new InsertionSort<int>(); arr = s.Sorting(arr); Console.WriteLine("Time for InsertionSort is {0}.", DateTime.Now - dt); for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(10000); } dt = DateTime.Now; s = new BinaryInsertionSort<int>(); arr = s.Sorting(arr); Console.WriteLine("Time for BinaryInsertionSort is {0}.", DateTime.Now - dt); for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(10000); } dt = DateTime.Now; s = new ShellSort<int>(); arr = s.Sorting(arr); Console.WriteLine("Time for ShellSort is {0}.", DateTime.Now - dt); arr = new int[1000000]; for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(1000000); } for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(10000); } dt = DateTime.Now; s = new HeapSort<int>(); arr = s.Sorting(arr); Console.WriteLine("Time for HeapSort is {0}.", DateTime.Now - dt); int ddd = 0; for (int i = 0; i < arr.Length - 2; i++) { //Console.Write(arr[i] + " "); if (arr[i] > arr[i + 1]) //Console.WriteLine("Fatal ERROR!!!"); ddd++; } Console.WriteLine("Error count: {0}", ddd); dt = DateTime.Now; s = new MergeSort<int>(); arr = s.Sorting(arr); Console.WriteLine("Time for MergeSort is {0}.", DateTime.Now - dt); //StreamWriter sw = new StreamWriter("C:/Users/suvorovi/1.txt"); for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(1000000); //sw.Write(arr[i] + " "); } //sw.WriteLine(""); dt = DateTime.Now; s = new QuickSort<int>(); arr = s.Sorting(arr); Console.WriteLine("Time for QuickSort is {0}.", DateTime.Now - dt); for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(1000000); //sw.Write(arr[i] + " "); } //sw.WriteLine(""); dt = DateTime.Now; s = new TimSort<int>(); arr = s.Sorting(arr); Console.WriteLine("Time for TimSort is {0}.", DateTime.Now - dt); ddd = 0; for (int i = 0; i < arr.Length - 2; i++) { //Console.Write(arr[i] + " "); if (arr[i] > arr[i + 1]) //Console.WriteLine("Fatal ERROR!!!"); ddd++; } Console.WriteLine("Error count: {0}", ddd); Console.ReadLine(); //sw.Close(); }