/*
Name: 数组排序 (冒泡法)
Copyright:
Author:
Date: 14-10-07 15:04
Description:
*/
#include <iostream>
#include <vector>
#include <cstddef> //size_t
using namespace std;
class order
{
public:
void actSmallToBig(int int_arr[],const size_t arr_sz)
{
pa=int_arr;
sz=arr_sz;
for(size_t ix=0; ix!=arr_sz; ++ix)
for(size_t _ix=ix; _ix!=arr_sz; ++_ix)
if(int_arr[ix]>=int_arr[_ix]) //交换过程
{
temp=int_arr[ix];
int_arr[ix]=int_arr[_ix];
int_arr[_ix]=temp;
}
}
void actBigToSmall(int int_arr[],const size_t arr_sz)
{
pa=int_arr;
sz=arr_sz;
for(size_t ix=0; ix!=arr_sz; ++ix)
for(size_t _ix=ix; _ix!=arr_sz; ++_ix)
if(int_arr[ix]<=int_arr[_ix]) //和actSmallToBig不同是“<=“
{
temp=int_arr[ix];
int_arr[ix]=int_arr[_ix];
int_arr[_ix]=temp;
}
}
void show()
{
for(int *pbegin=pa,*pend=pa+sz; pbegin!=pend; ++pbegin)
{
cout<<*pbegin<<' ';
cout.flush();
}
}
order():temp(0) {}
private:
int *pa;
int temp;
size_t sz;
};
int main()
{
const size_t arrSize=10;
int arr[arrSize];
for(int *pbegin=arr,*pend=arr+arrSize; pbegin!=pend; ++pbegin)
{
int val;
cin>>val;
*pbegin=val;
}
order px;
px.actSmallToBig(arr,arrSize);
cout<<"从小到大排序后:"<<endl;
px.show();
cout<<endl;
px.actBigToSmall(arr,arrSize);
cout<<"从大到小排序后:"<<endl;
px.show();
cout<<endl;
system("pause");
return 0;
}
今天看到有人问怎么实现对任意多个数进行排序,这个只要把数组改成vector向量就行了.算法是一样的