1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h>
#define BITSPERWORD 32 #define SHIFT 5 #define MASK 0x1F #define N 10000000
int a[1 + N/BITSPERWORD]; int x[N];
void set(int i); void clr(int i); int test(int i); int randint(int l, int u); void swap(int *a, int *b); void generate_rand_num(); void bitmap_sort();
int main(int argc, char *argv[]) { bitmap_sort(); }
void set(int i) { a[i>>SHIFT] |= (1<<(i & MASK)); }
void clr(int i) { a[i>>SHIFT] &= ~(1<<(i & MASK)); }
int test(int i) { return a[i>>SHIFT] & (1<<(i & MASK)); }
int randint(int l, int u) { int temp; srand((unsigned)time(NULL)); temp = floor(1 + (1.0*rand()/RAND_MAX)*(u - l + 1)); return temp; }
void swap(int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; }
void generate_rand_num() { FILE *fp; int k = N; int i; fp = fopen("in.txt", "w");
for (i = 0; i < N; i++){ x[i] = i + 1; } for(i = 0; i < k; i++){ swap(&x[i], &x[randint(i, N-1)]); fprintf(fp,"%d\n",x[i]); } fclose(fp); }
void bitmap_sort() { int i; FILE *in, *out; int num; in = fopen("in.txt", "r"); out = fopen("out.txt", "w");
generate_rand_num();
for(i = 0; i < N; i++){ clr(i); } while(fscanf(in, "%d", &num) != EOF){ set(num); } for(i = 0; i < N; i++){ if(test(i)){ fprintf(out, "%d\n", i); } } fclose(in); fclose(out); }
|