The question might be a little off since I took a blurry pic

The question might be a little off since I took a blurry picture of the question, sorry. I DO NOT NEED ANSWER FOR QUESTION IV just part 3 in picture, I wrote the question down best I could.

QUESTION: Given a list of n numbers A[1,n]. Design an algorithm to find the k th smallest number, where k is an integer and lies in [1,n]. For instance, let A [1..7]~ {-3,3,-9,2,7,5,11}. If k-2, your algoirthm will return -1. If k-4 it will return 3. Please describe your algorithm in pseudo code with concise comments.

YOU CAN TRY TO READ IT YOURSELF FROM THIS PICTURE, It is the first question. I cannot retake sorry.

Part I11: Algorithm design. (S points Given a list of n number A1Ln) Designgoo Find the k-th omallest mamber, wheneg k-4, it will return 3 Plense describe yom any of the algorithms thas have been discessed in cless or sed in your Answer your homework ins trey ALI Part IV: Complexity analysis. (10 points) For the following two for-loops, please calculate the number of times needed to run each line. Write dowr answer in the last column. Unit Number of times needed cost Line # | Code to run each line l foro-n; j>-I ; J/3) //n is a power of 3 | CI sum = sum + j; C2 C3 C4 C5 3 for (i-I; i

Solution

To find k\'th smallest element we need to sort the list accending then return k\'th element

Pseudocode:
PROCEDURE Kth _Smallest_element
   //sorting the list
   for all elements of list
       if list[i] > list[i+1] //if the current element is greater than next element
           swap(list[i], list[i+1]) //swap the element
       end if
   end for
   //list sorted
   return list[k]
END PROCEDURE

The question might be a little off since I took a blurry picture of the question, sorry. I DO NOT NEED ANSWER FOR QUESTION IV just part 3 in picture, I wrote th

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site