实现排序输出
#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
请按任意键继续. . .