using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace csharpsTest { class Program { static void Main(string[] args) { int[] arr = { 5, 2, 3, 6 }; Program mProgram = new Program(); mProgram.BubbleSort(arr); for (int i = 0; i < arr.Length; ++i) { System.Console.Write(" " + arr[i]); } } void BubbleSort(int[] arr) { int length = arr.Length; int testTimes = 0; for (int i = length - 1; i > 0; --i) { bool isAlready = true; for (int j = 1; j <= i; ++j) { if (arr[j - 1] > arr[j]) { Swap(ref arr[j - 1], ref arr[j]); isAlready = false; } } testTimes++; if (isAlready) { break; } } System.Console.WriteLine("testTims:" + testTimes); } void Swap(ref int a, ref int b) { int temp = a; a = b; b = temp; } } }