C++排序算法:冒泡排序_代码
template< class T>
void swaps(T& a, T& b) {
T temp = a;
a = b;
b = temp;
return;
}
class Myset {
private:
int* arr;
int size;
public:
Myset() {
arr = new int[1]();
size = 0;
}
~Myset() {
if (arr) {
delete[]arr;
}
}
int getsize() {
return this->size;
}
int* getarr() {
return this->arr;
}
bool adds(int i);
void sort_bubble_ver_one();
}
bool Myset::adds(int i) {
if (size == 0) {
arr[0] = i;
size++;
return true;
}
else {
int* prr = new int[size + 1];
for (int i = 0; i < size; i++) {
prr[i] = arr[i];
}
prr[size] = i;
delete[]arr;
arr = prr;
size++;
return true;
}
}
声明:
void sort_bubble_ver_one();
冒泡排序—的---具体定义:
void Myset::sort_bubble_ver_one() {
int i = 0;
int j = 0;
for (i = 1; i < this->size; i++) {
for (j = this->size - 1; j > i - 1; j--) {
if (this->arr[j] < this->arr[j - 1]) {
swaps(this->arr[j], this->arr[j - 1]);
}
else {
continue;
}
}
}
return;
}
运行结果:
|