real median_wirth(int n, real *x) { if (n&1) return kth_smallest(n, x, n/2); /* odd, picks the middle */ else return kth_smallest(n, x, n/2 - 1); /* even, a slight cheat */ /* never reached */ return x[0]; }
int kth_smallest(tree *root, int* k) { if (!root || (k && *k < 0)) return (-1); int rl = kth_smallest(root->left, k); *k = *k - 1; if (*k == 0) { return (root->data); } int rr = kth_smallest(root->right, k); return ((rl > rr) ? rl : rr); }
void kth_smallest(struct node* root,int k) { if(root->l==k-1) printf("%d ",root->data); else if(root->l>=k) { kth_smallest(root->left,k); } else { kth_smallest(root->right,k-root->l-1); } }
SEXP kthSmallest(SEXP I_SortVector, SEXP I_obs, SEXP I_rank) { SEXP ret; long obs, rank, i; double *SortVector; double ReturnElement; obs = asInteger(I_obs); rank = asInteger(I_rank)-1; SortVector = (double *) malloc(obs*sizeof(double)); for (i=0; i<obs; i++) { SortVector[i] = REAL(I_SortVector)[i]; // Rprintf("SortVector[%d]: %lf\n", i+1, SortVector[i]); }// end of i loop ReturnElement = kth_smallest(SortVector, obs, rank); free(SortVector); PROTECT(ret=allocVector(REALSXP,1)); REAL(ret)[0]=ReturnElement; UNPROTECT(1); return(ret); } //end of kthSmallest
int main(int argc, char **argv) { if (argc < 3) { printf("usage: %s <N> <M> [k]\n", argv[0]); return 0; } srand(time(NULL)); int N = atoi(argv[1]); int M = atoi(argv[2]); int k = 2; if (argc > 3) { k = atoi(argv[3]); if (!k) k = 2; } int * a = generate_array(N, M); shuffle_array(a, M); print_array(a, M); int small = kth_smallest(a, M, k); printf("%d smallest: %d\n", k, small); return 0; }
/* * Compute and save gain calibration */ void saveGaincal(cGlobal *global, int detIndex) { //printf("Processing gaincal\n"); // Dereference common variables cPixelDetectorCommon *detector = &(global->detector[detIndex]); long pix_nn = detector->pix_nn; // Grab a snapshot of the current running sum pthread_mutex_lock(&detector->powderData_mutex[0]); float *buffer = (float*) calloc(pix_nn, sizeof(float)); for(long i=0; i<pix_nn; i++) buffer[i] = detector->powderData_raw[0][i]; for(long i=0; i<pix_nn; i++) buffer[i] /= detector->nPowderFrames[0]; pthread_mutex_unlock(&detector->powderData_mutex[0]); // Find median value (this value will become gain=1) float dc; float *buffer2 = (float*) calloc(pix_nn, sizeof(float)); for(long i=0; i<pix_nn; i++) { buffer2[i] = buffer[i]; } dc = kth_smallest(buffer2, pix_nn, lrint(0.5*pix_nn)); printf("offset=%f\n",dc); free(buffer2); // Trap a potential error condition if(dc <= 0){ printf("Error calculating gain, offset = %f\n",dc); return; } // Calculate gain // gain=1 for a median value pixel // Possibly bound gain between 0.1 and 10 for(long i=0; i<pix_nn; i++) { buffer[i] /= (float) dc; //if(buffer[i] < 0.1 || buffer[i] > 10) // buffer[i]=0; } char filename[1024]; sprintf(filename,"r%04u-%s-gaincal.h5",global->runNumber, detector->detectorName); //printf("Saving gaincal to file: %s\n", filename); printf("%s\n", filename); #ifdef H5F_ACC_SWMR_WRITE pthread_mutex_lock(&global->swmr_mutex); #endif writeSimpleHDF5(filename, buffer, detector->pix_nx, detector->pix_ny, H5T_NATIVE_FLOAT); #ifdef H5F_ACC_SWMR_WRITE pthread_mutex_unlock(&global->swmr_mutex); #endif free(buffer); }
double lqd2(double **SresRaw, long nobs, long nvars_unique, long ncats) { double fit_lqd2, *rawres, *dif; long h, diflen, hidx, total_obs, i, ii, j, k, count; total_obs = nobs*(ncats-1); h = (long) (total_obs+nvars_unique+1)/2; diflen = (total_obs*(total_obs-1))/2; hidx = (h*(h-1))/2; rawres = (double *) calloc(total_obs, sizeof(double)); dif = (double *) calloc(diflen,sizeof(double)); count = 0; for (i=0; i<nobs; i++) { for (j=0; j<(ncats-1); j++) { rawres[count] = SresRaw[i][j]; //Rprintf("rawres[%d] %lf, SresRaw[%d][%d]: %lf\n", count, rawres[count], i, j, SresRaw[i][j]); count++; } // end of j } // end of i for (i=2; i<=total_obs; i++) { ii = ((i-1)*(i-2))/2; for(j=1; j<=(i-1); j++) { k = ii+j; dif[k-1] = fabs(rawres[i-1]-rawres[j-1]); } } // end of i /* qsort(dif, diflen, sizeof(double), (int (*)(const void *, const void *)) bcacmp); fit_lqd2 = dif[hidx-1] * 2.21914446599; */ fit_lqd2 = kth_smallest(dif, diflen, hidx-1) * 2.21914446599; if (!R_finite(fit_lqd2)) { /* Rprintf("XXX\n"); */ fit_lqd2 = 999991234; } //free memory free(dif); free(rawres); return(fit_lqd2); } // end of lqd2
int kth_largest(tree *root, int k) { if (root == NULL || k < 0) return (-1); int size = tree_size(root); if (k > size) return (-1); int nk = size - k; return (kth_smallest(root, &nk)); }
/*--------------------------------------------------------------------------- // Function median: // compute median // Input float array t[] of length n // computation in situ, t will be reordered ---------------------------------------------------------------------------*/ float median(float t[],int n) { float q,q2; if (n%2 == 1) //odd { q = kth_smallest(t,n,(n+1)/2); return q; } /* else even: */ /* q = kth_smallest(t,n,n/2); q2 = kth_smallest(t,n,n/2 + 1); */ q = quick_select(t,n,n/2); q2 = quick_select(t,n,n/2 + 1); return 0.5 * (q + q2); }
// quick select // median num int majorityElement(vector<int>& num) { return kth_smallest(num, num.size() / 2 + 1); // k based 1 // > n / 2 }
int main() { int n; scanf("%d",&n); int i; struct node* root=NULL; for(i=0;i<n;i++) { int a; scanf("%d",&a); insert(&root,a,NULL); } inorder(root); printf("\n"); preorder(root); printf("\n"); postorder(root); printf("\n"); int k; scanf("%d",&k); kth_smallest(root,k); /* int search; scanf("%d",&search); ser(root,search);*/ /* int arr[n]; for(i=0;i<n;i++) { scanf("%d",&arr[i]); } root=build(arr,root,0,n-1); inorder(root); printf("\n");*/ /* int a,b; scanf("%d %d",&a,&b); lowest_ancestor(root,a,b);*/ /*int search; scanf("%d",&search); int start=0; int end=k-1; int sum; int flag=1; for(i=0;i<n;i++) { sum=arr[i]; start=i+1; end=n-1; search=search-sum; while(start<=end) { if(arr[start]+arr[end]==search) { flag=0; printf("%d %d %d",sum,arr[start],arr[end]); break; } else if(arr[start]+arr[end]>search) { end--; } else { start++; } } if(flag==0) break; }*/ /* searchi(root,search); //printf("%d",root->right->left->data); inorder(root); printf("\n"); preorder(root); printf("\n"); postorder(root); printf("\n");*/ return 0; }