C # Bubble sorting,
Basic Principles
Compare the size of two adjacent numbers. After each comparison, place the maximum number at the end of the current round. Suppose there are Arrays: 258,445,131, 258, 445, 28, 28, the first round: And, the position does not need to be exchanged. In the second round: Comparison between 445 and 131, 445 is bigger than 131, then 445 is in the back, 131 is in the front, and so on. In the first round, the results are 258,131, 50,445, after the first round of comparison, the largest element ran to the last one. Therefore, for the second round of comparison, the last element does not need to be compared. In the second round, the comparison starts from index 0 and Index 1, but the comparison is not allowed. The algorithms are the same. The third and fourth rounds, and so on.
Code
public class Program { static List<int> list = new List<int>() { 258,445,131,97,22,36,17,38,28,50 }; static void Main(string[] args) { int temp; for (int i = list.Count; i > 0; i--) { for (int j = 0; j < i - 1; j++) { if (list[j] > list[j + 1]) { temp = list[j]; list[j] = list[j + 1]; list[j + 1] = temp; } } printlist(); } Console.ReadLine(); } static void printlist() { foreach( var s in list) { Console.Write(string.Format("{0} ",s)); } Console.WriteLine(); } }Output result