冒泡排序,你可以参考我这个
void SortBubble(int * array,int size){
for(int i = 0;i<=size-1;i++){
for(int j = size-1;j>i;j--){
if(array[j]<array[j-1])
{
int cup = array[j-1];
array[j-1] = bs[j];
array[j] = cup;
}
}
}
}
#include<iostream>
class Box
{
public:
Box() : Box(0, 0, 0) {}
Box(int l, int w, int h) :length(l), width(w), height(h) {}
bool operator<(const Box& b) const
{
return GetVolume() < b.GetVolume();
}
int GetVolume() const
{
return length * width * height;
}
private:
int length;
int width;
int height;
};
template<typename ElementType>
void SortBubble(ElementType* a, int size)
{
for (int i = 0; i < size - 1; ++i)
{
for(int j = size - 1; j > i; --j)
{
if (a[j] < a[j - 1])
{
std::swap(a[j], a[j - 1]);
}
}
}
}
int main()
{
#define ARR_SIZE 5
int intArr[ARR_SIZE] = { 3, 7, 1, 5, 6 };
SortBubble(intArr, ARR_SIZE);
Box boxArr[ARR_SIZE] = {
Box(1, 3, 8),
Box(9, 7, 6),
Box(1, 1, 1),
Box(10, 10, 10),
Box(5, 5, 5)
};
SortBubble(boxArr, ARR_SIZE);
}