排序算法
冒泡排序算法
/// <summary> /// 冒泡排序 /// </summary> /// <param name="arr"></param> public void BubbleSort() { int temp; for (int i = upper; i >= 1; i--) { for (int j = 0; j <= i - 1; j++) { if (arr[j] > arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } }
选择排序算法
/// <summary> /// 选择排序算法 /// </summary> public void SelectionSort() { int min, temp; for (int i = 0; i <= upper; i++) { min = i; for (int j = i + 1; j <= upper; j++) { if (arr[min] > arr[j]) { min = j; } } if (min != i) { temp = arr[i]; arr[i] = arr[min]; arr[min] = temp; } } }
插入排序算法
/// <summary> /// 插入排序 /// </summary> public void InsertionSort() { int inner, temp; for (int outer = 1; outer <= upper; outer++) { temp = arr[outer]; inner = outer; while (inner > 0 && arr[inner - 1] >= temp) { arr[inner] = arr[inner - 1]; inner -= 1; } arr[inner] = temp; } }
效率比较
总结,插入排序最慢,它的换位置次数最多,其次是冒泡排序,最快的是选择排序