Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started