Exemple #1
0
        public void SortNaturalRandom()
        {
            var valsSpan  = _vals.AsSpan();
            var vals2Span = _vals2.AsSpan();

            for (int i = 0; i < _arrays.Length; i++)
            {
                IntroSort <int, int, int> .Sort(_arrays[i], valsSpan, vals2Span);
            }
        }
Exemple #2
0
        public void Test1()
        {
            int[] keys = new int[] { 5, 8, 2, 16, 32, 12, 7 };
            int[] v    = new int[]    { 45, 42, 48, 24, 8, 28, 43 };
            int[] w    = new int[]    { 0, 1, 2, 3, 4, 5, 6 };
            IntroSort <int, int, int> .Sort(keys, v, w);

            Assert.IsTrue(AreEqual(new int[] { 2, 5, 7, 8, 12, 16, 32 }, keys));
            Assert.IsTrue(AreEqual(new int[] { 48, 45, 43, 42, 28, 24, 8 }, v));
            Assert.IsTrue(AreEqual(new int[] { 2, 0, 6, 1, 5, 3, 4 }, w));
        }
Exemple #3
0
        public void Sort_ShortArray()
        {
            int[] keys = new int[] { 5, 8, 2, 16, 32, 12, 7 };
            int[] v    = new int[]    { 45, 42, 48, 24, 8, 28, 43 };
            int[] w    = new int[]    { 0, 1, 2, 3, 4, 5, 6 };
            IntroSort <int, int, int> .Sort(keys, v, w);

            Assert.True(SpanUtils.Equal <int>(new int[] { 2, 5, 7, 8, 12, 16, 32 }, keys));
            Assert.True(SpanUtils.Equal <int>(new int[] { 48, 45, 43, 42, 28, 24, 8 }, v));
            Assert.True(SpanUtils.Equal <int>(new int[] { 2, 0, 6, 1, 5, 3, 4 }, w));
        }
Exemple #4
0
        public void HeapSortUsed()
        {
            var sort = new IntroSort <Item <int> >(3);
            var data = Enumerable.Range(0, 50)
                       .Select(i => new Item <int>(Randomizer.Next(), i))
                       .ToArray();

            sort.Sort(data);
            Assert.True(IsSorted(data));
            Assert.True(sort.HeadSortUsed);
        }
Exemple #5
0
        private void LongRandomArraysInner(int len, IRandomSource rng)
        {
            // Create random array.
            int[] keys = CreateRandomArray(len, rng);
            int[] v    = (int[])keys.Clone();
            int[] w    = (int[])keys.Clone();

            // Sort array.
            IntroSort <int, int, int> .Sort(keys, v, w);

            // Check array is sorted.
            Assert.True(SortUtils.IsSortedAscending <int>(keys));
            Assert.True(SortUtils.IsSortedAscending <int>(v));
            Assert.True(SortUtils.IsSortedAscending <int>(w));
        }
Exemple #6
0
 public void Sort_NaturallyOrderedKeys()
 {
     IntroSort <int, int, int> .Sort(_keys, _values, _values2);
 }
Exemple #7
0
 public void Sort()
 {
     IntroSort <int, int, int> .Sort(_keys, _values, _values2);
 }
Exemple #8
0
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            Stopwatch sw          = new Stopwatch();
            bool      ShowConsole = false;
            int       size        = 40000;

            List <int> bubbleList    = new List <int>();
            List <int> optBubbleList = new List <int>();
            List <int> stdSortList   = new List <int>();
            List <int> insertionSort = new List <int>();
            List <int> heapsortList  = new List <int>();
            List <int> introsortList = new List <int>();
            List <int> quicksortList = new List <int>();

            Random rand = new Random();

            Console.WriteLine("Initial Array ");

            for (int i = 0; i < size; i++)
            {
                bubbleList.Add(rand.Next() % 100);
                optBubbleList.Add(bubbleList[i]);
                stdSortList.Add(bubbleList[i]);
                insertionSort.Add(bubbleList[i]);
                heapsortList.Add(bubbleList[i]);
                introsortList.Add(bubbleList[i]);
                quicksortList.Add(bubbleList[i]);
                if (ShowConsole)
                {
                    Console.Write(bubbleList[i] + "; ");
                }
            }

            Console.WriteLine();

            long time;

            // Standard Sort

            sw.Start();
            stdSortList.Sort();
            sw.Stop();
            time = sw.ElapsedMilliseconds;
            sw.Reset();

            if (ShowConsole)
            {
                ShowList(stdSortList);
            }

            Console.WriteLine("Standard Sort en " + time + " ms");

            // Test Bubble Sort
            // sw.Start();
            //// BubbleSort.Sort<int>( bubbleList );
            // sw.Stop();
            // time = sw.ElapsedMilliseconds;
            // sw.Reset();

            // if ( ShowConsole )
            // {
            //     ShowList( bubbleList );
            // }

            // Console.WriteLine( "Bubble Sort en "+time+" ms" );


            // Test Optimized Bubble Sort

            //sw.Start();
            ////BubbleSort.OptimizedSort<int>( optBubbleList );
            //sw.Stop();
            //time = sw.ElapsedMilliseconds;
            //sw.Reset();

            //if ( ShowConsole )
            //{
            //    ShowList( optBubbleList );
            //}
            //Console.WriteLine( "Optimized Bubble Sort en " + time + " ms" );

            // Insertion Sort

            //sw.Start();
            //InsertionSort.InPlaceSort<int>( insertionSort );
            //sw.Stop();
            //time = sw.ElapsedMilliseconds;
            //sw.Reset();

            //if ( ShowConsole )
            //{
            //    ShowList( insertionSort );
            //}
            //Console.WriteLine( "Insertion Sort en " + time + " ms" );

            //// HeapSort

            //sw.Start();
            //heapsortList = HeapSort.Sort<int>( heapsortList );
            //sw.Stop();
            //time = sw.ElapsedMilliseconds;
            //sw.Reset();

            //if ( ShowConsole )
            //{
            //    ShowList( heapsortList );
            //}
            //Console.WriteLine( "HeapSort en " + time + " ms" );

            // QuickSort

            sw.Start();
            QuickSort.OptimizedSort <int>(quicksortList);
            sw.Stop();
            time = sw.ElapsedMilliseconds;
            sw.Reset();

            if (ShowConsole)
            {
                ShowList(quicksortList);
            }
            Console.WriteLine("Quicksort en " + time + " ms");

            // IntroSort

            sw.Start();
            IntroSort.InPlaceSort <int>(introsortList);
            sw.Stop();
            time = sw.ElapsedMilliseconds;
            sw.Reset();

            if (ShowConsole)
            {
                ShowList(introsortList);
            }
            Console.WriteLine("IntroSort en " + time + " ms");


            Console.Read();
        }
Exemple #9
0
        public void Test()
        {
            var sort = new IntroSort <Item <int> >();

            SortTest(sort, 50, false);
        }