`
ding43930053
  • 浏览: 38841 次
社区版块
存档分类
最新评论

交换排序法,进行int类型数组升序的排列

 
阅读更多
//交换排序法,进行int类型数组的排列:一(利用常量定义数组长度)
#include <stdafx.h>
#include <iostream> 
#include <stdlib.h>

using namespace std;


#define length = 5
void main()
{
	int a[length] = {58,26,82,16,74};				
	int max ,tempmax=length-1;
	for (int i=length-1;i>=0;i--)
	{
		for (int j=i-0;j>=0;j--)
		{
			if (a[i] <= a[j])
			{
				max = a[j];
				tempmax = j;
			}
		}
		a[tempmax] = a[i];
		a[i] = max;
		
	}
	for(int i=0;i<length;i++)
	{
		cout <<a[i]<<endl;
	}
}
//交换排序法,进行int类型数组的排列:二(动态检测数组的长度)
#include <stdafx.h>
#include <iostream> 
#include <stdlib.h>

using namespace std;


#define length = 5
void main()
{
	int a[] = {58,26,82,16,74};

					

	//动态检测数组的长度
	int length = sizeof(a)/sizeof(*a);

	int max ,tempmax=length-1;

	for (int i=length-1;i>=0;i--)
	{
		for (int j=i-0;j>=0;j--)
		{
			if (a[i] <= a[j])
			{
				max = a[j];
				tempmax = j;
			}
		}
		a[tempmax] = a[i];
		a[i] = max;
		
	}
	for(int i=0;i<length;i++)
	{
		cout <<a[i]<<endl;
	}
}



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics