All pastes #184617 Raw Edit

Miscellany

public text v1 · immutable
#184617 ·published 2006-09-28 01:55 UTC
rendered paste body
private void bubble3_sort()
        {
            swaps = 0;
            compares = 0;
            display();
            DateTime startTime = DateTime.Now;

            int first = 0;
            int last = nums.Length-1;
            bool finished = false;
            bool forward = true;
            while (finished == false)
            {
                first--;
                if (first < 0)
                { first = 0; }
                finished = true;
                int templast = first;
                bool firstchange = false;

                if (forward)
                {
                    if (first < 0)
                    { first = 0; }
                    for (int i = first; i < last; i++)
                    {
                        compares++;
                        if (nums[i] > nums[i + 1])
                        {
                            if (firstchange != true)
                            {
                                firstchange = true;
                                first = i - 1;
                                if (first < 0)
                                { first = 0; }
                            }
                            swap(nums, i, i + 1);
                            swaps++;
                            finished = false;
                            templast = i+2;
                        }
                        if (i % often == 0 && i != 0)
                        {
                            display();
                        }

                    }
                    forward = false;
                    last = templast;
                }
                else
                {
                    if (last == nums.Length)
                    { last = nums.Length - 1; }
                    for (int i = last; i > first; i--)
                    {
                        compares++;

                        if (nums[i] < nums[i - 1])
                        {
                            if (firstchange != true)
                            {
                                firstchange = true;
                                last = i+1;
                                if (last == nums.Length)
                                {
                                    last--;
                                }
                                
                            }
                            swap(nums, i, i - 1);
                            swaps++;
                            finished = false;
                            templast = i;
                        }
                        if (i % often == 0 && i != 0)
                        {
                            display();
                        }

                    }
                    forward = true;
                    first = templast-1;
                }

            }
            DateTime endTime = DateTime.Now;
            TimeSpan the_dif = endTime - startTime;
            txt_current.AppendText(Environment.NewLine);
            display();

            txt_history.AppendText("Smarter Bubble Sort:  Compared: " + compares.ToString() + "  " + "Swapped: " + swaps.ToString() + "  " + "Numbers in array: " + nums.Length.ToString() + "  It took: " + the_dif.ToString() + Environment.NewLine);


        }