The Bubble Sorting Algorithm is often seen, especially in some pen questions. next we will discuss the Bubble Sorting in c #. I have provided three solutions and will analyze their respective performance advantages and disadvantages. the first method is to estimate what everyone has mastered and use data exchange to achieve it. This is not much to say. The predecessors in the garden have a good analysis. There are a lot to search. simply paste the code: Copy code 1 // define array 2 static int [] nums = new int [] {100, 99, 45, 56, 67, 78, 98, 8, 7, 65, 55, 43, 32, 23, 35, 36, 38, 37,120,150, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 30, 32, 31, 29, 28, 26, 27, 25, 22, 24 }; 3 static void Main (string [] args) 4 {5 Cons Ole. writeLine ("traditional method of Bubble Sorting"); 6 BubblingOne (); 7 Console. readKey (); 8} 9 // <summary> 10 // Bubble sorting, traditional data exchange achieves 11 /// </summary> 12 static void BubblingOne () 13 {14 // define an array to be sorted 15 16 // define a temporary variable for data exchange 17 int temp; // Save the maximum value of 18 int I, j; // loop variable 19 for (I = 0; I <nums. length-1; I ++) 20 {21 for (j = 0; j <nums. length-1-I; j ++) 22 {23 if (nums [j] <nums [j + 1]) 24 {25 temp = nums [j]; 26 nums [j] = nums [j + 1]; 27 nums [j + 1] = temp; 28} 29} 30} 31 foreach (int c in nums) // use foreach to output the sorted array element 32 {33 Console. write (c + "\ t"); 34} 35} copy the code execution result as follows: Next we will introduce the second implementation method: the implementation of the query expression syntax of Linq. for more information about Linq, see copy code 1 // <summary> 2 // linq Bubble Sorting Algorithm 3 /// </summary> 4 static void BubblingTwo () 5 {6 IEnumerable <int> num = from a in nums orderby a descending select a; 7 foreach (int item in num) 8 {9 Console. write (item + "\ t "); 10} 11} copy the code. Please note that we have implemented the generic interface IEnumerable <T> as an interface through which, you can use the foreach statement to enumerate generic collection classes. Generic collection classes support IEnumerable <T>, just like non-generic collection classes (such as ArrayList) Support IEnumerable. The running result is the same as the first method. You may have discovered that the code is much more concise, and the original eight rows are shortened to one line. Isn't that what we are pursuing? Especially during the interview, does it save you a lot of time? Is there any other way to continue to implement Bubble sorting? The answer is yes. Remember that we operate on the data in the array. There are two extension methods of Linq: OrderByDescending and OrderBy for sorting data. For details, see the third implementation of sorting data: use the extended method OrderByDescending (inverted sort) and OrderBy (Forward sort) of Linq) copy the code /// <summary> /// use the extended OrderByDescending method of Linq /// </summary> static void BubblingThree () {IEnumerable <int> num = nums. orderByDescending (a => a); foreach (int item in num) {Console. write (item + "\ t") ;}} copy the code as concise as ever. note that this method implements the IEnumerable <T> interface. there may be more implementations, This article only introduces these three methods. since there are so many implementation methods, how should we choose which one to use? Here I have made a simple performance comparison (compare their respective execution time ). first, update the above Code and add time calculation: After the red area code is added to the three methods, we will compare the performance of the three methods of different data volumes by inserting 5 data records // 5 static int [] nums = new int [] {10, 99, 45, 56, 67}; the result is as follows: we can draw a simple conclusion that the syntax of the query expression is the worst in the case of small data volumes, and the OrderByDescending method is the best. next we will add a few more data records. // 15 pieces of data static int [] nums = new int [] {100, 99, 45, 56, 67, 78, 98, 8, 7, 65, 55, 43, 38, 37,120}; the result is as follows: Add more data: // 50 static int [] nums = new int [] {101,991,451,561,100, 99, 45, 56, 67, 78, 98, 8, 7, 65, 55, 43, 32, 23, 35, 36, 38, 37,120,150, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 30, 32, 31, 29, 28, 26, 27, 25, 22, 24}. The result is as follows: from this we can see that the situation has changed, and the first method is leading the way. However, in our test, we did not see the advantages of the Linq query expression syntax (thanks @ WAKU for its criticism, however, OrderByDescing () is a good extension method in Linq. The above describes three Bubble sorting methods in C # and performance analysis. the conclusion is inevitable because the data volume is small. Persuasive, interested friends can add tens of thousands more data records.