Please write program in C. Thank you. (i) The k-th order statistic of an array i
ID: 3731349 • Letter: P
Question
Please write program in C. Thank you.
(i) The k-th order statistic of an array is the k-th largest element. For our purposes, k starts at 0, thus the minimum element is the 0-th order statistic and the largest element is the n - 1-th order statistic. Another way to view it is: suppose we were to sort the array, then the k-th order statistic would be the element at index k in the sorted array. Write a function to find the k-th order statistic: int orderStatistic(const int *a, int size, int k); Note: you may use the selection sort algorithm in the code below to help you However, since the array a is labeled const in the prototype above, you cannot sort it directly. Think about implementing a deep-copy function for this purpose i void selectionSort (int *a, int size) f int i, j, min_index; for (i=0 ; 1size-1; i++) { min-index 1; for(j-i+1; j a[j]) 6 min-index = j ; 8 9 /swap int t = a[i] ; 12 a[min-index] t; 13 = 15Explanation / Answer
int orderStatististic(const int *a, int size, int k){
int i,j;
int min_index;
if (k >= size || k < 0){
return -1; //error situation
}
int *data = (int *)malloc(sizeof(int)*size));
for (i = 0; i<size; i++){
data[i] = a[i];
}
for (i = 0; i<size; i++){
min_index = i;
for (j = i+1; j<size; j++){
if (data[min_index] > data[j]){
min_index = j;
}
}
int t = data[i];
data[i] = data[min_index];
data[min_index] = t;
}
return data[k];
}