public void MergesortHundredElements() { SortList list = new SortList(100); Mergesort.mergesort(list); Assert.IsTrue(list.isSorted()); }
//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"; } }
public void MergesortFourElements() { SortList list = new SortList(4); Mergesort.mergesort(list); Assert.IsTrue(list.isSorted()); }
public void MergesortEfficiently() { SortList list = new SortList(100); Mergesort.mergesort(list); Assert.LessOrEqual(list.Gets, 700); Assert.LessOrEqual(list.Sets, 700); }
public static void Main(string[] args) { int[] arr = Console.ReadLine().Split().Select(int.Parse).ToArray(); int result = Mergesort.Sort(arr); Console.WriteLine(result); }
static void Main(string[] args) { int[] array = Console.ReadLine().Split().Select(int.Parse).ToArray(); Mergesort <int> .Sort(array); Console.WriteLine(string.Join(" ", array)); }
public void MergeSortSorted() { var list = new SortList(1000, true); Mergesort.mergesort(list); TestContext.Out.WriteLine("Gets: " + list.Gets); TestContext.Out.WriteLine("Sets: " + list.Sets); TestContext.Out.WriteLine("Swaps: " + list.Swaps); TestContext.Out.WriteLine("Comparisons: " + list.Comparisons); }
public static void Main(string[] args) { int[] arr = Console.ReadLine().Split().Select(int.Parse).ToArray(); Mergesort <int> .Sort(arr); StringBuilder builder = new StringBuilder(); foreach (var num in arr) { builder.Append(num + " "); } Console.WriteLine(builder); }
public void Sort_DuplicateItems_SortTheGivenList() { Mergesort mergeSort = new Mergesort(); List <int> toSort = new List <int> { 5, 3, 1, 5, 9 }; mergeSort.Sort(toSort); List <int> expected = new List <int> { 1, 3, 5, 5, 9 }; CollectionAssert.AreEqual(expected, toSort); }
public void Sort_OneItemInTheList_DoesNotModifyTheList() { Mergesort mergeSort = new Mergesort(); List <int> toSort = new List <int> { 2 }; mergeSort.Sort(toSort); List <int> expected = new List <int> { 2 }; CollectionAssert.AreEqual(expected, toSort); }
private static bool sortTest5() { int[] input = new int[1000000]; int[] solution = new int[1000000]; int randNum; Random rand = new Random(); for (int i = 0; i < input.Length; i++) { randNum = rand.Next(0, 1000000); input[i] = randNum; solution[i] = randNum; } input = Mergesort.compute(input); Array.Sort(solution); return(isSorted(input) && input.SequenceEqual(solution)); }
static void Main(string[] args) { string input = Console.ReadLine(); if (input == String.Empty) { return; } int[] arr = input .Split() .Select(int.Parse) .ToArray(); Mergesort <int> .Sort(arr); Console.WriteLine(String.Join(" ", arr)); }
static void Main(string[] args) { int[] arrayLength = { 100, 500, 1000, 5000, 10000, 50000, 100000 }; Insertionsort insertionSort = new Insertionsort(); Mergesort mergeSort = new Mergesort(); Quicksort quickSort = new Quicksort(); Radixsort radixSort = new Radixsort(); Selectionsort selectionSort = new Selectionsort(); Shellsort shellSort = new Shellsort(); TestArrays arrayGen = new TestArrays(); for (int i = 0; i < arrayLength.Length; i++) { int[] mass = arrayGen.sortedArray(arrayLength[i]); insertionSort.SortArray(mass); mergeSort.SortArray(mass); quickSort.SortArray(mass); radixSort.SortArray(mass); selectionSort.SortArray(mass); shellSort.SortArray(mass); } for (int i = 0; i < arrayLength.Length; i++) { int[] mass = arrayGen.randomArray(arrayLength[i]); insertionSort.SortArray(mass); mergeSort.SortArray(mass); quickSort.SortArray(mass); radixSort.SortArray(mass); selectionSort.SortArray(mass); shellSort.SortArray(mass); } for (int i = 0; i < arrayLength.Length; i++) { int[] mass = arrayGen.reverseArray(arrayLength[i]); insertionSort.SortArray(mass); mergeSort.SortArray(mass); quickSort.SortArray(mass); radixSort.SortArray(mass); selectionSort.SortArray(mass); shellSort.SortArray(mass); } }
static void Main() { int cant = 250, randNum, a = 1, b = cant; Random rand = new Random(); int[] arreglo = new int[cant]; for (int i = 0; i < cant; i++) { do { randNum = rand.Next(a, b + 1); } while (Array.IndexOf(arreglo, randNum) >= 0); arreglo[i] = randNum; } Mergesort.MSRecursivo(arreglo, 0, arreglo.Length - 1); foreach (int num in arreglo) { Console.Write("{0} -> ", num); } }
private static bool sortTest3() { return(Mergesort.compute(new int[] { 10 }).SequenceEqual(new int[] { 10 })); }
public void MergesortOneElement() { SortList list = new SortList(1); Mergesort.mergesort(list); }
private static void Main() { var sijainti = Environment.CurrentDirectory + @"\tulokset\"; while (true) { try { Directory.CreateDirectory(sijainti); Console.Write("Säikeiden lukumäärä: "); Tyolaiset = Convert.ToInt32(Console.ReadLine()); if (((int)(Math.Ceiling((Math.Log(Tyolaiset) / Math.Log(2)))) != (int)(Math.Floor((Math.Log(Tyolaiset) / Math.Log(2))))) || Tyolaiset < 2) { throw new Exception(); } Console.Write("Alkioiden lukumäärä per taulukko: "); AlkioidenMaara = Convert.ToInt32(Console.ReadLine()); Console.Write("Ajojen lukumäärä: "); AjojenMaara = Convert.ToInt32(Console.ReadLine()); Console.Write("Käytettävä algoritmi ([m]ergesort / [q]uicksort): "); Algoritmi = Console.ReadLine()?.ToLower(); if (!(Algoritmi == "m" || Algoritmi == "mergesort" || Algoritmi == "q" || Algoritmi == "quicksort")) { throw new Exception(); } break; } catch (Exception) { Console.WriteLine("Virhe syötteissä, yritä uudelleen (Huom! Säikeet kahden potensseina (2,4,8..))."); } } if (Algoritmi == "m") { Algoritmi = "mergesort"; } if (Algoritmi == "q") { Algoritmi = "quicksort"; } var tiedostonNimi = Algoritmi + "_Alkioita" + AlkioidenMaara + "_Tyolaisia" + Tyolaiset + ".csv"; using var writer = new StreamWriter(sijainti + tiedostonNimi, true, Encoding.UTF8); var csv = new CsvWriter(writer, CultureInfo.InvariantCulture); csv.Configuration.RegisterClassMap <TuloksetMap>(); var kaikkiSuoritusajat = new List <Tulokset>(); for (var indeksi = 0; indeksi < AjojenMaara; indeksi++) { Console.WriteLine("Suoritetaan " + (indeksi + 1) + ". ajoa"); var sekventiaalinenKellotus = new Stopwatch(); var rinnakkainenKellotus = new Stopwatch(); var sekventiaalinenTaulukko = GeneroiListaSatunnaisistaAlkioista(AlkioidenMaara, 0, 2147483647); var rinnakkainenTaulukko = (int[])sekventiaalinenTaulukko.Clone(); if (Algoritmi == "mergesort") { var mergesort = new Mergesort(); sekventiaalinenKellotus.Start(); mergesort.SekventiaalinenJarjestys(sekventiaalinenTaulukko); sekventiaalinenKellotus.Stop(); rinnakkainenKellotus.Start(); mergesort.RinnakkainenJarjestys(Tyolaiset, rinnakkainenTaulukko); rinnakkainenKellotus.Stop(); if (sekventiaalinenTaulukko.SequenceEqual(rinnakkainenTaulukko) && Jarjestetty(sekventiaalinenTaulukko)) { var suoritusajat = new Tulokset { RinnakkainenSuoritusaika = rinnakkainenKellotus.ElapsedMilliseconds, SekventiaalinenSuoritusaika = sekventiaalinenKellotus.ElapsedMilliseconds }; kaikkiSuoritusajat.Add(suoritusajat); } else { Console.WriteLine("Virhe mergesortin järjestämisessä"); } } else if (Algoritmi == "quicksort") { var quicksort = new Quicksort(); sekventiaalinenKellotus.Start(); quicksort.SekventiaalinenJarjestys(sekventiaalinenTaulukko); sekventiaalinenKellotus.Stop(); rinnakkainenKellotus.Start(); quicksort.RinnakkainenJarjestys(Tyolaiset, rinnakkainenTaulukko); rinnakkainenKellotus.Stop(); if (sekventiaalinenTaulukko.SequenceEqual(rinnakkainenTaulukko) && Jarjestetty(sekventiaalinenTaulukko)) { var suoritusajat = new Tulokset { RinnakkainenSuoritusaika = rinnakkainenKellotus.ElapsedMilliseconds, SekventiaalinenSuoritusaika = sekventiaalinenKellotus.ElapsedMilliseconds }; kaikkiSuoritusajat.Add(suoritusajat); } else { Console.WriteLine("Virhe quicksortin järjestämisessä"); } } else { Console.WriteLine("Väärä syöte algoritmiin"); break; } } Console.WriteLine(kaikkiSuoritusajat.Count == AjojenMaara ? "Kaikki suoritukset kirjattu tiedostoon." : "Osa suorituksista jäi kirjaamatta tiedostoon."); csv.WriteRecords(kaikkiSuoritusajat); writer.Close(); }
private static bool sortTest1() { return(Mergesort.compute(new int[] { 3, 9, 1, 4, 7 }).SequenceEqual(new int[] { 1, 3, 4, 7, 9 })); }
public void Sort_SortingNull_ThrowsException() { Mergesort mergeSort = new Mergesort(); }
public void sortBtn_Click(object sender, EventArgs e) { if (listViewSearchSort.Items.Count > 0) { if (comboSrt.SelectedIndex == 0) { string[] items = new string[listViewSearchSort.Items.Count]; for (int i = 0; i < listViewSearchSort.Items.Count; i++) { string a = ""; for (int b = 0; b < 5; b++) { if (b == 4) { a += listViewSearchSort.Items[i].SubItems[b].Text; } else { a += listViewSearchSort.Items[i].SubItems[b].Text + ","; } } items[i] = a; } List <DataAsc> list = new List <DataAsc>(); for (int i = 0; i < items.Length; i++) { string a; a = items[i]; dynamic b; b = a.Split(','); list.Add(new DataAsc() { Date = Convert.ToDateTime(b[0]), TransID = b[1].ToString(), Payment = b[2].ToString(), Price = Convert.ToInt32(b[3]), Cashier = b[4].ToString() }); } Mergesort.sort <DataAsc>(ref list); listViewSearchSort.Items.Clear(); foreach (var lists in list) { string a = Convert.ToString(lists); dynamic b; b = a.Split(','); var listViewItem = new ListViewItem(b); listViewSearchSort.Items.Add(listViewItem); } } else { string[] items = new string[listViewSearchSort.Items.Count]; for (int i = 0; i < listViewSearchSort.Items.Count; i++) { string a = ""; for (int b = 0; b < 5; b++) { if (b == 4) { a += listViewSearchSort.Items[i].SubItems[b].Text; } else { a += listViewSearchSort.Items[i].SubItems[b].Text + ","; } } items[i] = a; } List <DataDesc> list = new List <DataDesc>(); for (int i = 0; i < items.Length; i++) { string a; a = items[i]; dynamic b; b = a.Split(','); list.Add(new DataDesc() { Date = Convert.ToDateTime(b[0]), TransID = b[1].ToString(), Payment = b[2].ToString(), Price = Convert.ToInt32(b[3]), Cashier = b[4].ToString() }); } Mergesort.sort <DataDesc>(ref list); listViewSearchSort.Items.Clear(); foreach (var lists in list) { string a = Convert.ToString(lists); dynamic b; b = a.Split(','); var listViewItem = new ListViewItem(b); listViewSearchSort.Items.Add(listViewItem); } } } else { MessageBox.Show("cant"); } }