Ejemplo n.º 1
0
        /// <summary>
        /// Basic bubble sorting function
        /// </summary>
        /// <param name="array"></param>
        /// <returns></returns>
        public static void BubbleSort2(MyArray array)
        {
            array = array.Clone();
            int counter = 0;

            for (int i = array.UpperIndex; i >= 0; i--)
            {
                bool hasSwap = false;
                for (int j = 0; j < i; j++)
                {
                    if (array[j] > array[j + 1])
                    {
                        int temp = array[j];
                        array[j]     = array[j + 1];
                        array[j + 1] = temp;
                        hasSwap      = true;
                    }
                    //Console.Write("     Inner loop:  ");
                    //array.DisplayArray();

                    counter++;
                }

                Console.Write("Outer loop:  ");
                array.DisplayArray();

                if (!hasSwap)
                {
                    break;
                }
            }

            Console.WriteLine("Counter: " + counter.ToString());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Basic bubble sorting function
        /// </summary>
        /// <param name="array"></param>
        /// <returns></returns>
        public static void BubbleSort1(MyArray array)
        {
            int counter = 0;

            array = array.Clone();
            for (int i = array.UpperIndex; i >= 0; i--)
            {
                for (int j = 0; j < i; j++)
                {
                    if (array[j] > array[j + 1])
                    {
                        int temp = array[j];
                        array[j]     = array[j + 1];
                        array[j + 1] = temp;
                    }
                    //Console.Write("     Inner loop:  " );
                    //array.DisplayArray();

                    counter++;
                }

                Console.Write("Outer loop:  ");
                array.DisplayArray();
            }

            Console.WriteLine("Counter: " + counter.ToString());
        }