Search

Categories

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Send mail to the author(s) E-mail

# Thursday, 16 January 2014
( Array | Sorting )

Goal:  sort a bunch of numbers

image
File new project, then Unit Test Project.

image

Test, Run all tests.

use c#
try insertion sort

eg: 9 3 4 5 2

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace SkiffSort
{
    public class SkiffSort
    {
        public int[] Insert(int[] array, int numberToAdd)
        {
            int i = 0;
            for (i = 0; i < array.Length; i++)
            {
                if (array[i] > numberToAdd)
                    break;
            }

            int[] arrayNew = new int[array.Length + 1];
            arrayNew[i] = numberToAdd;

            // botom half
            for (int j = 0; j < i; j++)
            {
                arrayNew[j] = array[j];
            }

            // top half
            for (int k = i+1; k < arrayNew.Length; k++)
            {
                arrayNew[k] = array[k-1];
            }

            return arrayNew;
        }

        public int[] Sort(int[] startArray)
        {
            int[] sortedArray = new int[0];

            foreach (var item in startArray)
            {
                sortedArray = Insert(sortedArray, item);
            }

            return sortedArray;
        }
    }

    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void Sort_GivenAnUnsortedArray_ShouldReturnASortedArray()
        {
            var ss = new SkiffSort();

            int[] startArray = new int[] { 6,8,2,7 };

            var result = ss.Sort(startArray);

            int[] expected = new int[] { 2,6,7,8 };

            CollectionAssert.AreEqual(expected, result);
        }

        [TestMethod]
        public void Sort_GivenAnUnsortedArrayB_ShouldReturnASortedArray()
        {
            var ss = new SkiffSort();

            int[] startArray = new int[] { 6, 8, 2, 7, 1 };

            var result = ss.Sort(startArray);

            int[] expected = new int[] { 1,2, 6, 7, 8 };

            CollectionAssert.AreEqual(expected, result);
        }

        [TestMethod]
        public void Sort_GivenAnUnsortedArrayEmpty_ShouldReturnASortedArray()
        {
            var ss = new SkiffSort();

            int[] startArray = new int[] { };

            var result = ss.Sort(startArray);

            int[] expected = new int[] { };

            CollectionAssert.AreEqual(expected, result);
        }

        [TestMethod]
        public void Sort_GivenAAortedArray_ShouldReturnASortedArray()
        {
            var ss = new SkiffSort();

            int[] startArray = new int[] {2,3,4,5 };

            var result = ss.Sort(startArray);

            int[] expected = new int[] {2,3,4,5 };

            CollectionAssert.AreEqual(expected, result);
        }

        // large arrays

        [TestMethod]
        public void Insert_PassInASortedArrayAndANumberToInsert5_ShouldReturnSortedArrayWithElementInserted()
        {
            var ss = new SkiffSort();

            int[] startArray = new int[] {2,6,8};

            var result = ss.Insert(startArray, 5);

            int[] expected = new int[] { 2, 5, 6, 8 };

            CollectionAssert.AreEqual(expected, result);
        }

        [TestMethod]
        public void Insert_PassInASortedArrayAndANumberToInsert4_ShouldReturnSortedArrayWithElementInserted()
        {
            var ss = new SkiffSort();

            int[] startArray = new int[] { 2, 6, 8 };

            var result = ss.Insert(startArray, 4);

            int[] expected = new int[] { 2, 4, 6, 8 };

            CollectionAssert.AreEqual(expected, result);
        }

        // beginning
        [TestMethod]
        public void Insert_PassInASortedArrayAndANumberToInsert1_ShouldReturnSortedArrayWithElementInserted()
        {
            var ss = new SkiffSort();

            int[] startArray = new int[] { 2, 6, 8 };

            var result = ss.Insert(startArray, 1);

            int[] expected = new int[] { 1, 2, 6, 8 };

            CollectionAssert.AreEqual(expected, result);
        }

        // end
        [TestMethod]
        public void Insert_PassInASortedArrayAndANumberToInsert12_ShouldReturnSortedArrayWithElementInserted()
        {
            var ss = new SkiffSort();

            int[] startArray = new int[] { 2, 6, 8 };

            var result = ss.Insert(startArray, 12);

            int[] expected = new int[] { 2, 6, 8, 12 };

            CollectionAssert.AreEqual(expected, result);
        }
       
        //empty array
        [TestMethod]
        public void Insert_PassInAnEmptyArrayAndANumberToInsert10_ShouldReturnSortedArrayWithElementInserted()
        {
            var ss = new SkiffSort();

            int[] startArray = new int[] {};

            var result = ss.Insert(startArray, 10);

            int[] expected = new int[] { 10 };

            CollectionAssert.AreEqual(expected, result);
        }

    }
}
| | #