您的位置:首页 > 其它

实现数组插入排序并输出

2015-02-01 22:01 344 查看
实现排序输出

#include<iostream>
#include<cstdlib>
using namespace std;

class intarray
{
	int *p;//指针指向首地址
	int N;//确定有多少元素
	
public: 
	intarray(int size)
	{
		N = size;//接受多少个元素
		p = new int
;
;	}

	void setnum(int id, int x)
	{
		*(p+id) = x; 
	}

	void  sort()
	{
		for (int i=0;i<N;i++)
		{
			for (int j=0;j<N-i-1;j++)
			{
				if (p[j]>p[j+1])
				{
					int temp = p[j];
					p[j] = p[j+1];
					p[j+1] = temp;
				}
			}
		}
	}
	void printfAll()
	{
		cout<<endl;
		for (int *pa = p;pa<p+N;pa++)
		{
			cout<<*pa<<endl;
		}
	}
};
int main()
{
	intarray b(10);
	for (int i=0;i<10;i++)
	{
		b.setnum(i,rand()%999);
	}
	b.printfAll();
	b.sort();
	b.printfAll();
	system("pause ");
	return 0;
}
输出结果:

41

485

340

526

188

739

489

387

988

488

41

188

340

387

485

488

489

526

739

988

请按任意键继续. . .
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐