Arrays. Please write program in C. Thank you. (i) The k-th order statistic of an
ID: 3729344 • Letter: A
Question
Arrays. 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
Please find my implementation.
int orderStatistic(const int *a, int size, int k) {
// creatin a copy of 'a'
int *copy = (int *)malloc(size*sizeof(int));
int i;
for(i=0;i<size; i++)
copy[i] = a[i];
// now sorting the copy array
// calling selection sort
selectionSort(copy, size);
int order = copy[k]; // kth order
// deleting copy
free(copy);
return order;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.