Esempio n. 1
0
        public IEnumerator <object> GetEnumerator(NumericArrayModel array)
        {
            int n = array.Length;

            // Build max heap
            for (int i = n / 2 - 1; i >= 0; i--)
            {
                var e = Heapify(array, n, i);
                while (e.MoveNext())
                {
                    yield return(null);
                }
            }

            for (int i = n - 1; i >= 0; i--)
            {
                array.Swap(0, i);

                var e = Heapify(array, i, 0);
                while (e.MoveNext())
                {
                    yield return(null);
                }
            }



            for (int i = 0; i < array.Length; i++)
            {
                array.Complete(i);
                yield return(null);
            }
            yield break;
        }
Esempio n. 2
0
        public IEnumerator <object> GetEnumerator(NumericArrayModel array)
        {
            for (int i = 1; i < array.Length; i++)
            {
                for (int j = i; j > 0; j--)
                {
                    bool b = array.IsGreaterThan(j - 1, j);
                    yield return(null);

                    if (b)
                    {
                        array.Swap(j - 1, j);
                        yield return(null);
                    }
                    else
                    {
                        break;
                    }
                }
            }


            for (int i = 0; i < array.Length; i++)
            {
                array.Complete(i);
                yield return(null);
            }
            yield break;
        }
Esempio n. 3
0
        public IEnumerator <object> GetEnumerator(NumericArrayModel array)
        {
            for (int i = 0; i < array.Length - 1; i++)
            {
                int minIndex = i;
                for (int j = i; j < array.Length; j++)
                {
                    bool b = array.IsGreaterThan(minIndex, j);
                    yield return(null);

                    if (b)
                    {
                        minIndex = j;
                    }
                }
                if (minIndex != i)
                {
                    array.Swap(minIndex, i);
                    yield return(null);
                }
            }


            for (int i = 0; i < array.Length; i++)
            {
                array.Complete(i);
                yield return(null);
            }
            yield break;
        }
Esempio n. 4
0
        public IEnumerator<object> GetEnumerator(NumericArrayModel array)
        {
            int i;
            int left;
            int shift = -1;
            int right = array.Length - 1;

            while((left = shift) < right)
            {
                for(i = left + 1; i < right; i++)
                {
                    bool b = array.IsGreaterThan(i, i + 1);
                    yield return null;

                    if (b)
                    {
                        array.Swap(i, i + 1);
                        yield return null;

                        shift = i;
                    }
                }
                
                for(i = (right = shift) - 1; i > left; i--)
                {
                    bool b = array.IsGreaterThan(i, i + 1);
                    yield return null;

                    if (b)
                    {
                        array.Swap(i, i + 1);
                        yield return null;

                        shift = i;
                    }
                }
            }


            for (int j = 0; j < array.Length; j++)
            {
                array.Complete(j);
                yield return null;
            }
            yield break;
        }
Esempio n. 5
0
        public IEnumerator <object> GetEnumerator(NumericArrayModel array)
        {
            IEnumerator <object> sort = Sort(array, 0, array.Length - 1);

            while (sort.MoveNext())
            {
                yield return(null);
            }


            for (int i = 0; i < array.Length; i++)
            {
                array.Complete(i);
                yield return(null);
            }
            yield break;
        }
Esempio n. 6
0
        private IEnumerator <object> Heapify(NumericArrayModel array, int n, int i)
        {
            // Find largest among root, left child and right child
            int largest = i;
            int l       = 2 * i + 1;
            int r       = 2 * i + 2;

            bool b;

            if (l < n)
            {
                b = array.IsGreaterThan(l, largest);
                yield return(null);

                if (b)
                {
                    largest = l;
                }
            }

            if (r < n)
            {
                b = array.IsGreaterThan(r, largest);
                yield return(null);

                if (b)
                {
                    largest = r;
                }
            }

            if (largest != i)
            {
                array.Swap(i, largest);
                yield return(null);

                var e = Heapify(array, n, largest);
                while (e.MoveNext())
                {
                    yield return(null);
                }
            }
            yield break;
        }
Esempio n. 7
0
        public IEnumerator <object> GetEnumerator(NumericArrayModel array)
        {
            int gap = array.Length / 2;

            do
            {
                gap = gap / 2;
                if (gap % 2 == 0)
                {
                    gap++;
                }

                for (int i = 0; i < gap; i++)
                {
                    for (int j = i + gap; j < array.Length; j += gap)
                    {
                        for (int k = j - gap; k >= i; k -= gap)
                        {
                            bool b = array.IsGreaterThan(k, k + gap);
                            yield return(null);

                            if (b)
                            {
                                array.Swap(k, k + gap);
                                yield return(null);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
            }while (gap > 1);


            for (int i = 0; i < array.Length; i++)
            {
                array.Complete(i);
                yield return(null);
            }
            yield break;
        }
Esempio n. 8
0
        private IEnumerator <object> Sort(NumericArrayModel array, int left, int right)
        {
            if (left < right)
            {
                int pivot = left;

                int i = left;

                for (int j = left + 1; j <= right; j++)
                {
                    bool b = array.IsGreaterThan(pivot, j);
                    yield return(null);

                    if (b)
                    {
                        i++;
                        array.Swap(j, i);
                        yield return(null);
                    }
                }
                array.Swap(left, i);
                yield return(null);

                pivot = i;

                IEnumerator <object> sort1 = Sort(array, left, pivot - 1);
                while (sort1.MoveNext())
                {
                    yield return(null);
                }
                IEnumerator <object> sort2 = Sort(array, pivot + 1, right);
                while (sort2.MoveNext())
                {
                    yield return(null);
                }
            }
            yield break;
        }
Esempio n. 9
0
        public IEnumerator <object> GetEnumerator(NumericArrayModel array)
        {
            for (int i = array.Length - 1; i >= 0; i--)
            {
                for (int j = 0; j < i; j++)
                {
                    bool b = array.IsGreaterThan(j, j + 1);
                    yield return(null);

                    if (b)
                    {
                        array.Swap(j, j + 1);
                        yield return(null);
                    }
                }
            }

            for (int i = 0; i < array.Length; i++)
            {
                array.Complete(i);
                yield return(null);
            }
            yield break;
        }
Esempio n. 10
0
        public IEnumerator <object> GetEnumerator(NumericArrayModel array)
        {
            const float shrink = 1.3f;

            float gap     = array.Length;
            bool  swapped = true;

            while (gap > 1 || swapped)
            {
                if ((gap /= shrink) < 1)
                {
                    gap = 1;
                }
                swapped = false;
                for (int j = 0; j < array.Length - gap; j++)
                {
                    bool b = array.IsGreaterThan(j, j + (int)gap);
                    yield return(null);

                    if (b)
                    {
                        swapped = true;
                        array.Swap(j, j + (int)gap);
                        yield return(null);
                    }
                }
            }


            for (int i = 0; i < array.Length; i++)
            {
                array.Complete(i);
                yield return(null);
            }
            yield break;
        }
Esempio n. 11
0
 public IEnumerator <object> GetEnumerator(NumericArrayModel array)
 {
     throw new NotImplementedException();
 }