#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using std::cout;
using std::endl;
const int n = 80000;
// bubblesort
void bubble(int * a, int n) {
for (int i = 0; i < n-1; i++) {
for (int j = n-1; j > i; j--) {
if (a[j] < a[j-1]) {
int temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
}
}
}
}
// insertionsort
void insert(int *a, int n) {
for (int i = 1; i < n; i++) {
int temp = a[i];
while (i > 0 && a[i-1] > temp) {
a[i] = a[i-1];
i--;
}
a[i] = temp;
}
}
// mergesort
void combine(int *a, int p, int q, int r) {
int i = p, j = q+1;
int *temp = new int[r - p + 1];
for (int k = 0; k <= r - p; k++) {
if (j <= r && (a[i] >= a[j] || i > q)) {
temp[k] = a[j];
j++;
} else {
temp[k] = a[i];
i++;
}
}
for (int k = p; k <= r; k++) {
a[k] = temp[k - p];
}
delete temp;
}
void merge(int *a, int p, int q) {
if (p >= q) return;
merge(a, p, (p+q)/2);
merge(a, (p+q)/2+1, q);
combine(a, p, (p+q)/2, q);
}
// quicksort
void qsort(int * a, int left, int right) {
if (left >= right) return;
int i = left, j = right, k = a[left];
while (i < j) {
while (i < j && a[j] >= k) {
j--;
}
a[i] = a[j];
while (i < j && a[i] <= k) {
i++;
}
a[j] = a[i];
}
a[i] = k;
qsort(a, left, i-1);
qsort(a, i+1, right);
}
// heapsort
#define left(i) (i << 1) + 1
#define right(i) (i + 1) << 1
void max_heapfly(int * a, int i, const int & size) {
int lagest, l = left(i), r = right(i);
if (l < size && a[l] > a[i]) {
lagest = l;
} else {
lagest = i;
}
if (r < size && a[r] > a[lagest]) {
lagest = r;
}
if (lagest != i) {
int temp = a[i];
a[i] = a[lagest];
a[lagest] = temp;
max_heapfly(a, lagest, size);
}
}
void build_max_heap(int * a, int n) {
for (int i = n/2-1; i >= 0; i--) {
max_heapfly(a, i, n);
}
}
void heapsort(int * a, int n) {
build_max_heap(a, n);
for (int i = n-1; i > 0; i--) {
int temp = a[0];
a[0] = a[i];
a[i] = temp;
max_heapfly(a, 0, i);
}
}
int main() {
// test bubble
if (true) {
cout << "test bubble\n";
int a[n];
srand(time(NULL));
for (int i = 0; i < n; i++) a[i] = rand();
clock_t start = clock();
bubble(a, n);
clock_t end = clock();
// for (int i = 0; i < n; i++) cout << a[i] << " ";
double time = (double)(end - start) / CLOCKS_PER_SEC;
cout << endl << time << endl << endl;
}
// test insert
if (true) {
cout << "test insert\n";
int a[n];
srand(time(NULL));
for (int i = 0; i < n; i++) a[i] = rand();
clock_t start = clock();
insert(a, n);
clock_t end = clock();
// for (int i = 0; i < n; i++) cout << a[i] << " ";
double time = (double)(end - start) / CLOCKS_PER_SEC;
cout << endl << time << endl << endl;
}
// test merge
if (true) {
cout << "test merge\n";
int a[n];
srand(time(NULL));
for (int i = 0; i < n; i++) a[i] = rand();
clock_t start = clock();
merge(a, 0, n-1);
clock_t end = clock();
// for (int i = 0; i < n; i++) cout << a[i] << " ";
double time = (double)(end - start) / CLOCKS_PER_SEC;
cout << endl << time << endl << endl;
}
// test qsort
if (true) {
cout << "test qsort\n";
int a[n];
srand(time(NULL));
for (int i = 0; i < n; i++) a[i] = rand();
clock_t start = clock();
qsort(a, 0, n-1);
clock_t end = clock();
// for (int i = 0; i < n; i++) cout << a[i] << " ";
double time = (double)(end - start) / CLOCKS_PER_SEC;
cout << endl << time << endl << endl;
}
// test heapsort
if (true) {
cout << "test heapsort\n";
int a[n];
srand(time(NULL));
for (int i = 0; i < n; i++) a[i] = rand();
clock_t start = clock();
heapsort(a, n);
clock_t end = clock();
// for (int i = 0; i < n; i++) cout << a[i] << " ";
double time = (double)(end - start) / CLOCKS_PER_SEC;
cout << endl << time << endl << endl;
}
return 0;
}
Bubble sort, Insertion sort, merge sort, quick sort, heap sort
最新推荐文章于 2021-12-25 15:10:42 发布