对快速排序,归并排序,希尔排序的时间效率进行了一个测试。
归并排序果然厉害,我的希尔排序写的估计有点问题,太慢。
using namespace std;
//quick sort
int partition(int *data, int first, int last) {
int p = first;
int pivot = data[first];
while (first <= last) { // 细节1
if (data[first] <= pivot) {
++first;
}
else if (data[last] >= pivot) {
--last;
}
else {
swap(data[first], data[last]); //
}
}
swap(data[p], data[last]);
return last;
}
void quick_sort(int *data, int first, int last, int *tmp) {
if (first >= last) {
return;
}
else {
int index = partition(data, first, last);
quick_sort(data, first, index - 1, tmp);
quick_sort(data, index + 1, last, tmp);
}
}
// bubble sort
void bubble_sort(int *data, int first, int last) {
for (int t = 0; t < last - first; ++t) {
for (int i = 0; i < last - t; ++i) {
if (data[i] > data[i + 1]) {
swap(data[i], data[i + 1]);
}
}
}
}
//merge sort
void merge_data(int *data, int first, int mid, int last, int *tmp) {
int p1 = first; int p2 = mid + 1; int index = 0;
while (p1 <= mid && p2 <= last) {
if (data[p1] < data[p2]) {
tmp[index++] = data[p1++];
}
else {
tmp[index++] = data[p2++];
}
}
while (p1 <= mid) {
tmp[index++] = data[p1++];
}
while (p2 <= last) {
tmp[index++] = data[p2++];
}
// 把tmp还给data
for (int i = first; i <= last; ++i) {
data[i] = tmp[i - first];
}
}
void merge_sort(int *data, int first, int last, int *tmp) {
if (first >= last) {
return;
}
else {
//int *tmp = new int[last - first + 1];
int mid = first + (last - first) / 2;
merge_sort(data, first, mid, tmp);
merge_sort(data, mid + 1, last, tmp);
merge_data(data, first, mid, last, tmp);
}
}
//select sort
void select_sort(int *data, int first, int last) {
for (int i = first; i <= last - 1; ++i) {
int min = i;
for (int k = i + 1; k <= last; ++k) {
if (data[k] < data[min]) {
min = k;
}
}
swap(data[i], data[min]);
}
}
// insert sort
void insert_sort(int *data, int first, int last) {
for (int i = first + 1; i <= last; ++i) {
for (int k = i; k >= first + 1; --k) {
if (data[k] < data[k - 1]) {
swap(data[k], data[k - 1]);
}
else {
break;
}
}
}
}
// shell sort
void shell_sort(int *data, int first, int last, int *tmp) {
int N = last - first + 1;
//三层循环,有点儿小复杂
for (int gap = N / 3; gap >= 1; gap /= 2) {
//
for (int i = first + gap; i <= last; ++i) {
//
for (int k = i; k >= first + gap; k -= gap) {
if (data[k] < data[k - gap]) {
swap(data[k], data[k - gap]);
}
else {
break;
}
}
}
}
}
int main() {
int base = 2; int N = 25; int len = 0;
void(*sort[])(int *, int, int, int *) = {
quick_sort,
merge_sort,
shell_sort
//select_sort,
//insert_sort,
//bubble_sort
};
ofstream log("data.txt");
for (int i = 15; i <= N; ++i) {
log << i << endl;
len = pow(base, i);
int *data = new int[len];
int *tmp = new int[len];
for_each(data, data + len, [](int &i) -> void{ i = rand(); });
for (int i = 0; i < sizeof(sort) / sizeof(sort[0]); ++i) {
random_shuffle(data, data + len);
clock_t start = clock();
sort[i](data, 0, len - 1, tmp);
clock_t end = clock();
if (!is_sorted(data, data + len)) {
log << "error\n";
}
else {
log << double(end - start) / CLOCKS_PER_SEC << endl;
}
}
delete[] data;
delete[] tmp;
log << "\n\n\n";
}
log.close();
return 0;
}