Question: using C language. Write a function that implements quickselect to find the k-th smallest element in an array of n elements (1 k n). JUST

using C language. Write a function that implements quickselect to find the k-th smallest element in an array of n elements (1 k n). JUST ONE FUNCTION. NO QUICK SORT int quickselect(int * a, int k, int n)

it has to be work with main function

#include  #include  int quickselect( int *A, int k, int n); int main(void) { long i; int *space; int tmp; space = (int *) malloc( 1000000*sizeof(int)); for( i=0; i< 500000; i++ ) { *(space + i) = ((139*i)%500000); *(space + i + 500000) = 1000000 + ((141*i)%500000); } if( (tmp = quickselect( space, 500001, 1000000)) != 1000000 ) { printf(" Failed test 1. Returned %d instead of 1000000 ", tmp); fflush(stdout); exit(-1); } else printf("passed test1 "); for( i=0; i< 500000; i++ ) { *(space + i) = ((139*i)%500000); *(space + i + 500000) = 1000000 + ((141*i)%500000); } if( (tmp = quickselect( space, 1, 1000000)) != 0 ) { printf(" Failed test 2. Returned %d instead of 0 ", tmp); fflush(stdout); exit(-1); } else printf("passed test2 "); for( i=0; i< 500000; i++ ) { *(space + i) = ((139*i)%500000); *(space + i + 500000) = 1000000 + ((141*i)%500000); } if( (tmp = quickselect( space, 124, 1000000)) != 123 ) { printf(" Failed test 3. Returned %d instead of 123 ", tmp); fflush(stdout); exit(-1); } else printf("passed test3 "); printf("Quickselect successful "); exit(0); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!