从小到大
/// <summary> /// 冒泡排序(从小到大) /// </summary> /// <param name="list"></param> void Sort(List<int> list) { for (int i = list.Count - 1; i > 0; --i) { for (int j = 0; j < i; j++) { if (list[j] > list[j + 1]) { int temp = list[j]; list[j] = list[j + 1]; list[j + 1] = temp; } } } }
从大到小
/// <summary> /// 冒泡排序(从大到小) /// </summary> /// <param name="list"></param> void Sort(List<int> list) { for (int i = list.Count - 1; i > 0; --i) { for (int j = 0; j < i; j++) { if (list[j] < list[j + 1]) { int temp = list[j]; list[j] = list[j + 1]; list[j + 1] = temp; } } } }