排序算法:希尔排序_代码
class Myset {
private:
int* arr;
int size;
public:
Myset() {
arr = new int[1]();
size = 0;
cout << "create!" << endl;
}
~Myset() {
if (arr) {
delete[]arr;
cout << "delete" << endl;
}
else {
cout << "exit!" << endl;
}
}
int getsize() {
return this->size;
}
int* getarr() {
return this->arr;
}
void sort_xier_ver_one();
void print();
bool adds(int i);
}
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 Myset::sort_xier_ver_one() {
for (int d = size / 2; d >= 1; d = d / 2) {
for (int i = d; i < size; i++) {
int a = arr[i];
int j = i - d;
while (j >= 0 && arr[j] < a) {
arr[j + d] = arr[j];
j = j - d;
}
arr[j + d] = a;
}
}
}
|