直接插入排序 可运行代码
帮大家解决燃眉之急 懂得都懂 哈哈
#include <iostream>
using namespace std;
void InsertSort(int r[], int n)
{
int j = 0;
for (int i = 2; i <= n; i++)
{
r[0] = r[i];
j = i - 1;
while (r[0] < r[j])
{
r[j + 1] = r[j];
j--;
}
r[j + 1] = r[0];
}
}
void Print(int r[], int n)
{
for (int i = 1; i <= n; i++)
{
cout << r[i] << " ";
}
}
int main()
{
int array[15];
int n;
cout << "how many numbers do you want to insert" << endl;
cin >> n;
cout << "please insert" << endl;
for (int i = 1; i <= n; i++)
{
cin >> array[i];
}
InsertSort(array, n);
Print(array, n);
}
运行截图