Bubble sort of sorting algorithm

Source: Internet
Author: User

        Static voidMain (string[] args) {            int[] Array = { -, $, the, at,235,314,131,3,456, the};            Bubblesort (array);        Console.ReadLine (); }                 /// <summary>        ///name: Bubble sort///principle: If the number of n is ordered, the number of n-1 needs to be normalized, that is, the n-1 trip is required.        Each trip needs to be compared from the 1th number until the last one has not been returned. ///Example: 10 numbers to sort, the first trip to the smallest number will be returned to the last, that is, the 10th place, the second trip will be the 2nd place on the number of digits, the third will be the countdown 3rd position on the number, according to this, the last trip does not return to the position, because the 1th bit has been returned to the position///time Complexity: The worst case is O (squared of N) The best case is O (n)///Optimization: Add an identity variable, when the comparison sequence is {2,1,3,4,5,6,7,8,9,10}, only one comparison is required, the time complexity is O (n)/// </summary>        /// <param name= "Array" ></param>        Static voidBubblesort (int[] Array) {            BOOLFlag =true;  for(inti =0; I < array. Length && Flag; i++) {flag=false; //If J starts from 0 then array[j]<array[j+1] j+1 will cross                 for(intj =1; J < Array. Length-i; J + +) {Console.WriteLine (string. Format ("I={0},j={1}", i,j)); if(array[j-1] >Array[j]) {                        vart = array[j-1]; Array[j-1] =Array[j]; ARRAY[J]=T; Flag=true; }}} Console.WriteLine ("The result of the bubbling sort is:"); foreach(varIteminchArray)            {Console.WriteLine (item); }        }

Bubble sort of sorting algorithm

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.