实现数组插入排序并输出

实现排序输出

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

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


	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
请按任意键继续. . .


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值