#include <iostream>
using namespace std;
int main()
{
int a[17]={56,32,67,12,23,89,3,14,2,25,43,31,1,24,44,35,76};
void InsertionSort(int cards[], int n); //指针作形参必须写出形参名
void OutputOfArray(int cards[], int n); //指针作形参必须写出形参名
OutputOfArray(a,10);
InsertionSort(a,10); //可以只排前10个数
OutputOfArray(a,10); //可以只输出前10个数
return 0;
}
void InsertionSort(int cards[],int n)
{
for(int i = 1; i < n; i++)
{
int target = cards[i], pos = 0;
while(target > cards[pos])
pos++;
for(int j = i; j > pos; j--)
cards[j] = cards[j - 1];
cards[pos] = target;
}
}
void OutputOfArray(int cards[],int n)
{
for(int i = 0; i < n; i++)
cout << cards[i] << '\t';
cout << endl;
}
MOOC清华《程序设计基础》第4章:插入排序(函数写法)
最新推荐文章于 2023-05-21 12:57:26 发布