Exemple #1
0
        static void Sort(ref int[] mas, MySort sort_order)
        {
            /*sort array in descending and ascending order*/
            int temp;

            for (int k = 0; k < mas.Length; k++)
            {
                for (int i = 0; i < mas.Length - 1; i++)
                {
                    if (sort_order == MySort.Asc)
                    {
                        if (mas[i] > mas[i + 1])
                        {
                            temp       = mas[i];
                            mas[i]     = mas[i + 1];
                            mas[i + 1] = temp;
                        }
                    }
                    else
                    {
                        if (mas[i] < mas[i + 1])
                        {
                            temp       = mas[i];
                            mas[i]     = mas[i + 1];
                            mas[i + 1] = temp;
                        }
                    }
                }
            }
        }
        private void PerformCustomSort(bool C_to_T)
        {
            ListCollectionView lcv    = (ListCollectionView)CollectionViewSource.GetDefaultView(DataGrid_Dictionary.ItemsSource);
            MySort             mySort = new MySort(C_to_T);

            lcv.CustomSort = mySort;
        }
Exemple #3
0
        public static void Main(string[] args)
        {
            Person[] persons =
            {
                new Person {
                    LastName = "Huber", FirstName = "Herman", Age = 27
                },
                new Person {
                    LastName = "Adam", FirstName = "Paul", Age = 35
                },
                new Person {
                    LastName = "Mustermann", FirstName = "Max", Age = 42
                },
                new Person {
                    LastName = "Bertold", FirstName = "Brecht", Age = 19
                }
            };
            Console.WriteLine("Liste unsortiert");
            PrintOut(persons);

            MySort <Person> .Sort(persons);

            Console.WriteLine();
            Console.WriteLine("Liste sortiert nach VORNAME aufsteigend!!!");
            PrintOut(persons);

            MySort <Person> .Sort(persons, new PersonAgeComparer());

            Console.WriteLine();
            Console.WriteLine("Liste sortiert nach ALTER absteigend!!!");
            PrintOut(persons);
        }
        public void SortTest()
        {
            //Arrange
            string[] list = new string[5];
            list[0] = "Adolf";
            list[1] = "Maier";
            list[2] = "Müller";
            list[3] = "Huber";
            list[4] = "Burghard";
            //Act
            string[] index = { "Adolf", "Burghard", "Huber", "Maier", "Müller" };
            MySort <object> .Sort(list);

            int count = 0;

            foreach (var item in list)
            {
                int i = item.CompareTo(index[count++]);
                if (i != 0)
                {
                    break;
                }
            }
            //Assert
            Assert.AreEqual(list.ToString(), index.ToString());
        }
Exemple #5
0
        public void CheckSortListByModification()
        {
            MySort sort = new MySort();

            var dateOne   = DateTime.Parse("12/05/2012");
            var dateTwo   = DateTime.Parse("01/01/1989");
            var dateThree = DateTime.Parse("30.09.1999");
            var dateFour  = DateTime.Parse("10/09/2017");

            List <ViewModel> unSortedList = new List <ViewModel>
            {
                new ViewModel {
                    Type = "Folder", Name = "One", ModificationDate = dateOne, Size = 12.34, Attribute = "Hiden, Directory"
                },
                new ViewModel {
                    Type = "Folder", Name = "Two", ModificationDate = dateTwo, Size = 5.67, Attribute = "Directory"
                },
                new ViewModel {
                    Type = "File", Name = "Three", ModificationDate = dateThree, Size = 78.45, Attribute = "Archive"
                },
                new ViewModel {
                    Type = "File", Name = "Four", ModificationDate = dateFour, Size = 12.34, Attribute = "Hiden"
                }
            };

            var sortedList = sort.SortList(unSortedList, "Modification");

            var comparativeList = unSortedList.OrderBy(o => o.ModificationDate).ToList();

            Assert.Equal(comparativeList, sortedList);
        }
        private void PerformCustomSort(DataGridColumn column)
        {
            ListSortDirection direction = (column.SortDirection != ListSortDirection.Ascending) ? ListSortDirection.Ascending : ListSortDirection.Descending;

            column.SortDirection = direction;
            ListCollectionView lcv    = (ListCollectionView)CollectionViewSource.GetDefaultView(dataGrid.ItemsSource);
            MySort             mySort = new MySort(this, direction, column);

            lcv.CustomSort = mySort;    // provide our own sort
        }
        public void TestSort()
        {
            int[] arr = { 2, 7, 3, 11, 6 };

            int[] sortedArr = (int[])arr.Clone();
            Array.Sort(sortedArr);

            int[] mySortedArr = (int[])arr.Clone();
            MySort.InsertionSort(mySortedArr);

            Assert.AreEqual(sortedArr, mySortedArr);
        }
Exemple #8
0
 protected override void SetSort(string method)
 {
     if (MySort.ContainsKey(method))
     {
         List <LOCATION> resultSort = (List <LOCATION>)MySort[method].DynamicInvoke();
         LOCATIONS = new ObservableCollection <LOCATION>(resultSort);
         if (searchLOCATIONS != LOCATIONS)
         {
             searchLocation_ByName();
         }
         DisplayObjects();
     }
 }
Exemple #9
0
 public void SortTest()
 {
     //Arrange
     string[] list = new string[5];
     list[0] = "Adolf";
     list[1] = "Maier";
     list[2] = "Müller";
     list[3] = "Huber";
     list[4] = "Burghard";
     //Act
     string[] index = { "Adolf", "Burghard", "Huber", "Maier", "Müller" };
     MySort.Sort(list);
     //Assert
     CollectionAssert.AreEqual(list, index);
 }
Exemple #10
0
        static void Main(string[] args)
        {
            string[]     mass = new string[] { "aaca", "aaaa", "aab", "aaa", "aaaaa", "aa", "az", "aaz", "abaaa", "aabaa", "1aa", "a1a" };
            MyCheckArray obj  = new MyCheckArray();
            Thread       th1  = new Thread(obj.PrintMass);

            th1.Start();
            MySort del = obj.SortByLength;

            del /*?*/.Invoke(mass);
            //obj.SortByLength(mass);
            // obj.SortByAlphabet(mass);
            del /*?*/.Invoke(mass); //with "?" dosn't work
            Console.ReadKey();
        }
Exemple #11
0
        static void Main(string[] args)
        {
            // Linked List Example
            LinkedList.LinkedList <int> list = new LinkedList.LinkedList <int>();

            list.push_back(123);
            list.push_back(456);
            list.push_back(789);

            int i = 1;

            foreach (int val in list)
            {
                Console.WriteLine($"{i}: {val}");
                i += 1;
            }

            Console.WriteLine(list);

            list.pop_back();
            Console.WriteLine(list);

            list.pop_back();
            Console.WriteLine(list);

            list.pop_back();
            Console.WriteLine(list);


            // Binary Tree Example
            BinaryTree.BinaryTree <int, int> tree = new BinaryTree.BinaryTree <int, int>();

            tree.insert(2, 123);
            tree.insert(1, 456);
            tree.insert(3, 789);

            tree.remove(2);

            tree.find(2);
            tree.find(1);
            tree.find(3);


            // Insertion Sort Example
            int[] arr = { 2, 7, 3, 11, 6 };

            MySort.InsertionSort(arr);
        }
        static void Main(string[] args)
        {
            var csvReader = new CSVreader();
            var mySearch  = new MySearch(); //Linear and Binary, Binary should only sort sorted data
            var mySort    = new MySort();   //Insert and Shell

            string path = "C:/Users/New/Documents/visual studio 2017/Projects/Algorithms1Console/unsorted_numbers.csv";

            int[] data = csvReader.ReadFile(path); // comment out unnecessary stuff

            int[] sorted = mySort.InsertionSort(data);

            Console.WriteLine("Linear Search");
            var linearstopwatch = Stopwatch.StartNew();

            mySearch.LinearSearchHighest(data);
            linearstopwatch.Stop();
            Console.WriteLine("Linear Search: " + ((linearstopwatch.Elapsed.TotalMilliseconds).ToString()));

            Console.WriteLine();

            //Console.WriteLine("Binary Search");
            //var binarystopwatch = Stopwatch.StartNew();
            //mySearch.LinearSearchHighest(data);
            //binarystopwatch.Stop();
            //Console.WriteLine("Binary Search: " + ((binarystopwatch.Elapsed.TotalMilliseconds).ToString()));

            //Console.WriteLine();

            Console.WriteLine("Linear 1500th Search");
            var linear1500thstopwatch = Stopwatch.StartNew();

            mySearch.LinearSearchEvery1500th(sorted);
            linear1500thstopwatch.Stop();
            Console.WriteLine("Linear 1500th Search: " + ((linear1500thstopwatch.Elapsed.TotalMilliseconds).ToString()));

            Console.WriteLine();

            //Console.WriteLine("Binary 1500th Search");
            //var binary1500thstopwatch = Stopwatch.StartNew();
            //mySearch.BinarySearchEvery1500th(sorted);
            //binary1500thstopwatch.Stop();
            //Console.WriteLine("Binary 1500th Search: " + ((binary1500thstopwatch.Elapsed.TotalMilliseconds).ToString()));

            Console.ReadKey();
        }
Exemple #13
0
 public void ComparePersonLastnameTest()
 {
     //Arrange
     Person[] list =
     {
         new Person("Maier",  "Helmut", 42),
         new Person("Müller", "Thomas", 33),
         new Person("Huber",  "Hans", 27)
     };
     //Act
     Person[] erwartet =
     {
         list[2], list[0], list[1]
     };
     MySort.Sort(list, new PersonLastNameComparer());
     //Assert
     CollectionAssert.AreEqual(list, erwartet);
 }
    public int CompareTo(object other)
    {
        MySort otherMySort = (MySort)other;
        int    min         = Math.Min(this.splitvalues.Length, otherMySort.splitvalues.Length);

        for (int i = 0; i < min; i++)
        {
            string a = this.splitvalues[i];
            string b = otherMySort.splitvalues[i];
            if (a != b)
            {
                int numberA = 0;
                int numberB = 0;
                if (int.TryParse(a, out numberA))
                {
                    if (int.TryParse(b, out numberB))
                    {
                        int z = numberA.CompareTo(numberB);
                        //a & b are numbers
                        return(numberA.CompareTo(numberB));
                    }
                    else
                    {
                        //a number b string
                        return(-1);
                    }
                }
                else
                {
                    if (int.TryParse(b, out numberB))
                    {
                        //a string b number
                        return(1);
                    }
                    else
                    {
                        // a string b string
                        return(a.CompareTo(b));
                    }
                }
            }
        }
        return(splitvalues.Length.CompareTo(otherMySort.splitvalues.Length));
    }
Exemple #15
0
        public void CompareIntegerTest()
        {
            //Arrange
            MyList <object> list = new MyList <object>();

            list.Add(3);
            list.Add(4);
            list.Add(5);
            list.Add(0);
            list.Add(1);
            list.Add(2);
            object[] expected    = { 0, 1, 2, 3, 4, 5 };
            object[] targetArray = new object[6];
            //Act
            list.CopyTo(targetArray, 0);
            MySort.Sort(targetArray);
            //Assert
            CollectionAssert.AreEqual(expected, targetArray);
        }
        public void ComparePersonLastnameTest()
        {
            //Arrange
            Person[] list =
            {
                new Person("Maier",  "Helmut", 42),
                new Person("Müller", "Thomas", 33),
                new Person("Huber",  "Hans", 27)
            };
            //Act
            Person[] erwartet =
            {
                new Person("Huber",  "Hans",   27),
                new Person("Müller", "Thomas", 33),
                new Person("Maier",  "Helmut", 42)
            };
            MySort <object> .Sort(list, new PersonAgeComparer());

            //Assert
            Assert.AreEqual(list.ToString(), erwartet.ToString());
        }
Exemple #17
0
        public static void Main(string[] args)
        {
            Person[] persons =
            {
                new Person {
                    LastName = "Karlsberger", FirstName = "Herman", Age = 27
                },
                new Person {
                    LastName = "Liszt", FirstName = "Franz", Age = 35
                },
                new Person {
                    LastName = "Mustermann", FirstName = "Max", Age = 42
                },
                new Person {
                    LastName = "Bertolt", FirstName = "Brecht", Age = 19
                },
                new Person("Maier", "Helmut", 42),
                new Person("Müller", "Thomas", 33),
                new Person("Strauss", "Johann", 27)
            };
            Console.WriteLine("Liste unsortiert");
            PrintOut(persons);

            MySort.Sort(persons);
            Console.WriteLine();
            Console.WriteLine("Liste sortiert nach VORNAME aufsteigend!!!");
            PrintOut(persons);

            MySort.Sort(persons, new PersonAgeComparer());
            Console.WriteLine();
            Console.WriteLine("Liste sortiert nach ALTER absteigend!!!");
            PrintOut(persons);

            MySort.Sort(persons, new PersonLastNameComparer());
            Console.WriteLine();
            Console.WriteLine("Liste sortiert nach FAMILIENNAME aufsteigend!!!");
            PrintOut(persons);
        }
Exemple #18
0
    public static void Main()
    {
        string[] cities = { "Tokyo", "Beijing", "Hangzhou", "Kyoto", "Beijing", "Copenhagen", "Seattle" };

        IList <string> alst = new ArrayList <string>();

        alst.AddAll(cities);

        Console.WriteLine(string.Join(", ", MySort.GetPermutation1(alst)));
        Console.WriteLine();

        IList <string> llst = new LinkedList <string>();

        llst.AddAll(cities);

        Console.WriteLine(string.Join(", ", MySort.GetPermutation2(llst)));
        Console.WriteLine();

        Console.WriteLine("The rank of the cities:");
        var res = MySort.GetPermutation1(MySort.GetPermutation2(llst));

        Console.WriteLine(string.Join(", ", res));
        Console.WriteLine();
    }
Exemple #19
0
        static void Main(string[] args)
        {
            #region 计算器
            MyCalculate _MyCalculate = new MyCalculate();
            //一般的调用
            Calculate_Delegate _calculate_Delegate;
            //创建的多种方法
            _calculate_Delegate = new Calculate_Delegate(_MyCalculate.Add);
            //_calculate_Delegate = _MyCalculate.Add; //可以直接这么写(只要符合委托签名即可)
            //多播委托(栈方式 如果有返回值,则只返回后入的那个方法返回值)
            _calculate_Delegate += _MyCalculate.Subtraction;
            _calculate_Delegate += _MyCalculate.Multiplication;
            _calculate_Delegate -= _MyCalculate.Subtraction;
            _calculate_Delegate -= _MyCalculate.Divide;//可以去除不存在的方法
            double result;
            //执行的多种方法
            result = _calculate_Delegate.Invoke(4, 3);
            //Func<T> 委托
            result = _MyCalculate.DoCalculate <double>(4, 5, _MyCalculate.Add);
            Console.WriteLine($"{result}");
            result = _calculate_Delegate(4, 3);
            Console.WriteLine($"{result}");
            //方法作为参数传递给另一个方法
            result = _MyCalculate.DoCalculate(_MyCalculate.Divide, 4, 3);
            Console.WriteLine("计算结果是:" + result);

            #endregion

            #region 通用排序实例
            List <Employee> Employees = new List <Employee>
            {
                new Employee("和", 6),
                new Employee("林", 8),
                new Employee("粥", 5),
                new Employee("轮", 3),
                new Employee("顾", 9),
                new Employee("小", 4),
                new Employee("仟", 7)
            };

            MySort.Sort(Employees, Employee.CompareAge);
            Employees.ForEach(
                f =>
            {
                Console.WriteLine(f);
            });

            #endregion

            #region 多播委托和异常处理
            Action actions = One;
            actions += Two;
            ////第一种:这种多播委托 如果第一个方法异常了  就会停止迭代了  不会去执行后面的方法了
            try
            {
                actions.Invoke();
            }
            catch (Exception ex)
            {
                Console.WriteLine("actions.Invoke() exception");
            }
            //第二种:将多播委托使用GetInvocationList() ,得到一个Delegate[],再执行,出异常可以继续下一次迭代
            System.Delegate[] d = actions.GetInvocationList();
            foreach (Action item in d)
            {
                try
                {
                    item();
                }
                catch (Exception)
                {
                    Console.WriteLine("Delegate[] d , item() exception");
                }
            }

            #endregion

            //Action a = () => Console.WriteLine("1");
            Console.ReadKey();
        }
Exemple #20
0
 static void Main(string[] args)
 {
     MySort.BubbleSort();
     Console.Read();
 }