static void test_dp_lookup_random2() { double T[256]; int nsamps; int ntrials = 10000; double t; int i, idx; srand( time(NULL) ); nsamps = sizeof(T) / sizeof(double); random_partition( T, nsamps ); for ( i=0; i<ntrials; ++i ) { t = (rand() % 100) / 99.0; idx = dp_lookup( T, nsamps, t ); CU_ASSERT(idx<nsamps-1); CU_ASSERT( (t>0.9999 && idx==nsamps-2) || (t >= T[idx] && t < T[idx+1]) ); /* if ( idx < nsamps-2 ) printf( "%f in [%f,%f)\n", t, T[idx], T[idx+1] ); else printf( "%f in [%f,%f]\n", t, T[idx], T[idx+1] ); */ } }
static void test_dp_edge_weight_timing1() { double *Q1=0, *T1=0; double *Q2=0, *T2=0; int *idxv1=0; int *idxv2=0; double *E=0; int *P=0; int nsamps1 = 200; int nsamps2 = 250; Q1 = (double*)malloc( (nsamps1-1)*sizeof(double) ); Q2 = (double*)malloc( (nsamps2-1)*sizeof(double) ); T1 = (double*)malloc( nsamps1*sizeof(double) ); T2 = (double*)malloc( nsamps2*sizeof(double) ); idxv1 = (int*)malloc( nsamps1*sizeof(int) ); idxv2 = (int*)malloc( nsamps2*sizeof(int) ); E = (double*)malloc( nsamps1*nsamps2*sizeof(double) ); P = (int*)malloc( nsamps1*nsamps2*sizeof(int) ); CU_ASSERT_PTR_NOT_NULL_FATAL(Q1); CU_ASSERT_PTR_NOT_NULL_FATAL(Q2); CU_ASSERT_PTR_NOT_NULL_FATAL(T1); CU_ASSERT_PTR_NOT_NULL_FATAL(T2); CU_ASSERT_PTR_NOT_NULL_FATAL(E); CU_ASSERT_PTR_NOT_NULL_FATAL(P); random_partition( T1, nsamps1 ); random_partition( T2, nsamps2 ); dp_all_indexes(T1,nsamps1,T1,nsamps1,idxv1); dp_all_indexes(T2,nsamps2,T2,nsamps2,idxv2); dp_costs( Q1, T1, nsamps1, Q2, T2, nsamps2, 1, T1, idxv1, nsamps1, T2, idxv2, nsamps2, E, P ); if ( Q1 ) free(Q1); if ( Q2 ) free(Q2); if ( T1 ) free(T1); if ( T2 ) free(T2); if ( idxv1) free(idxv1); if ( idxv2) free(idxv2); if ( E ) free(E); if ( P ) free(P); }
void quicksort(int a[], int start, int end) { if (start < end) { int p = random_partition(a, start, end); quicksort(a, start, p - 1); quicksort(a, p + 1, end); } }
int find_kth_element(int a[], int start, int end, int k) { if (start == end) return a[start]; else if (start < end) { int p = random_partition(a, start, end); int i = p - start + 1; if (i == k) return a[p]; else if (i > k) return find_kth_element(a, start, p - 1, k); else return find_kth_element(a, p + 1, end, k - i); } }
static void quick_sort_r(register int a[], register int left, register int right) { //if array is empty or only 1 element sort is redudant if (left < right && !is_sorted(a, left, right)) { if ((right - left) < THRESHOLD) { insertion_sort_sub(a, left, right + 1); return; } //get the random number register int random = rand() % (right - left + 1) + left; //swap random to the last swap((a + random), (a + right)); //get the pivot index register int pivot_index = random_partition(a, left, right); //recursively run the sort again with half-left and half-right of the array quick_sort_r(a, left, pivot_index - 1); quick_sort_r(a, pivot_index + 1, right); } }