public void ShellSort() { var algorithm = new ShellSort(); var sortedVector = algorithm.Sort(DataDefinition.UnsortedVector()); Assert.Equal(DataDefinition.SortedVector, sortedVector); }
static void Main(string[] args) { System.Console.WriteLine("\n===== Opgave 1 : Faculteit =====\n"); Factorial.Run(); System.Console.WriteLine("\n===== Opgave 2 : Fibonacci =====\n"); Fibonacci.Run(); System.Console.WriteLine("\n===== Opgave 3 : Alternately =====\n"); Alternately.Run(); System.Console.WriteLine("\n===== Opgave 4 : Enen =====\n"); Ones.Run(); System.Console.WriteLine("\n===== Opgave 6 : ForwardString =====\n"); ForwardBackwardString.Run(); System.Console.WriteLine("\n===== Opgave 7 : Sorting =====\n"); Sorter isort = new InsertionSort(); Sorter msort = new MergeSort(); Sorter ssort = new ShellSort(); isort.Run(); msort.Run(); ssort.Run(); int[] numbers = { 100, 1000, 10000 }; foreach (int num in numbers) { isort.RunWithTimer(num); msort.RunWithTimer(num); ssort.RunWithTimer(num); } }
/// <summary> /// 第一个测试,测试结果按照 Insertion, Selection, Shell 排序。 /// </summary> /// <param name="n">测试的数组长度。</param> /// <returns>测试结果。</returns> static double[] TestA(int n) { var insertionSort = new InsertionSort(); var selectionSort = new SelectionSort(); var shellSort = new ShellSort(); var random = new Random(); // 每个元素的主键均为 String 类型(至少长 10 个字符),并含有一个 double 值。 var array = new Pair <string, double> [n]; var arrayBak = new Pair <string, double> [n]; for (var i = 0; i < n; i++) { array[i] = new Pair <string, double>(RandomString(20, random), random.NextDouble()); } array.CopyTo(arrayBak, 0); var results = new double[3]; results[0] = SortCompare.Time(insertionSort, array); arrayBak.CopyTo(array, 0); results[1] = SortCompare.Time(selectionSort, array); arrayBak.CopyTo(array, 0); results[2] = SortCompare.Time(shellSort, array); return(results); }
//This will show how the num list runs on each algorithm //This allows the user to compare them public void hitSpeedCompButton() { algView = !algView; //Change whether you are looking at the algorithm or speed comparison if (algView) //If you are now viewing at the algorithm pseudocode { algText.text = oldAlgText; //Change the text back to the pseudocode if (!half) { algDescription.enabled = true; } speedCompText.text = "Speed Comparison"; } else//Otherwise display the speed comparison { oldAlgText = algText.text; string storageText = "Bubble Sort:{0} μs\nCocktail Sort: {1} μs\nComb Sort: {2} μs\nHeap Sort: {3}" + " μs\nInsertion Sort: {4} μs\nMerge Sort:{5} μs\nQuick Sort:{6} μs\nShell Sort:{7} μs\nFlash Sort:{8} μs" + "\nBucket Sort:{9} μs\nRadix Sort:{10} μs"; //Format the text to show each element in microseconds algText.text = string.Format(storageText, BubbleSort.BubbleSortTime(new List <int>(copy)), CocktailSort.CocktailSortTime(new List <int>(copy)), CombSort.CombSortTime(new List <int>(copy)), HeapSort.HeapSortStartTime(new List <int>(copy)), InsertionSort.InsertionSortTime(new List <int>(copy)), Mergesort.MergeSortTime(new List <int>(copy), 0, copy.Count - 1), QuickSort.QuickSortStartTime(new List <int>(copy), 0, copy.Count - 1), ShellSort.ShellSortTime(new List <int>(copy)), FlashSort.FlashSortTime(new List <int>(copy)), BucketSort.BucketSortTime(new List <int>(copy), 34) , RadixSort.RadixSortTime(new List <int>(copy))); algDescription.enabled = false; speedCompText.text = "Algorithm"; } }
static void RunShellSort(int[] array) { var sortAlgorithm = new ShellSort(); var result = sortAlgorithm.Sort(array); PrintArray("Shell Sort", result); }
private void ShellSortButton_Click(object sender, EventArgs e) { CleaningLabels(); var shell = new ShellSort <SortedItem>(items); BtnClick(shell); }
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)); }
/// <summary> /// Основное действие /// </summary> /// <param name="Fill">Метод заполнения массивов</param> /// <param name="maxSize">максимальная величина массива</param> /// <param name="message">сообщение</param> private void ExecuteSort(Action <IArray <long>, int> Fill, int maxSize, string message) { Console.WriteLine($"{message} method"); IArray <long> arr1; arr1 = new BubbleArray <long>(maxSize); Fill(arr1, maxSize); SortArr(arr1, "Buble sort " + message); arr1 = new SelectedArray <long>(maxSize); Fill(arr1, maxSize); SortArr(arr1, "Selected sort " + message); arr1 = new InsertSort <long>(maxSize); Fill(arr1, maxSize); SortArr(arr1, "Insert sort " + message); arr1 = new MergeSort <long>(maxSize); Fill(arr1, maxSize); SortArr(arr1, "Merge sort " + message); arr1 = new ShellSort <long>(maxSize); Fill(arr1, maxSize); SortArr(arr1, "Shell sort " + message); arr1 = new QuickSort(maxSize); Fill(arr1, maxSize); SortArr(arr1, "Quick sort " + message); Console.WriteLine(); }
/// <summary> /// 第三个测试,测试结果按照 Insertion, Selection, Shell 排序。 /// </summary> /// <param name="n">测试的数组长度。</param> /// <returns>测试结果。</returns> static double[] TestC(int n) { var insertionSort = new InsertionSort(); var selectionSort = new SelectionSort(); var shellSort = new ShellSort(); var random = new Random(); // 每个元素的主键均为 int 类型,并含有一个 int[20] 值。 var array = new Pair <int, int[]> [n]; var arrayBak = new Pair <int, int[]> [n]; for (var i = 0; i < n; i++) { var temp = new int[20]; for (var j = 0; j < 20; j++) { temp[j] = random.Next(); } array[i] = new Pair <int, int[]>(random.Next(), temp); } array.CopyTo(arrayBak, 0); var results = new double[3]; results[0] = SortCompare.Time(insertionSort, array); arrayBak.CopyTo(array, 0); results[1] = SortCompare.Time(selectionSort, array); arrayBak.CopyTo(array, 0); results[2] = SortCompare.Time(shellSort, array); return(results); }
public override async Task DescendingTestAsync() { var data = await GetDuplicateDataAsync(); ShellSort.SortDescending(data); CollectionAssert.AreEqual(data, DataDescendingOrdered); }
public void TestShellSort() { ShellSort ss = new ShellSort(); TestEmptyList(ss); TestRandomInts(ss); }
/// <summary> /// 第二个测试,测试结果按照 Insertion, Selection, Shell 排序。 /// </summary> /// <param name="n">测试的数组长度。</param> /// <returns>测试结果。</returns> static double[] TestB(int n) { var insertionSort = new InsertionSort(); var selectionSort = new SelectionSort(); var shellSort = new ShellSort(); var random = new Random(); // 每个元素的主键均为 double 类型,并含有 10 个 String 值(每个都至少长 10 个字符)。 var array = new Pair <double, string[]> [n]; var arrayBak = new Pair <double, string[]> [n]; for (var i = 0; i < n; i++) { var temp = new string[10]; for (var j = 0; j < 10; j++) { temp[j] = RandomString(12, random); } array[i] = new Pair <double, string[]>(random.NextDouble(), temp); } array.CopyTo(arrayBak, 0); var results = new double[3]; results[0] = SortCompare.Time(insertionSort, array); arrayBak.CopyTo(array, 0); results[1] = SortCompare.Time(selectionSort, array); arrayBak.CopyTo(array, 0); results[2] = SortCompare.Time(shellSort, array); return(results); }
private void btnOrdenar_Click(object sender, EventArgs e) { if (ListaInteiros.Count == 0) { return; } int tag = 0; foreach (Control r in groupBox1.Controls) { if (r is RadioButton && ((RadioButton)r).Checked) { tag = int.Parse(r.Tag.ToString()); } } switch (tag) { case 1: SelectSort.Sort(ListaInteiros); break; case 2: BubbleSort.Sort(ListaInteiros); break; case 3: QuickSort.Sort(ListaInteiros); break; case 4: ShellSort.Sort(ListaInteiros); break; } CarregaGrid(); }
public override async Task ComparisonTestAsync() { var data = await GetDuplicateDataAsync(); ShellSort.Sort(data, (x, y) => x.CompareTo(y)); CollectionAssert.AreEqual(data, DataAscendingOrdered); }
static void Main(string[] args) { List<int> list = new List<int>(); Random rand = new Random(); for (int i = 0; i < 100000; i++ ) { list.Add(rand.Next(-500,500)); } List<ISorting> sortings = new List<ISorting>(); HeapSort heapSort = new HeapSort(); sortings.Add(heapSort); CoctailSort coctailSort = new CoctailSort(); sortings.Add(coctailSort); ShellSort shellSort = new ShellSort(); sortings.Add(shellSort); string[] name = new string[] { "heapsort", "coctailsort", "shellsort" }; int num = 0; foreach (ISorting i in sortings) { Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); i.Sort<List<int>, int>(list); stopWatch.Stop(); Console.WriteLine("Time of {0} sort = {1}", name[num], stopWatch.Elapsed); num++; } Console.ReadKey(); }
static void Main(string[] args) { System.Console.WriteLine("\n===== Opgave 1 : Faculteit =====\n"); Opgave1.Run(); System.Console.WriteLine("\n===== Opgave 2 : Fibonacci =====\n"); Opgave2.Run(); System.Console.WriteLine("\n===== Opgave 3 : OmEnOm =====\n"); Opgave3.Run(); System.Console.WriteLine("\n===== Opgave 4 : Enen =====\n"); Opgave4.Run(); System.Console.WriteLine("\n===== Opgave 6 : ForwardString =====\n"); Opgave6.Run(); System.Console.WriteLine("\n===== Opgave 7 : Sorting =====\n"); Sorter isort = new InsertionSort(); Sorter msort = new MergeSort(); Sorter ssort = new ShellSort(); isort.Run(); msort.Run(); ssort.Run(); int[] numbers = { 100, 1000, 10000 }; foreach (int num in numbers) { isort.RunWithTimer(num); msort.RunWithTimer(num); ssort.RunWithTimer(num); } }
public void ShellSortTest() { var arr = TestData.NewUnsortedArray; var sorter = new ShellSort(); TestHelper.DoTests(arr, sorter); }
private void btnShellSort_Click(object sender, EventArgs e) //SHELLSORT BUTTON { //Declaring a new instance of ShellSort and displaying properties to the Windows Form ShellSort shellSort = new ShellSort(); richTextBox2.AppendText(shellSort.TimeComplexity + "\n"); richTextBox2.AppendText(shellSort.SpaceComplexity); richTextBox2.AppendText("\n*All Big-O values represent worst-case scenarios unless otherwise noted"); //Displaying the Advantages and Disadvantages of ShellSort to the Windows Form StringBuilder qs = new StringBuilder(); qs.Append("SHELL SORT:\nShell sort algorithm is also known as Shell's method algorithm which is the variation of insertion sort algorithm. " + "Shell sort algorithm is an improved form of insertion sort algorithm as it compares elements separated by a gap of several position. " + "In Shell sort algorithm, the elements in an array are sorted in multiple passes and in each pass, data are taken with smaller and smaller " + "gap sizes. However, the finale of shell sort algorithm is a plain insertion sort algorithm. But the time we reach the last part, " + "the elements in an array are already sorted, thus Shell sort algorithm provides better performance." + "\n\nAdvantages of Shell Sort:\n -Shell sort algorithm is only efficient for finite number of elements in an array." + "\n -Shell sort algorithm is 5.32 x faster than bubble sort algorithm.\n\nDisadvantages of Shell Sort:" + "\n -Shell sort algorithm is complex in structure and bit more difficult to understand.\n -Shell sort algorithm is significantly " + "slower than the merge sort, quick sort and heap sort algorithms."); richTextBox1.AppendText(qs.ToString()); //Displaying extra notes from a rich text file in the bin using (StreamReader Reader = new StreamReader(@"C:\Users\Clayt\source\repos\CSC205\CSC205_StudyProject\CSC205_StudyProject\bin\ShellSort.rtf")) { while (!Reader.EndOfStream) { richTextBox5.AppendText(Reader.ReadLine()); } } }
public void ShellSort() { var characters = "SHELLSORTEXAMPLE".ToCharArray(); var shellSort = new ShellSort <char>(); shellSort.Go(characters); Assert.AreEqual("AEEEHLLLMOPRSSTX", new string(characters)); }
public void TestMethod2() { int[] arr = { 1, 3, 2 }; ShellSort sort = new ShellSort(); sort.Sort(arr); Assert.AreEqual(3, arr[2]); }
public void ShellSort_Smoke_Test() { var result = ShellSort <int> .Sort(TestArray); for (int i = 0; i < TestArray.Length; i++) { Assert.AreEqual(i, result[i]); } }
public void SortTest() { String[] a = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }; Shuffle.Do(a); Assert.IsFalse(SortHelper.IsSorted(a)); ShellSort.Sort(a); Assert.IsTrue(SortHelper.IsSorted(a)); }
public void SortTest(int[] array) { // Arrange // Act ShellSort.Sort(array); // Assert AssertEx.IsOrdered(array); }
public void ShellSort_Descending_Smoke_Test() { var result = ShellSort <int> .Sort(testArray, SortDirection.Descending); for (int i = 0; i < testArray.Length; i++) { Assert.AreEqual(testArray.Length - i - 1, result[i]); } }
private void ShellSortButton_Click(object sender, EventArgs e) { ShellLabel.Text = "Time: "; var Shell = new ShellSort <int>(Collection, "ShellSort"); Shell.Sort(); DisplayInfoFromSort(Shell); ShellLabel.Text += Shell.Time.ToString(); }
static void Main(string[] args) { int N = 1000; InsertionSort insertion = new InsertionSort(); SelectionSort selection = new SelectionSort(); ShellSort shell = new ShellSort(); double prevInsertion = 0; double prevSelection = 0; double prevShell = 0; for (int i = 0; i < 10; i++) { Console.WriteLine("N:" + N); int[] array = SortCompare.GetRandomArrayInt(N); int[] arrayBak = new int[N]; array.CopyTo(arrayBak, 0); Console.WriteLine("\tInsertion Sort"); double now = SortCompare.Time(insertion, array); Console.WriteLine("\t\tActual Time(ms):" + now); if (i != 0) { Console.WriteLine("\t\tEstimate Time(ms):" + prevInsertion * 4); Console.WriteLine("\t\tRatio:" + now / prevInsertion); } prevInsertion = now; arrayBak.CopyTo(array, 0); Console.WriteLine("\tSelection Sort"); now = SortCompare.Time(selection, array); Console.WriteLine("\t\tActual Time(ms):" + now); if (i != 0) { Console.WriteLine("\t\tEstimate Time(ms):" + prevSelection * 4); Console.WriteLine("\t\tRatio:" + now / prevSelection); } prevSelection = now; arrayBak.CopyTo(array, 0); Console.WriteLine("\tShell Sort"); now = SortCompare.Time(shell, array); Console.WriteLine("\t\tActual Time(ms):" + now); if (i != 0) { Console.WriteLine("\t\tEstimate Time(ms):" + prevShell * 2); Console.WriteLine("\t\tRatio:" + now / prevShell); } prevShell = now; N *= 2; } }
public void SortWithComparerTest_DESCOrder_NullComparer() { Point[] points = new Point[10]; for (int i = 0; i < 10; i++) { points[i] = new Point(i, 10 - i); } ShellSort.Sort(points, null, SortOrder.DESC); }
public void TestCaseAscending() { SampleArray <int> .Get(out int[] arr, out int[] arrCopy, () => _random.Next(0, 100)); Array.Sort(arrCopy); var sort = new ShellSort <int>(); sort.Sort(arr); Assert.Equal(arr, arrCopy); }
public void TestCaseDescending() { SampleArray <int> .Get(out int[] arr, out int[] arrCopy, () => _random.Next(0, 100)); Array.Sort(arrCopy, new Comparison <int>((x, y) => y.CompareTo(x))); var sort = new ShellSort <int>(Comparer <int> .Create(new Comparison <int>((x, y) => y.CompareTo(x)))); sort.Sort(arr); Assert.Equal(arr, arrCopy); }
public Data put(TKey key, TValue value) { this.datas[this.N++] = new Data(key, value); ShellSort.sort(this.datas); if (this.N == this.datas.Length) { return(this.datas[(this.N + 1) / 2]); } return(null); }
private void ShellSort_Click(object sender, EventArgs e) { panel4.Controls.Clear(); var listForSort = new ShellSort <SortedItem>(items); var NewItems = DisplayList(listForSort.Items); listForSort = new ShellSort <SortedItem>(NewItems); StartSorting(listForSort); }
// Driver method public static void Main() { int [] arr = { 12, 34, 54, 2, 3, 1, 10, 21, 30, -9, 0, -1 }; ShellSort ob = new ShellSort(); ob.sort(arr); Console.Write("RESULT :\n"); printArray(arr); }
static void Main(string[] args) { int counter = 100; List<IComparable> values = new List<IComparable>(); var random = new Random(DateTime.Now.Millisecond); for (int i = 0; i < counter; i++) { values.Add(new UserType(random.Next(0, 1000))); } var bubbleSort = new BubbleSort<IComparable, IList<IComparable>>(); RunAlgorithm(values, bubbleSort); var shelllSort = new ShellSort<IComparable, IList<IComparable>>(); RunAlgorithm(values, shelllSort); var quickSort = new QuickSort<IComparable, IList<IComparable>>(); RunAlgorithm(values, quickSort); Console.ReadKey(); }
public static long getSortTime(SORT_TYPE type, int[] array) { var attempts = new long[Form1.ATTEMPTS]; for (var j = 0; j < Form1.ATTEMPTS; j++) { switch (type) { case SORT_TYPE.SORT: myStopwatch.Restart(); Array.Sort(array); myStopwatch.Stop(); break; case SORT_TYPE.QUICKSORT: Quicksort<int> q = new Quicksort<int>(); myStopwatch.Restart(); q.QSort(array); myStopwatch.Stop(); break; case SORT_TYPE.HEAPSORT: Heapsort h = new Heapsort(); myStopwatch.Restart(); h.heapSort(array); myStopwatch.Stop(); break; case SORT_TYPE.SHELLSORT: ShellSort<int> s = new ShellSort<int>(); myStopwatch.Restart(); s.Sort(array); myStopwatch.Stop(); break; default: throw new Exception("Unknown error"); } attempts[j] = myStopwatch.ElapsedMilliseconds; } Array.Sort(attempts); return attempts[Form1.ATTEMT_NUMBER_TO_USE-1]; }
public void ShellSortTest() { //Arrange var shellSort = new ShellSort<string>(); var arrayToSort = new string[_q2.Length]; _q2.CopyTo(arrayToSort, 0); //Act arrayToSort.Sort(shellSort); //Assert Assert.IsTrue(arrayToSort.IsSorted()); }
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(); }