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