internal static void Main(string[] args) { var collection = new SortableCollection <int>(new[] { 22, 11, 101, 33, 0, 101 }); Console.WriteLine("All items before sorting:"); collection.PrintAllItemsOnConsole(); Console.WriteLine(); collection = new SortableCollection <int>(new[] { 22, 11, 101, 33, 0, 101 }); Console.WriteLine("SelectionSorter result:"); collection.Sort(new SelectionSorter <int>()); collection.PrintAllItemsOnConsole(); Console.WriteLine(); collection = new SortableCollection <int>(new[] { 22, 11, 101, 33, 0, 101 }); Console.WriteLine("BubbleSorter result:"); collection.Sort(new BubbleSorter <int>()); collection.PrintAllItemsOnConsole(); Console.WriteLine(); collection = new SortableCollection <int>(new[] { 22, 11, 101, 33, 0, 101 }); Console.WriteLine("InsertionSorter result:"); collection.Sort(new InsertionSorter <int>()); collection.PrintAllItemsOnConsole(); Console.WriteLine(); collection = new SortableCollection <int>(new[] { 22, 11, 101, 33, 0, 101 }); Console.WriteLine("Quicksorter result:"); collection.Sort(new QuickSorter <int>()); collection.PrintAllItemsOnConsole(); Console.WriteLine(); collection = new SortableCollection <int>(new[] { 22, 11, 101, 33, 0, 101 }); Console.WriteLine("MergeSorter result:"); collection.Sort(new MergeSorter <int>()); collection.PrintAllItemsOnConsole(); Console.WriteLine(); Console.WriteLine("Linear search 101:"); Console.WriteLine(collection.LinearSearch(101)); Console.WriteLine(); Console.WriteLine("Binary search 101:"); Console.WriteLine(collection.LinearSearch(101)); Console.WriteLine(); Console.WriteLine("Shuffle:"); collection.Shuffle(); collection.PrintAllItemsOnConsole(); Console.WriteLine(); Console.WriteLine("Shuffle again:"); collection.Shuffle(); collection.PrintAllItemsOnConsole(); }
internal static void Main(string[] args) { var collection = new SortableCollection <int>(new[] { 22, 11, 101, 33, 0, 101 }); Console.WriteLine("All items before sorting:"); collection.PrintAllItemsOnConsole(); Console.WriteLine(); Console.WriteLine("Linear search 101:"); Console.WriteLine(collection.LinearSearch(101)); Console.WriteLine(); Console.WriteLine("Binary search 101:"); Console.WriteLine(collection.BinarySearch(101)); Console.WriteLine(); Console.WriteLine("Shuffle:"); collection.Shuffle(); collection.PrintAllItemsOnConsole(); Console.WriteLine(); Console.WriteLine("Shuffle again:"); collection.Shuffle(); collection.PrintAllItemsOnConsole(); }
public void LinearSearchLast() { var collection = new SortableCollection <int>(new[] { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }); bool result = collection.LinearSearch(0); Assert.IsTrue(result); }
internal static void Main(string[] args) { var sw = new Stopwatch(); var collection = new SortableCollection <int>(new[] { 22, 11, 101, 33, 0, 101 }); Console.WriteLine("All items before sorting:"); collection.PrintAllItemsOnConsole(); Console.WriteLine(); Console.WriteLine("SelectionSorter result:"); sw.Start(); collection.Sort(new SelectionSorter <int>()); Console.WriteLine("Time : {0}", sw.ElapsedTicks); collection.PrintAllItemsOnConsole(); Console.WriteLine(); collection = new SortableCollection <int>(new[] { 22, 11, 101, 33, 0, 101 }); Console.WriteLine("Quicksorter result:"); sw.Restart(); collection.Sort(new Quicksorter <int>()); Console.WriteLine("Time : {0}", sw.ElapsedTicks); collection.PrintAllItemsOnConsole(); Console.WriteLine(); collection = new SortableCollection <int>(new[] { 22, 11, 101, 33, 0, 101 }); Console.WriteLine("MergeSorter result:"); sw.Restart(); collection.Sort(new MergeSorter <int>()); Console.WriteLine("Time : {0}", sw.ElapsedTicks); collection.PrintAllItemsOnConsole(); Console.WriteLine(); Console.WriteLine("Linear search 101:"); sw.Restart(); Console.WriteLine(collection.LinearSearch(101)); Console.WriteLine("Time : {0}", sw.ElapsedTicks); Console.WriteLine(); Console.WriteLine("Binary search 101:"); sw.Restart(); Console.WriteLine(collection.BinarySearch(101)); Console.WriteLine("Time : {0}", sw.ElapsedTicks); Console.WriteLine(); Console.WriteLine("Shuffle:"); sw.Restart(); collection.Shuffle(); Console.WriteLine("Time : {0}", sw.ElapsedTicks); collection.PrintAllItemsOnConsole(); Console.WriteLine(); Console.WriteLine("Shuffle again:"); sw.Restart(); collection.Shuffle(); Console.WriteLine("Time : {0}", sw.ElapsedTicks); collection.PrintAllItemsOnConsole(); }
internal static void Main(string[] args) { var collection = new SortableCollection <int>(new[] { 22, -2, 300, 11, 55, 33, 10, -15, 88, 101, 33, 0, 101, 44, 33 }); Console.WriteLine("All items before sorting:"); collection.PrintAllItemsOnConsole(); Console.WriteLine(); Console.WriteLine("SelectionSorter result:"); collection.Sort(new SelectionSorter <int>()); collection.PrintAllItemsOnConsole(); Console.WriteLine(); collection = new SortableCollection <int>(new[] { 22, -2, 300, 11, 55, 33, 10, -15, 88, 101, 33, 0, 101, 44, 33 }); Console.WriteLine("Quicksorter result:"); var quickSorter = new Quicksorter <int>(); collection.Sort(quickSorter); quickSorter.PrintResults(); //collection.PrintAllItemsOnConsole(); Console.WriteLine(); collection = new SortableCollection <int>(new[] { 22, -2, 300, 11, 55, 33, 10, -15, 88, 101, 33, 0, 101, 44, 33 }); Console.WriteLine("MergeSorter result:"); var mergeSort = new MergeSorter <int>(); collection.Sort(mergeSort); mergeSort.ShowResults(); //collection.PrintAllItemsOnConsole(); Console.WriteLine(); Console.WriteLine("Linear search 101:"); Console.WriteLine(collection.LinearSearch(101)); Console.WriteLine(); Console.WriteLine("Binary search 101:"); Console.WriteLine(collection.BinarySearch(101)); Console.WriteLine(); collection = new SortableCollection <int>(new int[20]); for (int i = 1; i <= 20; i++) { collection.Items[i - 1] = i; } Console.WriteLine("All items before sorting:"); collection.PrintAllItemsOnConsole(); Console.WriteLine("Shuffle:"); collection.Shuffle(); collection.PrintAllItemsOnConsole(); Console.WriteLine(); Console.WriteLine("Shuffle again:"); collection.Shuffle(); collection.PrintAllItemsOnConsole(); }
internal static void Main(string[] args) { var randomArr = GetRandomArr(15); randomArr[15 / 2] = 101; var collection = new SortableCollection <int>(randomArr); Console.WriteLine("All items before sorting:"); collection.PrintAllItemsOnConsole(); Console.WriteLine(); Console.WriteLine("SelectionSorter result:"); collection.Sort(new SelectionSorter <int>()); collection.PrintAllItemsOnConsole(); Console.WriteLine(); collection = new SortableCollection <int>(randomArr); Console.WriteLine("Quicksorter result:"); collection.Sort(new Quicksorter <int>()); collection.PrintAllItemsOnConsole(); Console.WriteLine(); collection = new SortableCollection <int>(randomArr); Console.WriteLine("MergeSorter result:"); collection.Sort(new MergeSorter <int>()); collection.PrintAllItemsOnConsole(); Console.WriteLine(); Console.WriteLine("Linear search 101:"); Console.WriteLine(collection.LinearSearch(101)); Console.WriteLine(); Console.WriteLine("Binary search 101:"); Console.WriteLine(collection.BinarySearch(101)); Console.WriteLine(); Console.WriteLine("Binary search 301:"); Console.WriteLine(collection.BinarySearch(301)); Console.WriteLine(); Console.WriteLine("Shuffle:"); collection.Shuffle(); collection.PrintAllItemsOnConsole(); Console.WriteLine(); Console.WriteLine("Shuffle again:"); collection.Shuffle(); collection.PrintAllItemsOnConsole(); }
internal static void Main(string[] args) { var collection = new SortableCollection <int>(new[] { 22, 11, 101, 33, 0, 101 }); Console.WriteLine("All items before sorting:"); collection.PrintAllItemsOnConsole(); Console.WriteLine(); /// /// Uncomment the sort you want to see and leave /// others commented /// //Console.WriteLine("SelectionSorter result:"); //collection.Sort(new SelectionSorter<int>()); //collection.PrintAllItemsOnConsole(); //Console.WriteLine(); Console.WriteLine("Quicksorter result:"); collection.Sort(new Quicksorter <int>()); collection.PrintAllItemsOnConsole(); Console.WriteLine(); //Console.WriteLine("MergeSorter result:"); //collection.Sort(new MergeSorter<int>()); //collection.PrintAllItemsOnConsole(); //Console.WriteLine(); Console.WriteLine("Linear search 101:"); Console.WriteLine(collection.LinearSearch(101)); Console.WriteLine(); Console.WriteLine("Binary search 101:"); Console.WriteLine(collection.BinarySearch(101)); Console.WriteLine(); Console.WriteLine("Shuffle:"); collection.Shuffle(); collection.PrintAllItemsOnConsole(); Console.WriteLine(); Console.WriteLine("Shuffle again:"); collection.Shuffle(); collection.PrintAllItemsOnConsole(); }
internal static void Main(string[] args) { var collection = new SortableCollection <int>(new[] { 22, 11, 101, 33, 0, 101 }); Console.WriteLine("All items before sorting:"); collection.PrintAllItemsOnConsole(); Console.WriteLine(); Console.WriteLine("SelectionSorter result:"); collection.Sort(new SelectionSorter <int>()); collection.PrintAllItemsOnConsole(); Console.WriteLine(); collection = new SortableCollection <int>(new[] { 22, 11, 101, 33, 0, 101 }); Console.WriteLine("Quicksorter result:"); collection.Sort(new Quicksorter <int>()); collection.PrintAllItemsOnConsole(); Console.WriteLine(); collection = new SortableCollection <int>(new[] { 22, 11, 101, 33, 0, 101 }); Console.WriteLine("MergeSorter result:"); collection.Sort(new MergeSorter <int>()); collection.PrintAllItemsOnConsole(); Console.WriteLine(); // Could not keep the reference to collection in MergeSort => Quicksorting to proceed! collection.Sort(new Quicksorter <int>()); Console.WriteLine("Linear search 101:"); Console.WriteLine(collection.LinearSearch(101)); Console.WriteLine(); Console.WriteLine("Binary search 101:"); Console.WriteLine(collection.BinarySearch(101)); Console.WriteLine(); Console.WriteLine("Shuffle:"); collection.Shuffle(); collection.PrintAllItemsOnConsole(); Console.WriteLine(); Console.WriteLine("Shuffle again:"); collection.Shuffle(); collection.PrintAllItemsOnConsole(); }
internal static void Main(string[] args) { var collection = new SortableCollection <int>(new[] { 22, 11, 101, 33, 0, 101 }); Console.WriteLine("All items before sorting:"); collection.PrintAllItemsOnConsole(); Console.WriteLine(); Console.WriteLine("SelectionSorter result:"); collection.Sort(new SelectionSorter <int>()); collection.PrintAllItemsOnConsole(); Console.WriteLine(); collection = new SortableCollection <int>(new[] { 22, 11, 101, 33, 0, 101 }); Console.WriteLine("Quicksorter result:"); collection.Sort(new Quicksorter <int>()); collection.PrintAllItemsOnConsole(); Console.WriteLine(); collection = new SortableCollection <int>(new[] { 22, 11, 101, 33, 0, 101 }); Console.WriteLine("MergeSorter result:"); collection.Sort(new MergeSorter <int>()); collection.PrintAllItemsOnConsole(); Console.WriteLine(); Console.WriteLine("Linear search 101:"); Console.WriteLine(collection.LinearSearch(101)); Console.WriteLine(); Console.WriteLine("Binary search 101:"); Console.WriteLine(collection.BinarySearch(101)); Console.WriteLine(); // Fisher–Yates shuffle // Complexity O(n) https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle Console.WriteLine("Shuffle:"); collection.Shuffle(); collection.PrintAllItemsOnConsole(); Console.WriteLine(); Console.WriteLine("Shuffle again:"); collection.Shuffle(); collection.PrintAllItemsOnConsole(); }
internal static void Main(string[] args) { var collection = new SortableCollection <int>(new[] { 77, 332, 651, 98, 1, 5454, 34, 1, 656, 1, 22, 11, 101, 33, 0, 101 }); Console.WriteLine("All items before sorting:"); collection.PrintAllItemsOnConsole(); Console.WriteLine(); Console.WriteLine("SelectionSorter result:"); collection.Sort(new SelectionSorter <int>()); collection.PrintAllItemsOnConsole(); Console.WriteLine(); collection = new SortableCollection <int>(new[] { 77, 332, 651, 98, 1, 5454, 34, 1, 656, 1, 22, 11, 101, 33, 0, 101 }); Console.WriteLine("Quicksorter result:"); collection.Sort(new Quicksorter <int>()); collection.PrintAllItemsOnConsole(); Console.WriteLine(); collection = new SortableCollection <int>(new[] { 77, 332, 651, 98, 1, 5454, 34, 1, 656, 1, 22, 11, 101, 33, 0, 101 }); Console.WriteLine("MergeSorter result:"); collection.Sort(new MergeSorter <int>()); collection.PrintAllItemsOnConsole(); Console.WriteLine(); Console.WriteLine("Linear search 11:"); Console.WriteLine(collection.LinearSearch(11)); Console.WriteLine(); Console.WriteLine("Binary search 11:"); Console.WriteLine(collection.BinarySearch(11)); Console.WriteLine(); Console.WriteLine("Shuffle:"); collection.Shuffle(); collection.PrintAllItemsOnConsole(); Console.WriteLine(); Console.WriteLine("Shuffle again:"); collection.Shuffle(); collection.PrintAllItemsOnConsole(); }