예제 #1
0
        static void Main(string[] args)
        {
            int[] arr = { 10, 80, 30, 90, 40, 50, 70 };

            BubbleSortAlgorithm bubbleSort = new BubbleSortAlgorithm();

            InsertSortAlgorithm insertSort = new InsertSortAlgorithm();

            ShellSortAlgorithm shellSort = new ShellSortAlgorithm();

            HeapSortAlgorithm heapSort = new HeapSortAlgorithm();

            SelectionSortAlgorithm selectSort = new SelectionSortAlgorithm();

            MergeSortAlgorithm mergeSort = new MergeSortAlgorithm();

            QuickSortAlgorithm quickSort = new QuickSortAlgorithm();


            arr = quickSort.Sort(arr);

            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i]);
                Console.Write(' ');
            }
            Console.WriteLine();

            Console.ReadLine();
        }
예제 #2
0
        public static void ExecuteSolution()
        {
            int orderCount = int.Parse(Console.ReadLine());

            var orderList = new List <Order>();

            //Fill order list
            for (int i = 1; i <= orderCount; i++)
            {
                string[] times = Console.ReadLine().Split(' ');

                var order = new Order(int.Parse(times[0]), int.Parse(times[1]));
                order.Id = orderList.Count + 1;

                orderList.Add(order);
            }

            // O(n2) Buble sort
            //BubleSortAlgorithm.SortItems<Order>(orderList);

            //O(nlogn)
            MergeSortAlgorithm.SortItems <Order>(orderList, 0, orderList.Count - 1);

            DisplayResults(orderList);
        }
예제 #3
0
        public void MergeSorTest()
        {
            var rand      = new Random();
            var array     = Enumerable.Range(0, 21).Select(x => rand.Next(-20, 21)).ToArray();
            var algorithm = new MergeSortAlgorithm();

            AssertSort(array, algorithm);
        }
예제 #4
0
        public void MergeSort_10NegativeNumbers_SortedArray()
        {
            int[] testArray     = { -6, -37, -15, -49, -1, -111, -8, -6, -23, -4 };
            int[] expectedArray = { -111, -49, -37, -23, -15, -8, -6, -6, -4, -1 };

            int[] actualArray = MergeSortAlgorithm.MergeSort(testArray);

            CollectionAssert.AreEqual(expectedArray, actualArray);
        }
예제 #5
0
        public void MergeSort_10PositiveNumbers_SortedArray()
        {
            int[] testArray     = { 6, 15, 2, 9, 28, 63, 48, 6, 15, 3 };
            int[] expectedArray = { 2, 3, 6, 6, 9, 15, 15, 28, 48, 63 };

            int[] actualArray = MergeSortAlgorithm.MergeSort(testArray);

            CollectionAssert.AreEqual(expectedArray, actualArray);
        }
예제 #6
0
        public void MergeSort_10PositiveAndNegativeNumbers_SortedArray()
        {
            int[] testArray     = { -9, 15, 0, -89, 13, 8, -46, -7, 7, 12 };
            int[] expectedArray = { -89, -46, -9, -7, 0, 7, 8, 12, 13, 15 };

            int[] actualArray = MergeSortAlgorithm.MergeSort(testArray);

            CollectionAssert.AreEqual(expectedArray, actualArray);
        }
예제 #7
0
 private void SetupScenery5()
 {
     merge     = new MergeSortAlgorithm <int>(new MyComparer());
     testArray = new int[100];
     for (int i = 0; i < 100; i++)
     {
         testArray[i] = i + 1;
     }
 }
예제 #8
0
 public static void MergeSort(int[] input, int low, int high)
 {
     if (low < high)
     {
         int middle = (low / 2) + (high / 2);
         MergeSort(input, low, middle);
         MergeSort(input, middle + 1, high);
         MergeSortAlgorithm.Merge(input, low, middle, high);
     }
 }
예제 #9
0
        private void SetupScenery7()
        {
            merge     = new MergeSortAlgorithm <int>(new MyComparer());
            testArray = new int[1000];
            Random rnd = new Random();

            for (int i = 0; i < 1000; i++)
            {
                testArray[i] = rnd.Next(1, 1000001);
            }
        }
예제 #10
0
        private void SetupScenery6()
        {
            merge     = new MergeSortAlgorithm <int>(new MyComparer());
            testArray = new int[100];
            int cnt = 100;

            for (int i = 0; i < 100; i++)
            {
                testArray[i] = cnt;
                cnt--;
            }
        }
예제 #11
0
 private void SetupScenery3()
 {
     merge        = new MergeSortAlgorithm <int>(new MyComparer());
     testArray    = new int[10];
     testArray[0] = 18;
     testArray[1] = 13;
     testArray[2] = 11;
     testArray[3] = 9;
     testArray[4] = 8;
     testArray[5] = 7;
     testArray[6] = 5;
     testArray[7] = 4;
     testArray[8] = 2;
     testArray[9] = 1;
 }
예제 #12
0
        static void Main(string[] args)
        {
            int[] arr = { 34, 534, 45, 23, 346, 34, 5 };

            Sorter sorter = new Sorter(arr);

            ISortAlgorithm algo = new BubbleSortAlgorithm();

            sorter.setAlgorithm(algo);
            sorter.sort();

            algo = new MergeSortAlgorithm();
            sorter.setAlgorithm(algo);
            sorter.sort();
        }
예제 #13
0
        public void MergeSort_SortedArray_SortedArray()
        {
            int[] testArray     = new int[10];
            int[] expectedArray = new int[10];

            for (int i = 1; i < 11; i++)
            {
                testArray[i - 1]     = i;
                expectedArray[i - 1] = i;
            }

            int[] actualArray = MergeSortAlgorithm.MergeSort(testArray);

            CollectionAssert.AreEqual(expectedArray, actualArray);
        }
예제 #14
0
 private void SetupScenery1()
 {
     merge        = new MergeSortAlgorithm <int>(new MyComparer());
     testArray    = new int[10];
     testArray[0] = 8;
     testArray[1] = 1;
     testArray[2] = 5;
     testArray[3] = 3;
     testArray[4] = 0;
     testArray[5] = 12;
     testArray[6] = 2;
     testArray[7] = 21;
     testArray[8] = 19;
     testArray[9] = 6;
 }
예제 #15
0
        private void Sort(int[] intTempArray, SortType sortType, CancellationToken cToken, HSBColor[] floatSortArray, bool hsb, int delay)
        {
            ISortingAlgorithm sortingAlgorithm = null;

            switch (sortType)
            {
            case SortType.QuickSort:
                sortingAlgorithm = new QuickSortAlgorithm();
                break;

            case SortType.BubbleSort:
                sortingAlgorithm = new BubbleSortAlgorithm();
                break;

            case SortType.HeapSort:
                sortingAlgorithm = new HeapSortAlgorithm();
                break;

            case SortType.SelectionSort:
                sortingAlgorithm = new SelectionSortAlgorithm();
                break;

            case SortType.MergeSort:
                sortingAlgorithm = new MergeSortAlgorithm();
                break;
                break;

            case SortType.ParalellMergeSort:
                sortingAlgorithm = new ParalellMergeSortAlgorithm();
                break;
            }

            sortingAlgorithm.Token = cToken;


            sortingAlgorithm.Delay = delay;

            if (hsb)
            {
                sortingAlgorithm.Sort(intTempArray, floatSortArray);
            }
            else
            {
                sortingAlgorithm.Sort(intTempArray);
            }
        }
예제 #16
0
        static void Main(string[] args)
        {
            int[] arr = { 5, 346, 34, 634, 6, 346, 4 };

            Sorter sorter = new Sorter(arr);

            IComparator comparator = new AscComparator();

            ISortAlgorithm algo = new BubbleSortAlgorithm(comparator);

            sorter.setAlgorithm(algo);
            sorter.sort();

            algo = new MergeSortAlgorithm(comparator);
            sorter.setAlgorithm(algo);
            sorter.sort();
        }
        public void MergeSortAlgorithm_MergeSortTests(int[] array)
        {
            MergeSortAlgorithm.Sort(array);

            Assert.IsTrue(IsSorted(array));
        }
예제 #18
0
        private static int[] Sorting()
        {
            int[] arr = new int[] { 8, 3, 2, 7, 9, 1, 4, 1 };
            Console.WriteLine("\n");
            Console.WriteLine("Before HeapSort:");
            foreach (int i in arr)
            {
                Console.Write(i + ",");
            }
            Console.ReadLine();
            HeapSortAlgorithm.HeapSort(arr);

            Console.WriteLine("\n");
            Console.WriteLine("After HeapSort:");
            foreach (int i in arr)
            {
                Console.Write(i + ",");
            }
            Console.WriteLine("\n");
            Console.ReadLine();

            arr = new int[] { 4, 88, 23, 65, 2, 89, 7, 3, 1, 90, 4 };

            Console.WriteLine("\n");
            Console.WriteLine("Before MergeSort:");
            foreach (int i in arr)
            {
                Console.Write(i + ",");
            }
            Console.ReadLine();
            MergeSortAlgorithm.MergeSort(arr, 0, arr.Length - 1);

            Console.WriteLine("\n");
            Console.WriteLine("After MergeSort:");
            foreach (int i in arr)
            {
                Console.Write(i + ",");
            }
            Console.WriteLine("\n");
            Console.ReadLine();

            arr = new int[] { 8, 3, 2, 7, 9, 1, 4, 1 };

            Console.WriteLine("\n");
            Console.WriteLine("Before QuickSort:");
            foreach (int i in arr)
            {
                Console.Write(i + ",");
            }
            Console.ReadLine();
            QuickSortAlgorithm.QuickSort(arr, 0, arr.Length - 1);

            Console.WriteLine("\n");
            Console.WriteLine("After QuickSort:");
            foreach (int i in arr)
            {
                Console.Write(i + ",");
            }
            Console.WriteLine("\n");
            Console.ReadLine();
            return(arr);
        }
예제 #19
0
 static long countInversions(int[] arr)
 {
     MergeSortAlgorithm.MergeSort(arr);
     return(MergeSortAlgorithm.inversions);
 }
예제 #20
0
 public void MergeSort()
 {
     int[] arr = { 31, 41, 59, 26, 41, 58 };
     MergeSortAlgorithm.Merge(arr, 0, arr.Length);
     CollectionAssert.AreEqual(arr, new int[] { 26, 31, 41, 41, 58, 59 });
 }
예제 #21
0
        static void Main(string[] args)
        {
            //Create new Games List
            Game        g  = new Game();
            ListEmpty   le = new ListEmpty();
            GameList    gl = new GameList(g, le);
            List <Game> Games;

            Games = gl.CreateGameList();

            int input = 1;

            while (input != 0)
            {
                Console.WriteLine("Sort your Games by Sell Price!");
                Console.WriteLine("----------------");
                Console.WriteLine();
                Console.WriteLine("Choose from the following options:");
                Console.WriteLine("  1: Add a game");
                Console.WriteLine("  2: View random 10 Games (unsorted) from list");
                Console.WriteLine("  3: View current (unsorted) list");
                Console.WriteLine("  4: View the Current Games List - Ascending Order");
                Console.WriteLine("  5: View the Current Games List - Descending Order");
                Console.WriteLine("  6: Clear your current Games List");
                Console.WriteLine("  7: Create a new Games List");
                Console.WriteLine("  0: Exit application");
                Console.WriteLine("----------------");
                string inp   = Console.ReadLine();
                int    outc  = 6;
                bool   check = Int32.TryParse(inp, out outc);
                if (check)
                {
                    input = Int32.Parse(inp);
                }
                else
                {
                    input = 6;  //hard coded, not sure why it sets to 0 atm
                }
                MergeSortAlgorithm ms           = new MergeSortAlgorithm(le);
                List <Game>        newGamesList = ms.MergeSort(Games);

                switch (input)
                {
                case 1:
                {
                    int    price;
                    string nameGame;
                    Console.WriteLine("You MAY enter a price for your game, then hit return:");
                    var  p       = Console.ReadLine();
                    int  result  = 0;
                    bool outcome = int.TryParse(p, out result);
                    if (outcome)
                    {
                        price = Int32.Parse(p);
                    }
                    else
                    {
                        price = result;
                    }
                    Console.WriteLine("You MAY enter a name of a game, then hit return:");
                    nameGame = Console.ReadLine();
                    g.AddNewGame(price, nameGame);
                    Games = gl.ReturnGames();
                    break;
                }

                case 2:
                {
                    Console.WriteLine("10 games in your list include:");
                    ms.Print10(g, gl);
                    Console.WriteLine("----------------");
                    break;
                }

                case 3:
                {
                    Console.WriteLine("The current list of {0} games in it's unsorted order", Games.Count);
                    ms.PrintAll(Games, false);
                    Console.WriteLine("----------------");
                    break;
                }

                case 4:
                {
                    Console.WriteLine("Now we'll sort the list of {0} games with MergeSort in ascending order", newGamesList.Count);
                    ms.PrintAll(newGamesList, false);
                    Console.WriteLine("----------------");
                    break;
                }

                case 5:
                {
                    Console.WriteLine("Now we'll sort the list of {0} games with MergeSort in descending order", newGamesList.Count);
                    ms.PrintAll(newGamesList, true);
                    Console.WriteLine("----------------");
                    break;
                }

                case 6:
                {
                    Console.WriteLine("Are you sure you want to clear your Game List? - Press Y to confirm");
                    string answer = Console.ReadLine();
                    if (answer == "Y" || answer == "y")
                    {
                        gl.ClearList();
                    }
                    break;
                }

                case 7:
                {
                    gl.CreateGameList();
                    break;
                }

                default:
                {
                    Console.WriteLine("You didn't enter in a correct value");
                    break;
                }
                }
            }
            Console.WriteLine("-------------------");
            Console.WriteLine("End of the Application");
        }