#include #include #include // XXX size_t waksmanSwapCount(size_t n) { if(n<=1) return 0; if(n==2) return 1; return n-1+waksmanSwapCount(n/2)+waksmanSwapCount((n+1)/2); } // Creates network. Output array lengths should be at least // waksmanSwapCount(n). Returns this size waksmanSwapCount(n) // If conventions here change, waksmanSwitches must also be changed size_t waksmanNetwork(unsigned a[],unsigned b[],size_t n) { size_t i,j=0,jd; if(n<=1) return 0; for(i=0;i i -> 2*i for i in [0,n/2] jd=waksmanNetwork(a+j,b+j,n/2); for(i=0;i i -> 2*i+1 for i in [0,n/2] // n-1 -> n/2 -> n-1 jd=waksmanNetwork(a+j,b+j,(n+1)/2); for(i=0;i0) if(!set[i]) { j=i; do { unsigned prej=arr[j]; set[j]=true; // go from prej to j through lower box if(prej/2