比如实现 1, 2, 3, 4, 5按从大到小排序变成5,4,3,2,1,使用冒泡排序,就是每次都把列表或者数组的第一个数字往后推,直到无法再推(if条件不满足)为止,依次类推
class Program
{
static void Main(string[] args)
{
List<int> list = new List<int>() { 1, 2, 3, 4, 5 };
for (int m = 0; m < list.Count; m++)
{
if (m == list.Count - 1)
{
Console.Write(list[m] + "");
Console.WriteLine();
}
else
{
Console.Write(list[m] + ",");
}
}
BubbleSort(list);
Console.Read();
}
public static void BubbleSort(List<int> list)
{
for (int i = 0; i < list.Count - 1; i++)
{
Console.WriteLine("在第1个for循环之内");
for (int j = 0; j < list.Count - 1 - i; j++)
{
Console.WriteLine("在第二个for循环之内");
if (list[j] < list[j + 1])
{
int temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
for (int m = 0; m < list.Count; m++) {
if (m==list.Count-1) {
Console.Write(list[m] + "");
Console.WriteLine();
}
else
{
Console.Write(list[m] + ",");
}
}
}
}
}
}
}