/// <summary>
        /// Given n d-digit numbers in which each digit can take on up to k possible values,RADIX-SORT correctly sorts these numbers in d(n+k) time if the stable sort it uses takes (n+k) time.
        /// </summary>
        /// <param name="arr"></param>
        /// <param name="sortProxy"> the sort must stable</param>
        /// <returns></returns>
        public static T[] Sort(T[] arr, StableSort sortProxy)
        {
            if (arr == null || arr.Length <= 1)
            {
                return(arr);
            }

            int len = arr[0].Count;

            for (int i = 1; i < arr.Length; i++)
            {
                if (arr[i].Count != len)
                {
                    throw new Exception("element must have same length");
                }
            }

            //from low position to high position
            for (int i = 0; i < arr.Length; i++)
            {
                // use a stable sort to sort array on digit i
                sortProxy(arr, i);
            }

            return(arr);
        }
Exemplo n.º 2
0
        public static List <Result> CalculateTimes()
        {
            Rand          rand      = new Rand();
            List <Result> keyValues = new List <Result>();

            StableSort                  stableSort          = new StableSort();
            StableSortWithArray         stableSortWithArray = new StableSortWithArray();
            List <List <List <long> > > data      = rand.GetData();
            List <List <long[]> >       dataArray = rand.GetArrayData();
            Stopwatch clock = new Stopwatch();

            int      i    = 0;
            TimeSpan time = new TimeSpan(0);


            Console.WriteLine("\nStableSortList");
            foreach (var listsByCount in data)
            {
                for (int j = 0; j < listsByCount.Count; j++)
                {
                    clock.Start();
                    var res = stableSort.Sort(listsByCount[j]);
                    clock.Stop();

                    time = clock.Elapsed;
                    Console.WriteLine("Length " + listsByCount[0].Count + " Time " + time);
                    clock.Reset();
                }


                keyValues.Add(new Result(stableSort.Name, listsByCount[0].Count, (i++) % 4 + 1, time));
                time = TimeSpan.Zero;
            }


            Console.WriteLine("StableSortArray");
            foreach (var listsByCount in dataArray)
            {
                for (int j = 0; j < listsByCount.Count; j++)
                {
                    clock.Start();
                    var res = stableSortWithArray.Sort(listsByCount[j]);
                    clock.Stop();

                    time = clock.Elapsed;
                    Console.WriteLine("Length " + listsByCount[0].Length + " Time " + time);
                    clock.Reset();
                }


                keyValues.Add(new Result(stableSortWithArray.Name, listsByCount[0].Length, (i++) % 4 + 1, time));
                time = TimeSpan.Zero;
            }



            return(keyValues);
        }