Beispiel #1
0
        static void Main(string[] args)
        {
            System.Console.WriteLine("hello world!");
            Random random = new Random();

            //int[] myArray = new int[] { 45, 36, 36, 18, 53, 72, 30, 48, 93, 15};
            int[]  myArray  = new int[10];
            string strArray = "";

            for (int i = 0; i < myArray.Length; i++)
            {
                myArray[i] = random.Next(1, 100);
                strArray   = strArray + myArray[i] + " ";
            }

            print("=================:" + strArray);

            SortFunc sf = new SortFunc();

            //排序
            //sf.BubbleSort(myArray, SortType.Ascending);
            //sf.InsertSort(myArray, SortType.Descending);
            //sf.QuickSort(myArray, SortType.Ascending);
            //sf.SimpleSelectSort(myArray, SortType.Ascending);
            sf.ShellSort(myArray, SortType.Descending);

            //print("myArray.Length{0}", myArray.Length);

            for (int i = 0; i < myArray.Length; i++)
            {
                print("{0}", myArray[i]);
            }

            System.Console.ReadLine();
        }
Beispiel #2
0
        /// <summary>
        /// Sorts the specified list.
        /// </summary>
        /// <typeparam name="P"></typeparam>
        /// <param name="list">The list.</param>
        /// <param name="lambda">The lambda.</param>
        /// <param name="descending">if set to <c>true</c>, then sorts the list in descending order.</param>
        public void Sort <P>(List <T> list, SortFunc <T, P> lambda, bool descending)
            where P : IComparable <P>
        {
            // Apply and set the sort, if items to sort
            list.Sort(delegate(T x, T y)
            {
                // Get property values
                P xValue = lambda(x);
                P yValue = lambda(y);

                // Do comparison
                int result;

                if (xValue == null)
                {
                    result = (yValue == null) ? 0 : 1;
                }
                else
                {
                    result = xValue.CompareTo(yValue);
                }

                // return based on direction
                return((descending) ? -(result) : result);
            });
        }
        /// <summary>
        /// Sorts the collection based on a specified lambda expression.
        /// </summary>
        /// <typeparam name="P"></typeparam>
        /// <param name="lambda">The lambda expression.</param>
        /// <param name="descending">if set to <c>true</c>, then sorts then collection in descending order.</param>
        public void Sort <P>(SortFunc <T, P> lambda, bool descending)
            where P : IComparable <P>
        {
            // get sorter and sort
            var sorter = new ListSorter <T>();

            sorter.Sort(Items as List <T>, lambda, descending);
        }
        /// <summary>
        /// Sorts the collection based on a specified lambda expression.
        /// </summary>
        /// <typeparam name="P"></typeparam>
        /// <param name="lambda">The lambda expression.</param>
        public void Sort <P>(SortFunc <T, P> lambda)
            where P : IComparable <P>
        {
            // get sorter and sort
            var sorter = new ListSorter <T>();

            sorter.Sort(Items as List <T>, lambda, false);
        }
Beispiel #5
0
            public int Compare(string[] x, string[] y)
            {
                int index  = 0;
                int retval = 0;

                while ((retval == 0) && (index < sorttype.Count))//如果比较相等且比较函数未到最后一个
                {
                    SortFunc func = (SortFunc)SortFuncHandle[sorttype[index]];
                    retval = func(x, y);
                    index++;
                }
                return(retval);
            }
Beispiel #6
0
        //初始化排序方法列表
        public void InitTable()
        {
            if (sorttype.Count == 0)
            {
                sorttype.Add(SortPolicy.MinHops);
                SortFunc MinHops = SortByMinHops;
                SortFuncHandle.Add(SortPolicy.MinHops, MinHops);
            }
            else
            {
                foreach (SortPolicy sp in sorttype)
                {
                    switch (sp)
                    {
                    case SortPolicy.MinHops:
                        SortFunc MinHops = SortByMinHops;
                        if (!SortFuncHandle.ContainsKey(SortPolicy.MinHops))
                        {
                            SortFuncHandle.Add(SortPolicy.MinHops, MinHops);
                        }
                        break;

                    case SortPolicy.MinMaxDist:
                        SortFunc MinMax = SortByMinMaxDist;
                        if (!SortFuncHandle.ContainsKey(SortPolicy.MinMaxDist))
                        {
                            SortFuncHandle.Add(SortPolicy.MinMaxDist, MinMax);
                        }
                        break;

                    case SortPolicy.TotalDist:
                        SortFunc TotalDist = SortByTotalDist;
                        if (!SortFuncHandle.ContainsKey(SortPolicy.TotalDist))
                        {
                            SortFuncHandle.Add(SortPolicy.TotalDist, TotalDist);
                        }
                        break;

                    default:
                        MinHops = SortByMinHops;
                        if (!SortFuncHandle.ContainsKey(SortPolicy.MinHops))
                        {
                            SortFuncHandle.Add(SortPolicy.MinHops, MinHops);
                        }
                        break;
                    }
                }
            }
        }
Beispiel #7
0
        /// <summary>
        /// 排序并返回列表。
        /// </summary>
        public System.Collections.Generic.List <DatabaseSchemaHandler> Sort()
        {
            System.Collections.Generic.List <DatabaseSchemaHandler> list = new System.Collections.Generic.List <DatabaseSchemaHandler>();
            //foreach(AssemblyRef
            System.Collections.Generic.Dictionary <string, SortEntry> refOrders = new System.Collections.Generic.Dictionary <string, SortEntry>();
            System.Collections.Generic.List <AssemblyRef>             all       = LinqHelper.ToList(_refs.Values);

            SortAction action = null; action = (p1, p2) => {
                foreach (string p11 in p1.refs)
                {
                    AssemblyRef p12;
                    if (_refs.TryGetValue(p11, out p12))
                    {
                        if (all.Remove(p12))
                        {
                            action(p12, p2 * 1000);
                        }
                    }
                }
                SortEntry p10 = new SortEntry()
                {
                    item  = p1,
                    order = p2 * (refOrders.Count + 1)
                };
                refOrders.Add(p1.fullName, p10);
                return(p10);
            };

            while (all.Count > 0)
            {
                AssemblyRef item = all[0];
                all.RemoveAt(0);
                action(item, -1);
            }
            SortFunc classOrderGetter = (p1) => p1.Attribute.Order + GetClassRef(p1.Attribute.TableName, p1.Attribute.References);

            foreach (SortEntry p1 in LinqHelper.OrderByDescending(refOrders.Values, p => p.order))
            {
                list.AddRange(p1.item.SortList(classOrderGetter));
            }
            refOrders.Clear();
            refOrders = null;
            all       = null;
            return(list);
        }
Beispiel #8
0
        private static int[][] BubbleSort(int[][] array, SortFunc sortFunc, IComparer comparer)
        {
            int[] buf         = null;
            int   arrayLength = array.Length;

            for (int i = 0; i < arrayLength; i++)
            {
                for (int j = i + 1; j < arrayLength; j++)
                {
                    if (comparer.Compare(sortFunc(array[i]), sortFunc(array[j])) == 1)
                    {
                        buf      = array[i];
                        array[i] = array[j];
                        array[j] = buf;
                    }
                }
            }

            return(array);
        }
        public static void ShowTestsFunc(SortFunc sortFunc)
        {
            var stopwatch = new Stopwatch();

            var testData = new Dictionary <int[], string>()
            {
                { new[] { 1, 3, 2, 0, -1, 10, 4, 456 }, "O(n^2)" },
                { new[] { 1, 2, 3, 4, 5, 6, 7, 8 }, "O(n)" },
            };

            foreach (KeyValuePair <int[], string> data in testData)
            {
                Console.WriteLine($"Time complexity: {data.Value}");
                Console.WriteLine($"Before sorting : {string.Join(" ", data.Key)}");

                stopwatch.Restart();
                sortFunc(data.Key);
                stopwatch.Stop();
                Console.Write($"{sortFunc.Method.Name,-25} |After sorting: {string.Join(" ", data.Key),-10} ");
                Console.WriteLine("|ElapseTicks: " + stopwatch.ElapsedTicks);
            }

            Console.WriteLine(new string('-', 65));
        }
Beispiel #10
0
 /// <summary>
 /// Sorts the specified list.
 /// </summary>
 /// <typeparam name="P"></typeparam>
 /// <param name="list">The list.</param>
 /// <param name="lambda">The lambda.</param>
 public void Sort <P>(List <T> list, SortFunc <T, P> lambda)
     where P : IComparable <P>
 {
     Sort(list, lambda, false);
 }
 internal void Register(SortFunc sortFunc)
 {
     this.sortFunc = sortFunc;
 }
Beispiel #12
0
        public ManagedJobList ReInitialize(DiscoDataContext Database, FilterFunc FilterFunction, SortFunc SortFunction, bool FilterAuthorization)
        {
            if (Database == null)
                throw new ArgumentNullException("Database");

            lock (updateLock)
            {
                if (FilterFunction != null)
                    this.FilterFunction = FilterFunction;
                if (SortFunction != null)
                    this.SortFunction = SortFunction;

                base.Items = this.SortFunction(this.DetermineItems(Database, this.FilterFunction(Database.Jobs), FilterAuthorization)).ToList();
            }
            return this;
        }
Beispiel #13
0
 public ManagedJobList(string Name, FilterFunc FilterFunction, SortFunc SortFunction)
 {
     this.Name = Name;
     this.FilterFunction = FilterFunction;
     this.SortFunction = SortFunction;
 }
Beispiel #14
0
 public System.Collections.Generic.IEnumerable <DatabaseSchemaHandler> SortList(SortFunc classOrderGetter)
 {
     return(LinqHelper.Where(list, p => p.Attribute.IsValid)
            .OrderBy(p => p.Attribute.Type)
            .ThenBy(p => classOrderGetter(p))
            .ThenBy(p => p.Attribute.TableName));
 }