// partition tree at node with position pos (counting from 0) in the // sorted sequence of all items, node become new root node. link partitionR (link currentTree, int pos) { int leftSubtreeSize = currentTree->left->size; if (leftSubtreeSize > pos) { currentTree->left = partitionR (currentTree->left, pos); currentTree = rotRight (currentTree) ; } else if (leftSubtreeSize < pos) { currentTree->right = partitionR (currentTree->right, pos - 1 - leftSubtreeSize); currentTree = rotLeft (currentTree) ; } return currentTree; }
float racoR(Element x[SIZE][SIZE],int n,int k[2*SIZE],int t,int arr[2],float best,int fl) { float d1,d2,p,q,sum[2] ; int p1[2*SIZE],p2[2*SIZE],i,a,b ; sum[0] = 0; sum[1] = 0; int l[SIZE],m[SIZE],g[2],h[2]; d1 = optTSPr(x,n,k,1,arr,MAX,(fl/20)); d1 = optTSPr(x,n,k,(3*t/5),arr,best,fl); /*for(i=0;i<2*n;i++) d[i]=k[i];*/ if(n>70) { Element A[SIZE][SIZE],B[SIZE][SIZE]; float check =0; int r=0; partitionR(x,n,k,A,B,l,m,g,h,sum); sum[1] += d1 ; printf("\n total = %f , sum[0] = %f ,sum[1] =%f \n",d1,sum[0],sum[1]) ; if(n%2==0) a= n/2; else a = (n/2 + 1); b = (int)(0.65*t) ; p=racoR(A,(n/2),p1,b,g,sum[0],(fl*2)); opt2(A,(n/2),p1,p,g); q=racoR(B,a,p2,b,h,sum[1],(fl*2)); opt2(B,a,p2,q,h); join(x,n,A,B,l,m); printf(" \n values of rec are %f , %f " ,p,q); /* check = p + q ; if(check < (sum[0] + sum[1])) { printf(" check = %f,p = %f q = %f ",check,p,q); for(i=0;i<n;i++) { k[i] = l[p1[i]]; } k[n] = l[p1[n-1]] ; printf(" p1[n-1] = %d k[n-1] =%d",l[p1[n-1]],k[n-1]); k[n+1] = m[p2[0]] ; for(i = n+2;i< 2*n;i++) { k[i] = m[p2[(i-n-2)]]; } d1 = check + x[l[p1[n-1]]][m[p2[0]]].e ; } */ } d2 = optTSPr(x,n,k,(int)(2*t/5),arr,d1,fl); if(d1<d2) { printf(" d1 = %f ",d1) ; return d1; } else { /*for(i=0;i<2*n;i++) k[i]=d[i];*/ return d2; } }
void sortR(int v[], int l, int r){ if(r <= l) return; uniform_int_distribution<> distr(l, r-1); int temp = distr(eng); swap(v[temp], v[r]); //printf("%d %d %d\n", temp, l, r); int mid = partitionR(v, l, r); sortR(v, l, mid - 1); sortR(v, mid + 1, r); }