数据结构与算法(8)--行排序 列排序

第一是行间排序,第二是行列变换,第三是前后移动元素,这些算法都组合起来,整个过程就变得很复杂了。

这次我是用二维数组来实现的,也可以用vector实现同样的效果,感觉能用vector还是用vector吧。而且我把书中的列排序,改变成行排序了,因为C++的习惯还是行优先数组。

#include<iostream>

using namespace std;

const int COLUMNS = 18;
const int ROWS = 3;

template<typename T>
void compareExchange(T& a, int i, int j)
{
	if(a[i]>a[j])
		swap((a[i]), (a[j]));
	//cout<<a[i]<<" "<<a[j]<<endl;
}

template<typename T>
void insertionSort(T& a, int n)
{
	for(int j=1; j<n; j++)
		for(int i=j-1; i>=0; i--)
		{
			compareExchange(a, i, i+1);
		}
}

template<typename T>
void rowSort(T (&a)[ROWS][COLUMNS])
{
	int s=ROWS, c=COLUMNS;
	//Handle the enter data, make sure it fit this algorithm requirements
	bool flag = true;
	if((c%2) != 0)
	{
		cerr<<"Error: Columns c must be even!"<<endl;
		flag = false;
	}
	if((c%s) != 0)
	{
		cerr<<"Error: Rows s must be a divisor of columns c!"<<endl;
		flag = fa
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值