示例#1
0
        public T[] CompareAndSortArray(T[] sortedArray, T nextValueToCompare)
        {
            ValueSwaper <T> swapper = new ValueSwaper <T>();

            T[] temparray = new T[sortedArray.Length + 1];
            for (int i = 0; i < sortedArray.Length; i++)
            {
                temparray[i] = sortedArray[i];
            }
            temparray[sortedArray.Length] = nextValueToCompare;
            bool swapped;

            do
            {
                swapped = false;
                for (int i = temparray.Length - 1; i > 0; i--)
                {
                    if (temparray[i].CompareTo(temparray[i - 1]) == -1)
                    {
                        sortedArray = swapper.Swap(temparray, i - 1, i);
                        swapped     = true;
                    }
                }
            } while (swapped);
            return(swapped == true ? sortedArray : temparray);
        }
        //PesudoCode
        //Ref : https://visualgo.net/sorting
        //repeat(numOfElements - 1) times
        //  set the first unsorted element as the minimum
        //  for each of the unsorted elements
        //      if element<currentMinimum
        //       set element as new minimum
        //  swap minimum with first unsorted position
        public IEnumerable <T> Sort(IEnumerable <T> items)
        {
            if (items == null)
            {
                throw new ArgumentNullException("input value cannot be null");
            }
            if (items.Count() == 0)
            {
                throw new ArgumentException("input collection should have atleast one value ");
            }
            ICollection <T> SortedArray = new List <T>();
            ValueSwaper <T> swapper     = new ValueSwaper <T>();
            T unsortedArrayWithoutCurrentMin;

            for (int i = 1; i < items.Count(); i++)
            {
                unsortedArrayWithoutCurrentMin =
                    (SortedArray == null)
                                ? items.Skip(i - 1).Min()
                                : items.Except(SortedArray).Min();
                SortedArray.Add(unsortedArrayWithoutCurrentMin);
            }
            var maxvalue = items.Max();

            SortedArray.Add(maxvalue);
            return(SortedArray);
        }
示例#3
0
        //PesudoCode
        //Ref : https://visualgo.net/sorting
        //do
        //swapped = false
        //for i = 1 to indexOfLastUnsortedElement
        //if leftElement > rightElement
        //swap(leftElement, rightElement)
        //swapped = true
        //while swapped

        public T[] Sort(T[] items)
        {
            if (items == null)
            {
                throw new ArgumentNullException("input value cannot be null");
            }
            if (items.Length == 0)
            {
                throw new ArgumentException("input collection should have atleast one value ");
            }
            bool swapped;

            T[]             sortedItem = new T[items.Length - 1];
            ValueSwaper <T> swapper    = new ValueSwaper <T>();

            do
            {
                swapped = false;
                for (int i = 1; i < items.Length; i++)
                {
                    if (items[i - 1].CompareTo(items[i]) == 1)
                    {
                        sortedItem = swapper.Swap(items, i - 1, i);
                        swapped    = true;
                    }
                }
            } while (swapped);


            return(sortedItem);
        }