public static void testResultsDemonstration(int[] tab, IAlgorithm alg) { SortAlgorithms.ShowTab(tab); alg.Run(tab); SortAlgorithms.ShowTab(tab); Console.WriteLine(); }
public static void testExecTime(int[] tab, IAlgorithm alg) { SortAlgorithms.AlgorithmDelegate Algorithm_Delegate = new SortAlgorithms.AlgorithmDelegate(alg.Run); var time = SortAlgorithms.timeOfExecution(tab, Algorithm_Delegate); Console.WriteLine($"time: {time}"); }
public void Run(int[] tab) { for (int i = 1; i < tab.Length; i++) { int j = i; while (j > 0 && tab[j - 1] > tab[j]) { SortAlgorithms.swap(ref tab[j - 1], ref tab[j]); j--; } } }
public void Run(int[] tab) { bool swapped = true; while (swapped) { swapped = false; for (int i = 1; i < tab.Length; i++) { if (tab[i - 1] > tab[i]) { SortAlgorithms.swap(ref tab[i - 1], ref tab[i]); swapped = true; } } } }
static void Main(string[] args) { IAlgorithm bubble = new BubbleSort(); IAlgorithm insertion = new InsertionSort(); int[] tab = { 5, 2, 9, 3, 7, 1, 6, 4, 8 }; int[] tab2 = SortAlgorithms.GenerateRandomArray(25, 0, 29); int[] tab3 = SortAlgorithms.GenerateRandomArray(25); int[] tab4 = SortAlgorithms.GenerateRandomArray(15, 100); int[] tab5 = SortAlgorithms.GenerateRandomArray(25000, 0, 1000000); testResultsDemonstration((int[])tab.Clone(), bubble); testResultsDemonstration((int[])tab.Clone(), insertion); // testExecTime((int[])tab5.Clone(), bubble); // testExecTime((int[])tab5.Clone(), insertion); testResultsDemonstration((int[])tab2.Clone(), SortAlgorithms.insertionSort); testExecTime((int[])tab2.Clone(), SortAlgorithms.insertionSort); Console.ReadLine(); }
public static int[] GenerateRandomArray(int len, int minValue) { int[] tab = new int[len]; Random rnd = new Random(); for (int i = 0; i < tab.Length; i++) { tab[i] = i + minValue; } for (int i = 0; i < tab.Length; i++) { int firstIdx = rnd.Next(tab.Length); int secondIdx = rnd.Next(tab.Length); if (firstIdx == secondIdx) { continue; } SortAlgorithms.swap(ref tab[firstIdx], ref tab[secondIdx]); } return(tab); }