插入排序法(升序)
#include<iostream>
using namespace std;
void main(){
int n; //数组长度
int* arr; //数组
//从键盘输入数据
cout << "输入数组长度:";
cin >> n;
arr = new int[n];
for (int i = 0; i < n; i++)
cin >> *(arr + i);
//插入排序(升序)
for (int i = 1; i < n; i++){
int key = *(arr + i);
for (int j = i - 1; i >= 0 && *(arr + j) > key; i--){
*(arr + j + 1) = *(arr + j);
*(arr + j) = key;
}
}
//打印arr
for (int i = 0; i < n; i++)
{
cout << *(arr + i) << " ";
}
system("pause");
}