#include<cstdio>
#include<ctime>
#include<windows.h>
#include<cstring>
#include<algorithm>
#include<queue>
#define print(ovo, a, b) for(int qwq = (a); qwq <= (b); qwq++) {write((ovo)[qwq]); putchar(' ');} putchar('\n');
#define FOR(a, b, c) for(register int (a) = (b); (a) <= (c); ++(a))
#define UFOR(a, b, c) for(register int (a) = (b); (a) >= (c); --(a))
const int P = 5, MAXN = 10005, begin = 1, end = 10000, low = 1, high = 99999;
int isdigit[300], bucket[100000], t[MAXN], ci[MAXN];
using namespace std;
void read(int &x)
{
int f = 1; x = 0; char s = getchar();
while(!isdigit[s]) {if(s == '-') f = -1; s = getchar();}
while(isdigit[s]) {x = x*10 + s-'0'; s = getchar();}
x *= f;
}
void bubble_sort(int arr[], int fr, int to); //冒泡排序
void mark_bubble_sort(int arr[], int fr, int to); //带标志的冒泡排序
void periphery_bubble_sort(int arr[], int fr, int to); //带边界的冒泡排序
void double_end_bubble_sort(int arr[], int fr, int to); //双端冒泡排序
void select_sort(int arr[], int fr, int to); //选择排序
void double_end_select_sort(int arr[], int fr, int to); //选择排序优化
void shell_sort(int arr[], int fr, int to); //希尔排序
void straight_insert_sort(int arr[], int fr, int to); //直接插入排序
void binary_insert_sort(int arr[], int fr, int to); //二分插入排序
void merge_sort(int arr[], int fr, int to); //归并排序
void count_sort(int arr[], int fr, int to); //计数排序
void radix_sort(int arr[], int fr, int to); //基数排序
void priority_sort(int arr[], int fr, int to); //优先队列排序
void hand_to_heap_sort(int arr[], int fr, int to); //手打堆排序
void std_sort(int arr[], int fr, int to); //std
void work(int k)
{
if(k == 1) bubble_sort(t, begin, end);
if(k == 2) mark_bubble_sort(t, begin, end);
if(k == 3) periphery_bubble_sort(t, begin, end);
if(k == 4) double_end_bubble_sort(t, begin, end);
if(k == 5) select_sort(t, begin, end);
if(k == 6) double_end_select_sort(t, begin, end);
if(k == 7) straight_insert_sort(t, begin, end);
if(k == 8) binary_insert_sort(t, begin, end);
if(k == 9) merge_sort(t, begin, end);
if(k == 10) count_sort(t, begin, end);
if(k == 11) radix_sort(t, begin, end);
if(k == 12) priority_sort(t, begin, end);
if(k == 13) hand_to_heap_sort(t, begin, end);
if(k == 14) std_sort(t, begin, end);
}
void write(int x)
{
if(x < 0) {putchar('-'); x = -x;}
if(x > 9) write(x/10);
putchar((x % 10) + '0');
}
double clocker(int k)
{
FOR(i, begin, end) t[i] = ci[i];
LARGE_INTEGER t1, t2, tc;
QueryPerformanceFrequency(&tc);
QueryPerformanceCounter(&t1);
work(k);
QueryPerformanceCounter(&t2);
//print(t, begin, end);
return (t2.QuadPart - t1.QuadPart)*1.0/tc.QuadPart;
}
int main()
{
for(int i = '0'; i <= '9'; i++) isdigit[i] = 1;
freopen("test.txt", "r", stdin);
FOR(i, begin, end) read(ci[i]);
printf("bubble_sort: %lf\n", clocker(1));
printf("mark_bubble_sort: %lf\n", clocker(2));
printf("periphery_bubble_sort: %lf\n", clocker(3));
printf("double_end_bubble_sort: %lf\n", clocker(4));
printf("select_sort: %lf\n", clocker(5));
printf("double_end_select_sort: %lf\n", clocker(6));
printf("straight_insert_sort: %lf\n", clocker(7));
printf("binary_insert_sort: %lf\n", clocker(8));
printf("merge_sort: %lf\n", clocker(9));
printf("count_sort: %lf\n", clocker(10));
printf("radix_sort: %lf\n", clocker(11));
printf("priority_sort: %lf\n", clocker(12));
printf("hand_to_heap_sort: %lf\n", clocker(13));
printf("std_sort: %lf\n", clocker(14));
}
void bubble_sort(int arr[], int fr, int to) //基础冒泡排序
{
FOR(i, 1, to-fr)
FOR(j, fr, to-i)
if(arr[j] > arr[j+1]) swap(arr[j], arr[j+1]);
}
void mark_bubble_sort(int arr[], int fr, int to) //带标志位的冒泡排序
{
int mark = 1;
for(register int i = 1; i <= to-fr && mark; i++)
{
mark = 0;
FOR(j, fr, to-i)
if(arr[j] > arr[j+1]) {swap(arr[j], arr[j+1]); mark = 1;}
}
}
void periphery_bubble_sort(int arr[], int fr, int to) //带边界的冒泡排序
{
register int len = to-fr, confine = 1;
while(confine > 0)
{
confine = 0;
FOR(i, fr+1, fr+len)
if (arr[i-1] > arr[i]) {swap(arr[i], arr[i-1]); confine = i;}
len = confine-fr-1;
}
}
void double_end_bubble_sort(int arr[], int fr, int to) //双端冒泡排序
{
register int l = fr, r = to;
while(l < r)
{
FOR(j, l, r-1)
if(arr[j] > arr[j+1]) swap(arr[j], arr[j+1]);
r--;
UFOR(j, r, l+1)
if(arr[j-1] > arr[j]) swap(arr[j-1], arr[j]);
l++;
}
}
void select_sort(int arr[], int fr, int to)
{
register int x;
FOR(i, fr, to-1)
{
x = i;
FOR(j, i+1, to) if(arr[x] > arr[j]) x = j;
swap(arr[i], arr[x]);
}
}
void double_end_select_sort(int arr[], int fr, int to)
{
int l = fr, r = to, min = l, max = l;
while(l <= r)
{
min = l;
max = l;
FOR(i, l, r)
{
if(arr[i] < arr[min]) min = i;
if(arr[i] > arr[max]) max = i;
}
swap(arr[l], arr[min]);
if(l == max) max = min;
swap(arr[r], arr[max]);
l++, r--;
}
}
void shell_sort(int arr[], int fr, int to)
{
}
void straight_insert_sort(int arr[], int fr, int to)
{
FOR(i, fr+1, to)
{
int x = arr[i];
UFOR(j, i, fr)
if(x < arr[j-1] && j-1 >= fr) arr[j] = arr[j-1];
else {arr[j] = x; break;}
}
}
void binary_insert_sort(int arr[], int fr, int to)
{
FOR(i, fr+1, to)
{
int l = fr, r = i-1, mid, x = i;
while(l <= r)
{
mid = (l+r)>>1;
if(arr[i] < arr[mid]) x = mid, r = mid - 1;
else l = mid + 1;
}
mid = arr[i];
UFOR(j, i, x+1) arr[j] = arr[j-1];
arr[x] = mid;
}
}
void merge_sort(int arr[], int fr, int to)
{
if(fr == to) return;
int mid = (fr + to) >> 1;
merge_sort(arr, fr, mid);
merge_sort(arr, mid+1, to);
int tmp[to-fr+5], i = fr, j = mid+1, curTmp = 0;
while(i <= mid && j <= to)
{
if(arr[i] > arr[j]) tmp[curTmp++] = arr[j++];
else tmp[curTmp++] = arr[i++];
}
if(i <= mid) while(i <= mid){tmp[curTmp++] = arr[i]; i++;}
if(j <= to) while(j <= to) {tmp[curTmp++] = arr[j]; j++;}
curTmp = 0;
for(int i = fr; i <= to; i++) arr[i] = tmp[curTmp++];
}
void count_sort(int arr[], int fr, int to)
{
memset(bucket, 0, sizeof(bucket));
register int op = fr-1;
FOR(i, fr, to) bucket[arr[i]]++;
FOR(i, low, high) if(bucket[i]) {arr[++op] = i; bucket[i]--; i--;}
}
void radix_sort(int arr[], int fr, int to)
{
int qwq[] = {1, 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
const int radix = 12;
int count[radix], j;
memset(bucket, 0, sizeof(bucket));
FOR(k, 1, P)
{
FOR(i, 0, radix-1) count[i] = 0;
FOR(i, fr, to) count[((arr[i] / qwq[k]) % 10)]++;
FOR(i, 1, radix-1) count[i] = count[i] + count[i-1];
UFOR(i, to, fr)
{
j = (arr[i] / qwq[k]) % 10;
bucket[count[j]-- - 1] = arr[i];
}
for(int i = fr, j = 0; i <= to; ++i, ++j) arr[i] = bucket[j];
}
}
void priority_sort(int arr[], int fr, int to)
{
priority_queue<int, vector<int>, greater<int> > q;
FOR(i, fr, to) q.push(arr[i]);
FOR(i, fr, to) {arr[i] = q.top(); q.pop();}
}
void hand_to_heap_sort(int arr[], int fr, int to)
{
struct Heap
{
int heap[MAXN], len;
Heap() {len = 0;}
void Down(int p)
{
int key = heap[p];
while(p*2 <= len)
{
int son = 2*p;
if(son+1 <= len && heap[son+1] < heap[son]) son++;
if(heap[son] >= key) break;
heap[p] = heap[son]; p = son;
}
heap[p] = key;
}
int top() {return heap[1];}
void pop(){heap[1] = heap[len--]; Down(1);}
void push(int p)
{
heap[++len] = p;
int x = len;
while(x >= 2)
{
if(heap[x] >= heap[x>>1]) break;
if(heap[x] < heap[x>>1]) swap(heap[x], heap[x>>1]), x >>= 1;
}
}
}q;
FOR(i, fr, to) q.push(arr[i]);
FOR(i, fr, to) {arr[i] = q.top(); q.pop();}
}
void std_sort(int arr[], int fr, int to)
{
sort(arr+fr, arr+to);
}
//随机数发生器
#include<cstdio>
#include<ctime>
#include<cstdlib>
#define random1(a, b) rand() % ((b)-(a)+1)+(a)
#define random2(a, b) xorshf96() % ((b)-(a)+1)+(a)
const unsigned long begin = 1, end = 10000, low = 1, high = 99999;
static unsigned long x = 123456789, y = 362436069, z = 521288629;
unsigned long xorshf96(void)
{
unsigned long t;
x ^= x << 16, x ^= x >> 5, x ^= x << 1;
t = x, x = y, y = z;
z = t ^ x ^ y;
return z;
}
void write(int x)
{
if(x < 0) {putchar('-'); x = -x;}
if(x > 9) write(x/10);
putchar((x % 10) + '0');
}
int main()
{
freopen("test.txt", "w", stdout);
srand((unsigned) time(NULL));
for(int i = begin; i <= end; i++) {write(random1(low, high)); putchar('\n');}
}
耗时