예제 #1
0
        public void TestBubbleSort()
        {
            var sort = new BubleSort();

            sort.Sort(_arr);
            CollectionAssert.AreEqual(_arr, _expectedArr);
        }
        public void BubleSortIntegerTest(int[] original, int[] expected)
        {
            var target = new BubleSort();

            var actual = target.Sort(original);

            Assert.That(actual, Is.EqualTo(expected));
        }
        public void BubleSortCharacterTest(char[] original, char[] expected)
        {
            var target = new BubleSort(); // TODO: Use and Interface in order to re-use these tests to execute different algorithms.

            var actual = target.Sort(original);

            Assert.That(actual, Is.EqualTo(expected));
        }
        public void BubleSortIntegerTest(int[] original, int[] expected)
        {
            var target = new BubleSort();

            var actual = target.Sort(original);

            Assert.That(actual, Is.EqualTo(expected));
        }
        public void BubleSortCharacterTest(char[] original, char[] expected)
        {
            var target = new BubleSort(); // TODO: Use and Interface in order to re-use these tests to execute different algorithms.

            var actual = target.Sort(original);

            Assert.That(actual, Is.EqualTo(expected));
        }
예제 #6
0
        public void ShouldSortWithBubleSort()
        {
            //Arrange
            BubleSort bubleSort = new BubleSort();
            int       n1 = 100, n2 = 150;
            int       expectedn1 = 150, expectedn2 = 100;

            //Act
            bubleSort.Swap(ref n1, ref n2);

            //Assert
            Assert.AreEqual(n1, expectedn1);
            Assert.AreEqual(n2, expectedn2);
        }
        public void BubleSort()
        {
            var _bubleSort = new BubleSort();

            var list = _bubleSort.bubleSort(new System.Collections.Generic.List <string>
            {
                "Edilaine",
                "Brian",
                "Nicolas"
            });

            Assert.AreEqual(list[0], "Brian");
            Assert.AreEqual(list[1], "Edilaine");
            Assert.AreEqual(list[2], "Nicolas");
        }
예제 #8
0
        public void ExibeDTPoliticoOrdenadoPorCodigo()
        {
            dataGridView1.Rows.Clear();
            LinkedList <Politico> p = Cache.DeserializaPolitico();

            Politico[] vetorPolitico  = new Politico[p.Count];
            Politico[] vetorPolitico2 = new Politico[p.Count];
            int        contador       = 0;

            foreach (Politico i in p)
            {
                vetorPolitico[contador] = i;
                contador++;
            }

            vetorPolitico2 = BubleSort.OrdenaPoliticoPorCodigo(vetorPolitico);

            LinkedList <Politico> g = new LinkedList <Politico>();

            contador = 0;
            foreach (Politico i in p)
            {
                g.Add(vetorPolitico2[contador]);
                contador++;
            }

            var rows = new LinkedList <string[]>();

            foreach (Politico t in g)
            {
                string[] row1 = new string[]
                {
                    t.Codigo.ToString(),
                              t.Nome,
                              t.NumeroChapa.ToString(),
                              t.Cargo,
                              t.MilhoesLavados.ToString(),
                              t.Partido.Nome
                };
                rows.Add(row1);
            }

            foreach (string[] rowrray in rows)
            {
                dataGridView1.Rows.Add(rowrray);
            }
        }
예제 #9
0
        public void ExibeDTPartidoOrdenadoPorCodigo()
        {
            dataGridView1.Rows.Clear();
            LinkedList <Partido> p = Cache.DeserializaPartido();

            Partido[] vetorPartido  = new Partido[p.Count];
            Partido[] vetorPartido2 = new Partido[p.Count];
            int       contador      = 0;

            foreach (Partido i in p)
            {
                vetorPartido[contador] = i;
                contador++;
            }

            vetorPartido2 = BubleSort.OrdenaPartidoPorCodigo(vetorPartido);

            LinkedList <Partido> g = new LinkedList <Partido>();

            contador = 0;
            foreach (Partido i in p)
            {
                g.Add(vetorPartido2[contador]);
                contador++;
            }

            var rows1 = new LinkedList <string[]>();

            foreach (Partido t in g)
            {
                string[] row1 = new string[]
                {
                    t.Codigo.ToString(),
                             t.Nome,
                             t.NumeroChapa.ToString(),
                             t.Financiador,
                             t.JuizConfianca,
                             t.Sigla
                };
                rows1.Add(row1);
            }

            foreach (string[] rowrray in rows1)
            {
                dataGridView2.Rows.Add(rowrray);
            }
        }
예제 #10
0
        static void Main(string[] args)
        {
            int[] arr1 = { 31, 15, 10, 2, 4, 2, 14, 23, 12, 66 };

            StrategySort sort    = new SelectionSort();
            Context      context = new Context(sort, arr1);

            context.Sort();
            context.PrintArray();

            int[] arr2 = { 1, 5, 10, 2, 4, 12, 14, 23, 12, 66 };

            sort    = new InsertionSort();
            context = new Context(sort, arr2);

            context.Sort();
            context.PrintArray();

            int[] arr3 = { 1, 8, 4, 7, 3, 7, 1, 6 };
            sort    = new BubleSort();
            context = new Context(sort, arr3);
            context.Sort();
            context.PrintArray();
        }
예제 #11
0
        static void Main(string[] args)
        {
            var fourthLargest = QuickSelect.FindKthLargest(new int[] { -10, 2, 1, 9, -6, 44, 0, 3, -17, 8 }, 4);

            int[][] people = new int[][] { new int[] { 7, 0 }, new int[] { 4, 4 }, new int[] { 7, 1 },
                                           new int[] { 5, 0 }, new int[] { 6, 1 }, new int[] { 5, 2 } };

            Array.Sort(people, (a, b) => {
                if (a[0] > b[0])
                {
                    return(1);
                }
                else if (a[0] < b[0])
                {
                    return(-1);
                }
                else
                {
                    return(0);
                }
            });

            var sorted = people.OrderByDescending(i => i[0]).ThenBy(i => i[1]).ToArray();
            SortedList <int, int> sortedList = new SortedList <int, int>();
            HashSet <int>         map        = new HashSet <int>();
            StringBuilder         sb         = new StringBuilder("Hello");

            for (int i = 0; i < sb.Length; i++)
            {
                if (sb[i] >= 65 && sb[i] <= 90)
                {
                    sb[i] = (char)(sb[i] + 32);
                }
            }
            int[] arrayForSort = new int[] { -10, 2, 1, 9, -6, 44, 0, 3, -17, 8 };
            BubleSort.Sort(arrayForSort, false);
            int[] nums2  = new int[] { 0, 1, 2, 4, 5, 6, 7, 9 };
            var   binRes = binarySearch(nums2, 0, nums2.Length - 1, 0);

            List <int> list  = new List <int>(new int[] { 1, 3, 5, 7, 9 });
            var        biRes = list.BinarySearch(4);

            int[] nums = new int[] { 3, 0, 1 };
            (new missing_number()).MissingNumber(nums);


            int seed = 0;
            var xor  = nums.Aggregate(seed++, (x, y) => x ^ y);

            xor = Enumerable.Range(1, 4).Aggregate((x, y) => x ^ y);

            (new Swapping_two_integer_variables_without_an_intermediary_variable()).swapArray(nums, 0, 2);
            ListNode head = new ListNode(0);
            ListNode pre  = head;

            for (int i = 1; i < 10; i++)
            {
                ListNode tmp = new ListNode(i);
                pre.next = tmp;
                pre      = pre.next;
            }
            var bst = (new convert_sorted_list_to_binary_search_treeV2()).SortedListToBST(head);

            LeetCode.TreeNode node   = new LeetCode.TreeNode(1);
            LeetCode.TreeNode node1  = new LeetCode.TreeNode(2);
            LeetCode.TreeNode node12 = new LeetCode.TreeNode(5);
            node.left  = node1;
            node.right = node12;
            LeetCode.TreeNode node21 = new LeetCode.TreeNode(3);
            LeetCode.TreeNode node22 = new LeetCode.TreeNode(4);
            node1.left  = node21;
            node1.right = node22;
            LeetCode.TreeNode node31 = new LeetCode.TreeNode(6);
            node12.right = node31;

            (new flatten_binary_tree_to_linked_list()).Flatten(node);

            (new construct_binary_tree_from_preorder_and_inorder_traversalV2()).BuildTree(
                new int[] { 3, 9, 20, 15, 7 },
                new int[] { 9, 3, 15, 20, 7 }
                );

            IList <IList <int> > result2 = new List <IList <int> >();

            result2.Add(new int[] { 1, 2, 3 }.Concat(new int[] { 4, 5, 6 }).ToArray());
            List <int[]> _matrix = new List <int[]>();

            _matrix.Add(new int[] { 1, 2, 0, 4 });
            _matrix.Add(new int[] { 5, 6, 7, 8 });
            _matrix.Add(new int[] { 9, 10, 11, 12 });

            var matrix = _matrix.ToArray();

            var row1 = matrix[0];
            var bol  = Enumerable.Any(row1, i => i == 0);

            for (int i = 0; i < 4; i++)
            {
                matrix[0][i] = 0;
            }

            //int n = 3;
            //int[][] matrix = new int[n][];
            //for (int i = 0; i < n; i++)
            //{
            //    matrix[i] = new int[n];
            //}
            //List<int[]> positions = new List<int[]>();
            //int top = 0, left = 0, count = 1;
            //while (n > 0)
            //{
            //    if (n == 1)
            //    {
            //        (positions as List<int[]>).Add(new int[] { top, left });
            //        break;
            //    }
            //    (positions as List<int[]>).AddRange(Enumerable.Range(left, n - 1).Select(i => new int[] { top, i }));
            //    (positions as List<int[]>).AddRange(Enumerable.Range(top, n - 1).Select(i => new int[] { i, (left + n - 1) }));
            //    (positions as List<int[]>).AddRange(Enumerable.Range(left + 1, n - 1).Reverse().Select(i => new int[] { (top + n - 1), i }));
            //    (positions as List<int[]>).AddRange(Enumerable.Range(top + 1, n - 1).Reverse().Select(i => new int[] { i, left }));
            //    top++;
            //    left++;
            //    n -= 2;
            //}

            //Array.ForEach(positions.ToArray(), i => {
            //    matrix[i[0]][i[1]] = count;
            //    count++;
            //    Console.WriteLine($"[{i[0]},{i[1]}] - {matrix[i[0]][i[1]]}");
            //});


            //int[][] matrix2 = new int[2][];
            //IList<int[]> resti = new List<int[]>();
            //int width =3, height = 3, top = 1, left = 1;
            //(resti as List<int[]>).AddRange(Enumerable.Range(left, width - 1).Select(i => new int[] { top, i }).ToArray());
            //(resti as List<int[]>).AddRange(Enumerable.Range(top, height - 1).Select(i => new int[] { i, (left + width - 1) }).ToArray());
            //(resti as List<int[]>).AddRange(Enumerable.Range(left + 1, width - 1).Reverse().Select(i => new int[] { (top + height - 1), i }).ToArray());
            //(resti as List<int[]>).AddRange(Enumerable.Range(top + 1, height - 1).Reverse().Select(i => new int[] { i, left }).ToArray());
            //var qwe = resti.Select(i => matrix2[i[0]][i[1]]).ToList();

            //ListNode head = new ListNode(1);
            //ListNode node1 = new ListNode(4);
            //ListNode node2 = new ListNode(3);
            //ListNode node3 = new ListNode(2);
            //ListNode node4 = new ListNode(5);
            //ListNode node5 = new ListNode(2);
            //head.next = node1;
            //node1.next = node2;
            //node2.next = node3;
            //node3.next = node4;
            //node4.next = node5;

            //var head2 = (new partition_list()).Partition(head, 3);
            var data1 = (new permutation_sequenceV2()).GetPermutation(4, 10);

            int[] arr1 = new int[] { 0, 0, 3, 0, 0, 0, 0, 0, 0 };
            int[] arr2 = new int[] { -1, 1, 1, 1, 2, 3 };

            (new merge_sorted_array()).MergeV2(arr1, 3, arr2, 6);
            var car = (new CarPlateNumber()).Generate();
            int rob = (new house_robber_ii()).Rob(new int[] { 1, 2, 1, 1 });

            int[] arr       = Enumerable.Repeat(int.MinValue, 4).ToArray();
            var   perm      = (new permutations()).Permute(new int[] { 1, 1, 2 });
            var   solutions = (new n_queens()).SolveNQueens(4);

            IList <IList <string> > list2 = new List <IList <string> >();

            for (int i = 0; i < 8; i++)
            {
                list2.Add(Enumerable.Repeat <string>(".", 8).ToList());
            }
            list2[0][0] = "change";


            Dictionary <string, IList <int> > res3 = new Dictionary <string, IList <int> >();
            var res4 = res3.Values.ToList();

            var res = combination_sum.Answer(new int[] { 2, 3, 4 }, 24);

            List <char[]> board = new List <char[]>();

            board.Add(new char[] { '5', '3', '.', '.', '7', '.', '.', '.', '.' });
            board.Add(new char[] { '6', '.', '.', '1', '9', '5', '.', '.', '.' });
            board.Add(new char[] { '.', '9', '8', '.', '.', '.', '.', '6', '.' });
            board.Add(new char[] { '8', '.', '.', '.', '6', '.', '.', '.', '3' });
            board.Add(new char[] { '4', '.', '.', '8', '.', '3', '.', '.', '1' });
            board.Add(new char[] { '7', '.', '.', '.', '2', '.', '.', '.', '6' });
            board.Add(new char[] { '.', '6', '.', '.', '.', '.', '2', '8', '.' });
            board.Add(new char[] { '.', '.', '.', '4', '1', '9', '.', '.', '5' });
            board.Add(new char[] { '.', '.', '.', '.', '8', '.', '.', '7', '9' });

            var valid = valid_sudoku.Answer(board.ToArray());


            var answer = find_first_and_last_position_of_element_in_sorted_array.Answer(new int[] { 1, 2, 3, 4, 5, 6, 7, 8 }, 9);

            var ans3 = Problem3.Answer(new int[, ] {
                { 1, 3, 1, 2, 9, 4 }, { 1, 5, 1, 2, 6, 1 }, { 4, 2, 1, 2, 8, 3 }, { 6, 1, 4, 3, 1, 1 }
            });
            var ans32 = Problem3.ImporvedAnswer(new int[, ] {
                { 1, 3, 1, 2, 9, 4 }, { 1, 5, 1, 2, 6, 1 }, { 4, 2, 1, 2, 8, 3 }, { 6, 1, 4, 3, 1, 1 }
            });
            var ans  = Problem2.Answer(4, 6);
            var ans2 = Problem2.ImporvedAnswer(4, 6);


            var answer1  = Problem1.Answer1(10);
            var answer11 = Problem1.ImprovedAnswer1(10);
            var answer2  = Problem1.Answer2(10);
            var result   = generate_parentheses.GenerateParenthesis(4);

            foreach (var s in result)
            {
                Console.WriteLine(s);
            }

            //ThreeSumClosestHelper threeSumClostest = new ThreeSumClosestHelper();
            //var result = threeSumClostest.ThreeSumClosest(new int[] { -1, 2, 1, 4 }, 1);

            //var result = ThreeSum.Answer(new int[] { -1, 0, 1, 2, -1, -4, 3, 4, -5, 6 });
            //var result = ThreeSum.Answer(new int[] { 0,0,0,0,0});
            //foreach (var s in result)
            //{
            //    Console.WriteLine(string.Join(" ", s));
            //}

            //var result = LetterCombinationsOfAPhoneNumber.Answer("789");
            //foreach (var s in result)
            //{
            //    Console.WriteLine(s);
            //}

            //var result = DfsPermutation.Make(new int[] { 1,3,6,2});

            //DfsPermutationGenerator dfs = new DfsPermutationGenerator(9);
            //dfs.Make();

            //DfsSolution.subsets(new int[] { 1,2,3});

            RomanToInteger.RomanToInt("MCMXCIV");
            StringtoInteger.Answer("-91283472332");

            LeetCodeTesting();

            QuickSortTest();
            BinaryHeapTest();
            BinarySearchTreeTest();
            BinarySearchTreeMirrorTest();
            AVLTreeTest();
            RedBlackTreeTest();
            SplayTreeTest();
            SegmentTreeTest_Max();
            SegmentTreeTest_Min();
            SegmentTreeTest_Sum();
            BTreeTest();

            Console.ReadLine();
        }
        public void BubleSortTestNullInput()
        {
            var target = new BubleSort();

            Assert.Throws <ArgumentNullException>(() => target.Sort <int>(null));
        }
예제 #13
0
        static void Main(string[] args)
        {
            ShowInfo();
            MyStruct[] arr = new[]
            {
                new MyStruct(4), new MyStruct(40), new MyStruct(87), new MyStruct(8), new MyStruct(4), new MyStruct(2),
                new MyStruct(78), new MyStruct(18), new MyStruct(5), new MyStruct(13), new MyStruct(6), new MyStruct(1)
            };
            List <MyStruct>   mas = new List <MyStruct>(arr);
            MyMass <MyStruct> m   = new MyMass <MyStruct>(mas);



            var mc = m.Clone();

            //var strat = new Context();


            Console.WriteLine("Не отсортированный массив " + m.ToString());

            //strat.SetStrategy(new BubleSort());
            //strat.DoSomeLogic(m, new IntComparep());
            var strat = new BubleSort <MyStruct>(new IntComparep(), GetInt);

            strat.DoAlgorithm(m, true);

            Console.WriteLine("Buble sort " + m.ToString());

            strat.DoAlgorithm(m, false);
            Console.WriteLine("Reverse buble sort " + m.ToString());


            var strat1 = new MergeSort <MyStruct>(new IntComparep(), GetInt);

            strat1.DoAlgorithm(m, true);

            Console.WriteLine("Merge sort " + m.ToString());

            strat1.DoAlgorithm(m, false);
            Console.WriteLine("Reverse merge sort " + m.ToString());


            var strat2 = new QuickSort <MyStruct>(new IntComparep(), GetInt);

            strat2.DoAlgorithm(m, true);

            Console.WriteLine("Quick sort " + m.ToString());

            strat2.DoAlgorithm(m, false);
            Console.WriteLine("Quick buble sort " + m.ToString());


            var strat3 = new HeapSort <MyStruct>(new IntComparep(), GetInt);

            strat3.DoAlgorithm(m, true);

            Console.WriteLine("heap sort " + m.ToString());

            strat3.DoAlgorithm(m, false);
            Console.WriteLine("Reverse heap sort " + m.ToString());



            var strat5 = new SelectionSort <MyStruct>(new IntComparep(), GetInt);

            strat5.DoAlgorithm(m, true);

            Console.WriteLine("Selection sort " + m.ToString());

            strat5.DoAlgorithm(m, false);
            Console.WriteLine("Reverse selection sort " + m.ToString());
            var strat4 = new IsertionSort <MyStruct>(new IntComparep(), GetInt);

            strat4.DoAlgorithm(m, true);

            Console.WriteLine("Insertion sort " + m.ToString());

            strat4.DoAlgorithm(m, false);
            Console.WriteLine("Reverse insertion sort " + m.ToString());



            //strat.SetStrategy(new SelectionSort());
            //strat.DoSomeLogic(m, new IntComparep());
            //Console.WriteLine("Selection sort " + m.ToString());
            //strat.DoSomeLogic(m, new IntComparep(), false);
            //Console.WriteLine("Reverse Selection sort " + m.ToString());

            //strat.SetStrategy(new IsertionSort());
            //strat.DoSomeLogic(m, new IntComparep());
            //Console.WriteLine("Insertion sort " + m.ToString());
            //strat.DoSomeLogic(m, new IntComparep(), false);
            //Console.WriteLine("Reverse insertion sort " + m.ToString());

            //strat.SetStrategy(new QuickSort());
            //strat.DoSomeLogic(m, new IntComparep());
            //Console.WriteLine("Quick sort " + m.ToString());
            //strat.DoSomeLogic(m, new IntComparep(), false);
            //Console.WriteLine("Reverse quick sort " + m.ToString());

            //strat.SetStrategy(new MergeSort());
            //strat.DoSomeLogic(m, new IntComparep());
            //Console.WriteLine("Merge sort " + m.ToString());
            //strat.DoSomeLogic(m, new IntComparep(), false);
            //Console.WriteLine("Reverse merge sort " + m.ToString());

            //strat.SetStrategy(new HeapSort());
            //strat.DoSomeLogic(m, new IntComparep());
            //Console.WriteLine("Heap sort " + m.ToString());
            //strat.DoSomeLogic(m, new IntComparep(), false);
            //Console.WriteLine("Reverse heap sort " + m.ToString());
        }
        public void BubleSortTestNullInput()
        {
            var target = new BubleSort();

            Assert.Throws<ArgumentNullException>( () => target.Sort<int>(null));
        }