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