MergeSort() static private method

static private MergeSort ( List array ) : List
array List
return List
Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
 public void TenElementsRandomOrderTest()
 {
     SetupScenery1();
     merge.MergeSort(testArray, 0, testArray.Length - 1);
     int[] newArray = testArray;
     for (int i = 0; i < newArray.Length - 1; i++)
     {
         Assert.IsTrue(newArray[i] <= newArray[i + 1]);
     }
 }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
0
 static long countInversions(int[] arr)
 {
     MergeSortAlgorithm.MergeSort(arr);
     return(MergeSortAlgorithm.inversions);
 }
Ejemplo n.º 8
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");
        }