List<int> list = new List<int>() { 72, 90, 83, 54, 59, 30, 31, 78, 2, 77, 82, 84, 85, 86, 89 };
void BubbleSort(List<int> list)
{
int temp = 0;
int i = 0, j = 0;
for (i = 0; i < list.Count - 1; i++)
{
bool flag = false;
//比较i次就少i个,因为比较过的都在最后,已经排好了
for (j = 0; j < list.Count - i - 1; j++)
{
if (list[j] > list[j + 1])
{
temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
flag = true;
}
}
if (flag == false) break;//flag == false,此次未交换过,证明已经排好了
}
}
转载:https://blog.csdn.net/km930324/article/details/101776147