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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site