插入排序就像摸牌一下,手中的牌一直都是排好序的,新的牌插到这个已排序序列 中 算法复杂度: 最佳情况:T(n) = O(n) 最坏情况:T(n) = O(n^2) C++代码实现如下: #include <iostream> using namespace std; typedef int ElementType; void InsertionSort(ElementType elems[], int size); int main() { ElementType EleArray [10]; for (int i = 0; i < 10; i++) cin >> EleArray[i]; InsertionSort(EleArray, 10); for (int i = 0; i < 10; i++) cout << EleArray[i] <<endl; int h; cin >> h; } /*插入排序:递减*/ void InsertionSort(ElementType elems[], int size) { for (int i = 1; i < size; i++) { int j = i - 1; ElementType key = elems[i]; while (key > elems[j] && j >= 0) { elems[j + 1] = elems[j]; j--; } elems[j + 1] = key; } }