src/vptree_cilk.c
// Returns the idx-th element of arr when arr is sorted
// idx is the index (starting from 1) of the point we want to find when the array is sorted. For the median idx should be the middle one (i.e (length+1)/2 for odd lengths etc)
Returns the idx-th element of arr when arr is sorted
idx is the index (starting from 1) of the point we want to find when the array is sorted. For the median idx should be the middle one /2 for odd lengths etc)
Returns the idx - th element of arr when arr is sorted idx is the index ( starting from 1 ) of the point we want to find when the array is sorted . For the median idx should be the middle one / 2 for odd lengths etc )
double quickselect(double arr[], int length, int idx){
    if (length == 1){
        return arr[0];
    }
    double pivot = arr[length - 1];
    int pivotIndex = partition(arr, 0, length - 1);
    int lowerLength = pivotIndex;
    pivotIndex++;
    int higherLength = (length - (lowerLength + 1));
    double *lower = calloc(lowerLength, sizeof(double));
    double *higher = calloc(higherLength, sizeof(double));
    memcpy(lower, arr, sizeof(double) * lowerLength);
    memcpy(higher, arr + pivotIndex, sizeof(double) * higherLength);
    double result = 0;
    if (idx <= lowerLength){
        result = quickselect(lower, lowerLength, idx);
    }
    else if(idx == pivotIndex){
        result = pivot;
    }
    else{
        result = quickselect(higher, higherLength, idx - pivotIndex);
    }
    free(lower);
    free(higher);
    return result;
}
double quickselect ( double arr [ ] , int length , int idx ) { if ( length == 1 ) { return arr [ 0 ] ; } double pivot = arr [ length - 1 ] ; int pivotIndex = partition ( arr , 0 , length - 1 ) ; int lowerLength = pivotIndex ; pivotIndex ++ ; int higherLength = ( length - ( lowerLength + 1 ) ) ; double * lower = calloc ( lowerLength , sizeof ( double ) ) ; double * higher = calloc ( higherLength , sizeof ( double ) ) ; memcpy ( lower , arr , sizeof ( double ) * lowerLength ) ; memcpy ( higher , arr + pivotIndex , sizeof ( double ) * higherLength ) ; double result = 0 ; if ( idx <= lowerLength ) { result = quickselect ( lower , lowerLength , idx ) ; } else if ( idx == pivotIndex ) { result = pivot ; } else { result = quickselect ( higher , higherLength , idx - pivotIndex ) ; } free ( lower ) ; free ( higher ) ; return result ; }
Returns the idx-th element of arr when arr is sorted
idx is the index (starting from 1) of the point we want to find when the array is sorted.
Returns the idx - th element of arr when arr is sorted idx is the index ( starting from 1 ) of the point we want to find when the array is sorted .
// Check to end recursion // Select last array element as pivot // Get index of pivot after we partition the array // Create the higher and lower arrays that occur after partitioning in QuickSort fashion // At this point pivotIndex, lowerLength and higherLength all start from 1 not 0 // Variable to store result of following recursive calls // This means that the point we're looking (median in our case) is in the lower partition // This means that the median is our pivot point // This means that the median is in the higher partition // Free memory allocated to lower and higher // Return result