C # version Bubble sort optimization

Source: Internet
Author: User

Previously wrote a C # version of the bubble sort implementation. Since the previous version was implemented with a two-tier for loop, the ordering of this version is still more space-optimized.

Before the sort:

int temp = 0;            for (int i = arr.) Length-1; i > 0; i--)            {for                (int j = 0; J < i; j + +)                {                    if (Arr[j] > arr[j + 1])                    {                        temp = arr[j + 1];                        Arr[j + 1] = Arr[j];                        ARR[J] = temp;}}            }
It can be seen that due to the existence of a two-layer for loop, the entire sort must be performed (N-2)! The second judgment, even if the first generation of the array is already sorted.

The bubbling sort of this version is optimized:

Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks;        Namespace _0301 Array's bubble sort _ Optimization {class Program {static void Main (string[] args) {int len = 0;        Array size int min = 0;        Array lower bound int max = 0;            The upper bound of the array Console.WriteLine ("A random array will be generated below");                Receive array size do {Console.WriteLine ("Enter the array size, it must be a positive integer less than or equal to 10:"); len = Int.            Parse (Console.ReadLine ()); } while ((len <= 0) | |            (Len > 10));            Receive Array Nether Console.WriteLine ("Please enter the lower bound of the array, it must be an integer:"); min = Int.            Parse (Console.ReadLine ());                Receive array upper bound do {Console.WriteLine ("Enter the upper bounds of the array, it must be an integer, and be larger than the minimum value of the array, and allow the array to hold {0} number:", Len); max = Int.            Parse (Console.ReadLine ()); } while ((Max <= min) | |            ((Max-min + 1) < Len);            Console.WriteLine (); ConsolE.writeline ("array length: {0}\n array lower bound: {1}\n array upper bound: {2}", Len, Min, Max);            Console.WriteLine ("Generate array as follows:");            int iSeed = 6;            Random RA = new Random (iSeed);            int[] arr = new Int[len]; Prints the value of the original array for (int i = 0; i < arr. Length; i++) {Arr[i] = ra.                Next (min, max);                Console.Write (Arr[i]); if (I < arr.                Length-1) {Console.Write (",");                } else {Console.WriteLine ();            }}//Start array sort Console.WriteLine ("Array generation finished, start sorting");            Console.WriteLine ();            #region The original sort, this sort must be compared (n-2)! Secondary//int temp = 0; for (int i = arr.) Length-1; i > 0; i--)//{//for (int j = 0; J < i; j + +)//{//if (Arr[j] ; Arr[j + 1])//{//TEMP = arr[j + 1];            Arr[j + 1] = Arr[j];            ARR[J] = temp;            }//}//} #endregion//This sort is the most comparable (N-2)! times, but if the current is the optimal sort result then stop the comparison directly            int temp = 0;            BOOL swapped = true;                do {swapped = false; for (int i = 0; i < arr. Length-1; i++) {if (Arr[i] > arr[i + 1]) {temp = AR                        R[i];                        Arr[i] = arr[i + 1];                        Arr[i + 1] = temp;                    swapped = true;            }}} while (swapped);            Prints the sorted result Console.WriteLine ("Post-order Result:"); for (int i = 0; i < arr. Length;                i++) {Console.Write (arr[i]); if (I < arr.            Length-1) {Console.Write (",");    } else {Console.WriteLine ();            }} Console.WriteLine ("End of Program");        Console.readkey (); }    }}
This version of the order changes the outer loop to the do...while () implementation, and sets an identity variable swapped, if the internal loop does not perform a data substitution, the array sequence has been optimized, immediately ending the outer loop. This sort is most comparable (N-2)! times, but stops the comparison directly if the result is currently the optimal sort.

In fact, the outer loop is a for loop that can also do this, but relatively do...while () is more elegant to write.

Operating effect:


C # version Bubble sort optimization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.