1.23 The median of an ordered set is an element such that the number of elements
ID: 3882592 • Letter: 1
Question
1.23 The median of an ordered set is an element such that the number of elements less than the median is within one of the number that are greater, assuming no ties. a. Write an algorithm to find the median of three distinct integers a, b, and c. b. Describe D, the set of inputs for your algorithm, in light of the discussion in Sec- tion 1.4.3 following Example 1.9 How many comparisons does your algorithm do in the worst case? On the average? How many comparisons are necessary in the worst case to find the median of three numbers? Justify your answer. c. d.Explanation / Answer
int getMedian(int a, int b , int c) { int x = a-b; int y = b-c; int z = a-c; if(x*y > 0) return b; if(x*z > 0) return c; return a; } c.) worst case-2,best case-1 d.)worst case-2, as if both if conditions are not satisfied then it will finally return a.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.