Question:A file that contains up to n positive integers, each of which is smallerN, WhereN = 10 ^ 7,All positive integers are unique. HowNIntegers are listed in ascending order.
Constraints:Maximum1 MBMemory space is available, with sufficient disk storage space.
Exercise 2 Exercise 3 implement bitvectors for sorting.
#include<stdio.h>#define N 10000000#define Shift 5#define BitPerWord (sizeof(int)*8)#define Mask ((1<<Shift)-1)int a[1+N/BitPerWord];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 main(){ freopen("data.in","r",stdin); freopen("data.out","w",stdout); for(int i=0;i<N;i++) clr(i); int a; while(scanf("%d",&a)!=EOF) set(a); for(int i=0;i<N;i++){ if(test(i)) printf("%d ",i); } printf("\n"); return 0;}
Exercise 4: Return K random integers in the ascending order between 0 and n-1.
for i = [0,n) x[i]=ifor i = [0,k) swap(i,rand(i,n-1)) print(x[i])
Chapter 1 of Programming