C #: Bubbling sorting algorithm

Source: Internet
Author: User

Note: int[] arrays are reference types

1 namespaceDemo2 {3      Public class Program4     {5         Static voidMain (string[] args)6         {7             //Original Array8             int[] Nums =New int[] { A,Ten,4,8,1,3,2,9,5,6, - };9Console.Write ("Original array:");Ten             foreach(intNinchnums) OneConsole.Write ($"{n}"); A  -             //Ascending - BUBBLESORTASC (nums); theConsole.Write ("\ n-Large:"); -             foreach(intNinchnums) -Console.Write ($"{n}"); -  +             //Descending - Bubblesortdesc (nums); +Console.Write ("\ n-Small:"); A             foreach(intNinchnums) atConsole.Write ($"{n}"); - Console.readkey (); -         } -         /// <summary> -         ///sort from small to large (using intermediate variables) -         /// </summary> in         Static voidBUBBLESORTASC (int[] nums) -         { to             inttmp; +              for(intI=0; I<nums. length;i++) -             { the                  for(intj=i+1; J<nums. length;j++) *                 { $                     if(nums[j]<Nums[i])Panax Notoginseng                     { -TMP =Nums[i]; theNums[i] =Nums[j]; +NUMS[J] =tmp; A                     } the                 } +             } -         } $         /// <summary> $         ///sort from big to small -         /// </summary> -         Static voidBubblesortdesc (int[] nums) the         { -              for(intI=0; I<nums. length;i++)Wuyi             { the                  for(intj=i+1; J<nums. length;j++) -                 { Wu                     if(nums[i]<Nums[j]) -                     { AboutNums[i] = Nums[i] +Nums[j]; $NUMS[J] = nums[i]-Nums[j];  -Nums[i] = nums[i]-Nums[j]; -                     } -                 } A             }  +         } the     } -}

Execution Result:

C #: Bubbling 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.