Example #1
0
        public static void DoTests(IComparable[] arr, ISortAlgorithm sorter)
        {
            var isSorted = TestHelper.IsSortedAsc(arr);

            Assert.IsFalse(isSorted);

            isSorted = TestHelper.IsSortedDesc(arr);
            Assert.IsFalse(isSorted);

            sorter.Sort(arr);
            isSorted = TestHelper.IsSortedAsc(arr);
            Assert.IsTrue(isSorted);

            arr      = arr.Reverse().ToArray();
            isSorted = TestHelper.IsSortedDesc(arr);
            Assert.IsTrue(isSorted);
        }
        public void ExecuteSort_StaticArray_ReturnsSortedArray(ISortAlgorithm <int> sortAlgorithm)
        {
            List <int> staticList = new List <int>
            {
                10,
                3,
                2,
                11,
                1,
                0,
                0
            };

            IEnumerable <int> sortedCollection = sortAlgorithm.Sort(staticList);

            Assert.That(sortedCollection, Is.Ordered);
        }
        /// <summary>
        /// Sorts the specified IList(Of T) using the algorithm specified in the order specified.
        /// </summary>
        /// <typeparam name="T">type of the elements in toSort</typeparam>
        /// <param name="toSort">the list to sort.</param>
        /// <param name="algorithm">The algorithm to use.</param>
        /// <param name="direction">The direction to sort the elements in.</param>
        /// <param name="startIndex">The start index.</param>
        /// <param name="endIndex">The end index.</param>
        /// <param name="compareFunc">The compare func to compare two elements in the list to sort.</param>
        public static void Sort <T>(this IList <T> toSort, SortAlgorithm algorithm, SortDirection direction, int startIndex, int endIndex, Comparison <T> compareFunc)
        {
            if (toSort.IsNullOrEmpty() || (toSort.Count == 1))
            {
                return;
            }

            int startIndexToUse = startIndex;
            int endIndexToUse   = endIndex;

            if (startIndexToUse < 0)
            {
                startIndexToUse = 0;
            }
            if (startIndexToUse >= toSort.Count)
            {
                throw new IndexOutOfRangeException(string.Format("startIndex's value {0} is outside the list to sort, which has {1} values.", startIndexToUse, toSort.Count));
            }
            if (endIndexToUse < 0)
            {
                endIndexToUse = toSort.Count - 1;
            }
            if (endIndexToUse >= toSort.Count)
            {
                throw new IndexOutOfRangeException(string.Format("endIndex's value {0} is outside the list to sort, which has {1} values.", endIndexToUse, toSort.Count));
            }

            Comparison <T> comparisonToUse = compareFunc;

            if (compareFunc == null)
            {
                comparisonToUse = GeneralUtils.GetUsableComparison <T>(null);
            }

            ISortAlgorithm sorter = SortAlgorithmFactory.CreateSortAlgorithmImplementation(algorithm);

            if (sorter != null)
            {
                sorter.Sort(toSort, direction, startIndexToUse, endIndexToUse, comparisonToUse);
            }
        }
Example #4
0
        /// <summary>
        /// Creates the sort algorithm implementation.
        /// </summary>
        /// <param name="algorithm">The algorithm.</param>
        /// <returns></returns>
        internal static ISortAlgorithm CreateSortAlgorithmImplementation(SortAlgorithm algorithm)
        {
            ISortAlgorithm toReturn = null;

            switch (algorithm)
            {
            case SortAlgorithm.SelectionSort:
                toReturn = new SelectionSorter();
                break;

            case SortAlgorithm.ShellSort:
                toReturn = new ShellSorter();
                break;

            case SortAlgorithm.QuickSort:
                toReturn = new QuickSorter();
                break;
            }

            return(toReturn);
        }
        public SortAlgorithmSnapShot(ISortAlgorithm sort)
        {
            Initial();

            sort.SetSnapShot(this);
        }
 public void SetAlgorithm(ISortAlgorithm sortAlgorithm)
 {
     this.sortAlgorithm = sortAlgorithm;
 }
Example #7
0
 public JobsSorter(IStringValidator stringValidator, JobsBuilder jobsBuilder, JobsValidator jobsValidator, ISortAlgorithm sortAlgorithm)
 {
     _stringValidator = stringValidator;
     _jobsBuilder     = jobsBuilder;
     _jobsValidator   = jobsValidator;
     _sortAlgorithm   = sortAlgorithm;
 }
 public StringBucketSort(ISortAlgorithm sortAlgorithm)
 {
     _sortAlgorithm = sortAlgorithm;
 }
Example #9
0
 public static int[] HelperSort(ISortAlgorithm algorithm, int[] unsorted)
 {
     return(algorithm.Sort(unsorted));
 }
 public SortSync(T[][] arr, ISortAlgorithm <T> sortAlgorithm) : base(arr, sortAlgorithm)
 {
 }
 public Sort(T[] arr, ISortAlgorithm <T> algo)
 {
     array         = arr;
     SortAlgorithm = algo;
 }
Example #12
0
 public SessionSorter(ISortAlgorithm defaultSort, ISortAlgorithm votingSort)
 {
     _defaultSort = defaultSort;
     _votingSort  = votingSort;
 }
 public SessionSorter(ISortAlgorithm defaultSort, ISortAlgorithm votingSort)
 {
     _defaultSort = defaultSort;
     _votingSort = votingSort;
 }
 public SortParallel(T[][] arr, ISortAlgorithm <T> sortAlgorithm) : base(arr, sortAlgorithm)
 {
 }
        public SortAlgorithmSnapShot(ISortAlgorithm sort)
        {
            Initial();

            sort.SetSnapShot(this);
        }
 protected SortBase(T[][] arr, ISortAlgorithm <T> sortAlgorithm)
 {
     Array = arr;
     this.SortAlgorithm = sortAlgorithm;
 }
Example #17
0
 /// <summary>
 /// Создание экземпляра класса <see cref="KruskalAlgorithm"/>
 /// </summary>
 /// <param name="sortAlgorithm">Алгоритм сортировки</param>
 public KruskalAlgorithm(ISortAlgorithm sortAlgorithm)
 {
     _sortAlgorithm = sortAlgorithm ?? throw new ArgumentNullException(nameof(sortAlgorithm));
 }
Example #18
0
        void DrawView()
        {
            Context.MakeCurrent(null);
            GL.Oes.BindFramebuffer(All.FramebufferOes, ViewFrameBuffer);
            GL.Viewport(0, 0, BackingWidth, BackingHeight);

            GL.MatrixMode(All.Projection);
            GL.LoadIdentity();
            // GL.Ortho (-0.55f, 0.55f, -1.5f, 1.5f, -1.0f, 1.0f);
            GL.Ortho(-0.5f, 0.5f, -0.5f, 0.5f, -1.0f, 1.0f);
            GL.MatrixMode(All.Modelview);
            //if (finish)
            //	GL.Rotate (3.0f, 0.0f, 0.0f, 1.0f);

            GL.ClearColor(1f, 1f, 1f, 1.0f);
            GL.Clear(ClearBufferMask.ColorBufferBit);

            float[] squareVerticesD = new float[8];
            float   countX          = MyColorContainer.ColorElements.Length;
            float   countY          = 1f;
            int     i = 0;

            for (float x = 0; x < countX; x += 1f)
            {
                for (float y = 0; y < countY; y += 1f)
                {
                    var   dx    = x / countX;
                    var   dy    = y / countY;
                    float sizeX = 1 / (countX * 1f);
                    float sizeY = 1 / (countY * 1f);
                    squareVerticesD [0] = -0.5f + dx;
                    squareVerticesD [1] = -0.5f + dy;
                    squareVerticesD [2] = -0.5f + sizeX + dx;
                    squareVerticesD [3] = -0.5f + dy;
                    squareVerticesD [4] = -0.5f + dx;
                    squareVerticesD [5] = -0.5f + sizeY + dy;
                    squareVerticesD [6] = -0.5f + sizeX + dx;
                    squareVerticesD [7] = -0.5f + sizeY + dy;
                    GL.VertexPointer(2, All.Float, 0, squareVerticesD);
                    GL.EnableClientState(All.VertexArray);
                    GL.ColorPointer(4, All.UnsignedByte, 0, MyColorContainer.ColorElement(i));
                    i = (i + 1) % 255;
                    GL.EnableClientState(All.ColorArray);
                    GL.DrawArrays(All.TriangleStrip, 0, 4);
                }
            }
            if (!finish)
            {
                if (sort == null)
                {
                    sort = SortFactory.Create(sortAlgorithm);
                    this.MainViewController.SetSortName(sort.SortName());
                    this.MainViewController.SegmentedControl.SelectedSegment = sortAlgorithm % 4;
                }
                sortstep++;
                if (AnimationInterval != 0 &&
                    sortstep % (int)((1 / AnimationInterval) / 30) == 0)
                {
                    finish = !sort.SortStep(MyColorContainer.ColorElements);
                    this.MainViewController.AnzahlIterationenLabel.Text = string.Format("{0} Interations for sorting {1} Elements", ++this.anzahlIterationen, anzahlElemente);
                }
            }
            else
            {
                if (sort != null)
                {
                    sortAlgorithm++;
                    sort              = null;
                    sortstep          = 0;
                    anzahlIterationen = 0;
                }
                sortstep++;
                if (AnimationInterval != 0 &&
                    sortstep % (int)((1 / AnimationInterval) * 5) == 0)
                {
                    finish = false;
                    MyColorContainer.Randomize(5000);
                }
            }

            // MyColorContainer.Sort ();

            GL.Oes.BindRenderbuffer(All.RenderbufferOes, ViewRenderBuffer);
            Context.EAGLContext.PresentRenderBuffer((uint)All.RenderbufferOes);
        }
Example #19
0
 public void setAlgorithm(ISortAlgorithm algo)
 {
     this.algo = algo;
 }
Example #20
0
 public void SetSortAlgorithm(ISortAlgorithm sortAlgorithm)
 {
     _sortAlgorithm = sortAlgorithm;
 }
 protected StableSortAlgorithmTest(ISortAlgorithm<string> algorithm)
     : base(algorithm)
 {
 }