Exemple #1
0
        public void SortWithNullArrayShouldThrowException()
        {
            // Arrange
            SpecialArray.Collections.SpecialArray array = null;

            // Act
            Action action = () => ArrayActions.Sort(array);

            // Assert
            action.Should().Throw <ArgumentException>().WithMessage("Cannot sort a null SpecialArray");
        }
Exemple #2
0
        public void SortArrayWithTwoElementsShouldSortTheArray()
        {
            // Arrange
            var items = new List <int>(new[] { 3, 1 });

            SpecialArray.Collections.SpecialArray array = SpecialArray.Collections.SpecialArray.From(items);

            // Act
            ArrayActions.Sort(array);

            // Assert
            array.Items.Should().BeInAscendingOrder();
        }
Exemple #3
0
        public void SortArrayWithThreeElementsSecondPermutationShouldSortTheArray()
        {
            // Arrange
            var items = new List <int>(new[] { 3, 1, 2 });

            SpecialArray.Collections.SpecialArray array = SpecialArray.Collections.SpecialArray.From(items);
            var expectedItems = new List <int>(new[] { 1, 2, 3 });

            // Act
            ArrayActions.Sort(array);

            // Assert
            array.Items.Should().BeInAscendingOrder();
        }
Exemple #4
0
        public void SortArrayWithManyElementsShouldSortTheArray()
        {
            // Arrange
            var items = new List <int>(new[] { 7, 8, -2, 6, 2, 3, 1 });

            SpecialArray.Collections.SpecialArray array = SpecialArray.Collections.SpecialArray.From(items);
            var expectedItems = new List <int>(new[] { -2, 1, 2, 3, 6, 7, 8 });

            // Act
            ArrayActions.Sort(array);

            // Assert
            array.Items.Should().BeInAscendingOrder();
        }
Exemple #5
0
        public static void Sort(SpecialArray array)
        {
            if (array == null)
            {
                throw new ArgumentException("Cannot sort a null SpecialArray");
            }

            for (int index = array.Size() - 1; index > 0; index--)
            {
                for (int subIndex = index - 1; subIndex >= 0; subIndex--)
                {
                    if (array[subIndex] > array[index])
                    {
                        array.Swap(index);
                        array.Swap(subIndex);
                    }
                }
            }
        }