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

本文介绍了如何使用二维数组实现行排序算法,将书中的列排序调整为行排序,探讨了行间排序、行列变换和元素移动的组合应用。建议使用vector进行实现,文章来源于Slience的csdn技术博客,分享了原创的算法思想。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

这次我是用二维数组来实现的,也可以用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 = false;
	}
	if(!(c>=2*s*s))
	{
		cerr<<"Warning: Columns c should be >= rows 2*s*s, "
			<<"or you may not get the correct answer!"<<endl;
	}
	if(!flag) return;

	//Sort the rows' elements, using insertion sort algorithm
	for(int i=0; i<s; i++)
	{
		insertionSort(a[i], c);
	}
	
	//Change elements of row to column
	int n = 0;
	int m = 0;
	T aTemp[ROWS+1][COLUMNS];
	for(int i=0; i<s; i++)
		for(int j=0; j<c; j++)
		{
			if(n<s)
			{
				aTemp[n][m] = a[i][j];
				n++;
			}			//或者也可以把j++放到这里面,n++对应到j++
			else
			{
				n=0;
				m++;
				j--;		//非常容易忽略的地方,一不留神就没有考虑到要倒退一个数字了。
				//一定要在脑子多走几次比较新的逻辑,否则非常容易忽略细节的地方,导致出错。
				//会浪费很多时间的。
			}		
		}

	//Sort the aTemp rows' elements, using insertion sort algorithm
	for(int i=0; i<s; i++)
	{
		insertionSort(aTemp[i], c);
	}

	//Change elements of row to column
	n = 0;
	m = 0;
	for(int j=0; j<c; j++)
		for(int i=0; i<s; )
		{
			if(m<c)
			{
				a[n][m] = aTemp[i][j];
				m++;
				i++;
			}//把i和m放在同一个{}里面,这样保证两个下标同步,否则会出现掉值
			else
			{
				m=0;
				n++;
			}		
		}

	//Sort the rows' elements, using insertion sort algorithm
	for(int i=0; i<s; i++)
	{
		insertionSort(a[i], c);
	}

	for(int i=0; i<s+1; i++)
		for(int j=0; j<c; j++)
			aTemp[i][j] = 0;	//Supposed 0 is our minimum value. 
							//You can chenge to minus unlimit if need arise;

	//move every elements back to half of columns position
	int tempIndex = 0;
	int halfC = c/2;
	for(int i=0; i<s; i++)
		for(int j=0; j<c; j++)
		{
			tempIndex = i*c+j+halfC;
			n = tempIndex/c;
			m = tempIndex%c;
			aTemp[n][m] = a[i][j];
		}

	//Sort the rows' elements
	for(int i=0; i<s+1; i++)
	{
		insertionSort(aTemp[i], c);
	}

	//move every elements half of columns position
	for(int i=0; i<s; i++)
		for(int j=0; j<c; j++)
		{
			tempIndex = i*c+j+halfC;
			n = tempIndex/c;
			m = tempIndex%c;
			a[i][j] = aTemp[n][m];
		}
	for(int i=halfC; i<c; i++)
		a[s-1][i] = aTemp[s][i];
}

void test()
{
	//初始化数组
	double a[ROWS][COLUMNS] = 
	{{32., 12., 0.7, 5., 0.1, 0.7, 0.8,0.7, 99., 0.4, 1., 2.5, 3.6, 5., 9., 12., 19.,953},
	 {0.2, 0.8, 4., 8., 2., 0.8, 56.,0.3, 0.8, 0.4, 8., 7.5, 3.9, 5., 111., 52., 19.,39},
	 {0.25, 0.3, 2., 9., 0.2, 0.78, 0.6,0.37, 0.83, 0.3, 8., 6.5, 9.6, 5., 181., 72., 19.,351}};

	//排序前
	for(int i=0; i<ROWS; i++)
	{
		for(int j=0; j<COLUMNS; j++)
		{
			cout<<a[i][j]<<"\t";
		}
	}
	cout<<endl<<endl;

	//调用排序函数
	rowSort(a);	
	
	//排序后
	for(int i=0; i<ROWS; i++)
	{
		for(int j=0; j<COLUMNS; j++)
		{
			cout<<a[i][j]<<"\t";
		}
	}
	cout<<endl;

}

int main()
{
	test();
	return 0;
}

—————————————————————————————————

本文原创自Sliencecsdn技术博客。

本博客所有原创文章请以链接形式注明出处。

欢迎关注本技术博客,本博客的文章会不定期更新。


大多数人想要改造这个世界,但却罕有人想改造自己。

世上没有绝望的处境,只有对处境绝望的人。

                                              ————By slience

—————————————————————————————————



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值