MOOC清华《程序设计基础》第4章:插入排序(函数写法)

#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;
}

posted on 2017-10-05 18:17  sunshineman1986  阅读(118)  评论(0)    收藏  举报

导航