代码段
#include<iostream>
using namespace std;
void InsertionSort(int* arr, int len) {
for (int j = 1; j < len; j++) {
int key = arr[j];
int i = j - 1;
while (i >= 0 && key < arr[i]) {
arr[i + 1] = arr[i];
i--;
}
arr[i + 1] = key;
}
}
void PrintfArray(int* arr, int len) {
for (int i = 0; i < len; i++) {
cout << arr[i]<<" ";
}
}
int main() {
int size;
cout << "请输入你要输入数组元素个数:" << endl;
cin >> size;
cout << "请输入你要输入的数组:" << endl;
int* array = new int[size];
for (int i = 0; i < size; i++) {
cin >> array[i];
}
InsertionSort(array, size);
PrintfArray(array, size);
return 0;
}
如有不对请斧正,大家一起进步!!!