二维数组排列

 功能:排列一个m行n列 大小的数组
 输入: int * pArray 指向数组第一个元素的指针,m为行数, n为列数 ,请按第i列排列
 输出:按第i列排序完成后的数组放到入参指定的地址中 (i的取值范围 1 - n)  
 返回:
 功能:排列一个m行n列 大小的数组
 输入: int * pArray 指向数组第一个元素的指针,m为行数, n为列数 ,请按第i列排列
 输出:按第i列排序完成后的数组放到入参指定的地址中    (i的取值范围 1 - n)  

 返回:空

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
</pre><pre name="code" class="cpp">void RangeArray(int * pArray,unsigned int  m, unsigned int  n,unsigned int  i)
{   
	if( pArray == NULL || m<0 || n<0 || i>n )
		return;
	int * tempArray=new  int[n];
	//从最后一列开始排序,排序到指定列
	for(unsigned int column=n-1;column>=i-1;column--)
	{
		//对每一列进行排序,冒泡排序的变形
		for(unsigned int iIn=0;iIn<m-1;iIn++)
		{ 
			for(unsigned int j=0;j<m-iIn-1;j++)
			{ 
				if (*(pArray+(j+1)*n+column) < *(pArray+(j)*n+column))
				{
					memcpy(tempArray,pArray+j*n,n*sizeof(int));
					memcpy(pArray+j*n,pArray+(j+1)*n,n*sizeof(int));
					memcpy(pArray+(j+1)*n,tempArray,n*sizeof(int));
				}
			}
		}
		if(column==0)
		{break;}
	}	
}

int main()
{
	int array[] =
	{
		1,3,2,
		2,2,4,
		2,3,1,
		1,3,1
	};
	int m = 4;
	int n = 3;
	int i = 2;
	RangeArray(array,m,n,i);
	return 0;
}

整理自: 点击打开链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值