scau 8638 直接插入排序

scau 8638 直接插入排序,第1张

时间限制:1000MS  代码长度限制:10KB
提交次数:2050 通过次数:1393

题型: 编程题 语言: G++;GCC

Description
用函数实现直接插入排序,并输出每趟排序的结果.



 

输入格式
第一行:键盘输入待排序关键的个数n
第二行:输入n个待排序关键字,用空格分隔数据


 

输出格式
每行输出一趟排序结果,数据之间用一个空格分隔


 

输入样例
10
5 4 8 0 9 3 2 6 7 1


 

输出样例
4 5 8 0 9 3 2 6 7 1
4 5 8 0 9 3 2 6 7 1
0 4 5 8 9 3 2 6 7 1
0 4 5 8 9 3 2 6 7 1
0 3 4 5 8 9 2 6 7 1
0 2 3 4 5 8 9 6 7 1
0 2 3 4 5 6 8 9 7 1
0 2 3 4 5 6 7 8 9 1
0 1 2 3 4 5 6 7 8 9


 

提示
 


 

作者

 yqm
 

Version:

0



#include 

using namespace std;

int ans[1000];

void print(int* a)
{
	for (int i = 1; i <= a[0]; i++)
		cout << a[i] << ' ';
	cout << endl;
}

void insertsort(int* ans)
{
	int i, j;
	for (i = 2; i <= ans[0]; i++) {
		if (ans[i] < ans[i - 1]) {
			j = i;
			while (j > 1 && ans[j] < ans[j - 1]) {
				swap(ans[j], ans[j - 1]);
				--j;
			}	
		}
		print(ans);
	}
}

int main()
{
	int i, n;
	cin >> n;
	ans[0] = n;
	for (i = 1; i <= n; i++)
		cin >> ans[i];
	insertsort(ans);
}

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1325798.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-12
下一篇2022-06-12

发表评论

登录后才能评论

评论列表(0条)

    保存