Find the complexity of the function used to find the kth sma
Find the complexity of the function used to find the kth smallest integer in an unordered array of integers. The code is given below. int selectkth (int a[], int k, int n) {int i, j, mini, tmp; for (i = 0; i
Solution
int selectkth(int a[], int k, int n){
int i, j, mini, tmp;
for(i=0; i<k; i++){ // we are iterating k times
mini = i;
for(j=i+1; j<n; j++){ // we are iterationg n times in worst case
if(a[j] < a[mini])
mini = j;
}
tmp = a[i];
a[i] = a[mini];
a[mini] = tmp;
}
}
This is Selection Sort, where outer for loop iterating only K times
Outer for loop iterates: K times
Inner for loop iterates: N times
So, time : Big-O(kn)
![Find the complexity of the function used to find the kth smallest integer in an unordered array of integers. The code is given below. int selectkth (int a[], i Find the complexity of the function used to find the kth smallest integer in an unordered array of integers. The code is given below. int selectkth (int a[], i](/WebImages/36/find-the-complexity-of-the-function-used-to-find-the-kth-sma-1107949-1761586814-0.webp)