Beispiel #1
0
        public void sort(SortingArray array, int sortingSpeed)
        {
            Stopwatch sw = new Stopwatch();

            for (int i = 1; i < array.Length; i++)
            {
                int j = i;
                while (j > 0 && array.values[j - 1] > array.values[j])
                {
                    array.setColorAt(System.Drawing.Color.Red, j);
                    array.notifySubscribers();

                    sw.Start();
                    while (true)
                    {
                        if (sw.ElapsedMilliseconds > sortingSpeed)
                        {
                            break;
                        }
                    }
                    sw.Reset();

                    int pom = array.values[j];
                    array.values[j]     = array.values[j - 1];
                    array.values[j - 1] = pom;
                    array.setColorAt(System.Drawing.Color.Green, j);
                    j--;
                }
            }
            array.notifySubscribers();
        }
Beispiel #2
0
        public void sort(SortingArray array, int sortingSpeed)
        {
            int n = array.Length;

            Stopwatch sw = new Stopwatch();

            while (n > 1)
            {
                int new_n = 0;
                for (int i = 1; i < n; i++)
                {
                    array.setColorAt(System.Drawing.Color.Red, i - 1);
                    array.notifySubscribers();

                    sw.Start();
                    while (true)
                    {
                        if (sw.ElapsedMilliseconds > sortingSpeed)
                        {
                            break;
                        }
                    }
                    sw.Reset();

                    if (array.values[i] < array.values[i - 1])
                    {
                        int pom = array.values[i];
                        array.values[i]     = array.values[i - 1];
                        array.values[i - 1] = pom;
                        new_n = n - 1;
                    }
                    array.setColorAt(System.Drawing.Color.Green, i - 1);
                }
                n = new_n;
            }
            array.notifySubscribers();
        }